Skip to content

Commit

Permalink
Build multi-platform docker images
Browse files Browse the repository at this point in the history
Also fixes bug with "/" characters in image names, although chaincode
labels now have the same issue since they are used in filenames!

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti committed Nov 11, 2022
1 parent 0a329bd commit 5a86047
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 48 deletions.
86 changes: 45 additions & 41 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,81 @@ name: Docker CI
on:
workflow_call:
inputs:
imagename:
description: 'A Docker image name passed from the caller workflow'
image-name:
description: 'A Docker image name passed from the caller workflow.'
required: true
type: string
path:
description: 'A path containing a Dockerfile passed from the caller workflow'
description: 'A path containing a Dockerfile passed from the caller workflow.'
required: true
type: string
chaincode:
description: 'A boolean indicating whether to create a chaincode package passed from the caller workflow'
chaincode-label:
description: 'An optional chaincode label passed from the caller workflow. If present, will prepare a chaincode package.'
required: false
default: false
type: boolean
type: string

jobs:
build:
runs-on: ubuntu-latest
outputs:
image_digest: ${{ steps.publish_image.outputs.image_digest }}
image_digest: ${{ steps.push.outputs.digest }}

steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: |
docker build ${DOCKER_BUILD_PATH} --file ${DOCKER_BUILD_PATH}/Dockerfile --label "org.opencontainers.image.revision=${GITHUB_SHA}" --tag ${IMAGE_NAME}
docker tag ${IMAGE_NAME} ghcr.io/hyperledger-labs/${IMAGE_NAME}:${GITHUB_SHA}
if [ "${GITHUB_REF:0:10}" = "refs/tags/" ]; then
docker tag ${IMAGE_NAME} ghcr.io/hyperledger-labs/${IMAGE_NAME}:${GITHUB_REF_NAME}
docker tag ${IMAGE_NAME} ghcr.io/hyperledger-labs/${IMAGE_NAME}:latest
fi
env:
DOCKER_BUILD_PATH: ${{ inputs.path }}
IMAGE_NAME: ${{ inputs.imagename }}
- name: Publish Docker image
id: publish_image
if: github.event_name != 'pull_request'
run: |
echo ${DOCKER_PW} | docker login ghcr.io -u ${DOCKER_USER} --password-stdin
docker push ghcr.io/hyperledger-labs/${IMAGE_NAME}:${GITHUB_SHA}
if [ "${GITHUB_REF:0:10}" = "refs/tags/" ]; then
docker push ghcr.io/hyperledger-labs/${IMAGE_NAME}:${GITHUB_REF_NAME}
docker push ghcr.io/hyperledger-labs/${IMAGE_NAME}:latest
fi
echo ::set-output name=image_digest::$(docker inspect --format='{{index .RepoDigests 0}}' ghcr.io/hyperledger-labs/${IMAGE_NAME}:${GITHUB_SHA} | cut -d'@' -f2)
env:
IMAGE_NAME: ${{ inputs.imagename }}
DOCKER_USER: ${{ github.actor }}
DOCKER_PW: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout
uses: actions/checkout@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ inputs.image-name }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,format=long
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
id: push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

package:
if: inputs.chaincode && needs.build.outputs.image_digest != ''
if: inputs.chaincode-label != '' && needs.build.outputs.image_digest != ''
needs: build
runs-on: ubuntu-latest

steps:
- name: Create package
uses: hyperledgendary/package-k8s-chaincode-action@ba10aea43e3d4f7991116527faf96e3c2b07abc7
with:
chaincode-label: ${{ inputs.imagename }}
chaincode-image: ghcr.io/hyperledger-labs/${{ inputs.imagename }}
chaincode-label: ${{ inputs.chaincode-label }}
chaincode-image: ${{ inputs.image-name }}
chaincode-digest: ${{ needs.build.outputs.image_digest }}

- name: Rename package
if: startsWith(github.ref, 'refs/tags/v')
run: mv ${IMAGE_NAME}.tgz ${IMAGE_NAME}-${CHAINCODE_VERSION}.tgz
run: mv ${CHAINCODE_LABEL}.tgz ${CHAINCODE_LABEL}-${CHAINCODE_VERSION}.tgz
env:
IMAGE_NAME: ${{ inputs.imagename }}
CHAINCODE_LABEL: ${{ inputs.chaincode-label }}
CHAINCODE_VERSION: ${{ github.ref_name }}

- name: Release package
if: startsWith(github.ref, 'refs/tags/v')
uses: softprops/action-gh-release@v1
with:
files: ${{ inputs.imagename }}-${{ github.ref_name }}.tgz
files: ${{ inputs.chaincode-label }}-${{ github.ref_name }}.tgz
4 changes: 2 additions & 2 deletions .github/workflows/go-contract-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ jobs:
name: Docker build
uses: ./.github/workflows/docker-build.yml
with:
imagename: go-contract
image-name: ghcr.io/hyperledger-labs/fabric-builder-k8s/sample-go-contract
path: samples/go-contract
chaincode: true
chaincode-label: go-contract
4 changes: 2 additions & 2 deletions .github/workflows/java-contract-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ jobs:
name: Docker build
uses: ./.github/workflows/docker-build.yml
with:
imagename: java-contract
image-name: ghcr.io/hyperledger-labs/fabric-builder-k8s/sample-java-contract
path: samples/java-contract
chaincode: true
chaincode-label: java-contract
4 changes: 2 additions & 2 deletions .github/workflows/node-contract-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ jobs:
name: Docker build
uses: ./.github/workflows/docker-build.yml
with:
imagename: node-contract
image-name: ghcr.io/hyperledger-labs/fabric-builder-k8s/sample-node-contract
path: samples/node-contract
chaincode: true
chaincode-label: node-contract
2 changes: 1 addition & 1 deletion .github/workflows/peer-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ jobs:
name: Docker build
uses: ./.github/workflows/docker-build.yml
with:
imagename: k8s-fabric-peer
image-name: ghcr.io/hyperledger-labs/fabric-builder-k8s/k8s-fabric-peer
path: .

0 comments on commit 5a86047

Please sign in to comment.