Updated documentation #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Test Docker Image | |
on: | |
workflow_dispatch: | |
inputs: | |
run_tests: | |
type: boolean | |
description: "Test docker image after build" | |
required: true | |
default: true | |
push: | |
branches: | |
- main | |
env: | |
IMAGE_NAME: multi-arch-go | |
jobs: | |
docker_build: | |
runs-on: ${{ matrix.os }} | |
strategy: | |
fail-fast: false | |
matrix: | |
include: | |
- os: ${{vars.BASE_OS}} | |
platform: linux/amd64 | |
- os: ${{vars.BASE_OS}}-arm | |
platform: linux/arm64 | |
steps: | |
- name: Prepare | |
run: | | |
platform=${{ matrix.platform }} | |
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV | |
- name: Extract Docker image metadata | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ vars.DOCKER_USER }}/${{env.IMAGE_NAME}} | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{vars.DOCKER_USER}} | |
password: ${{secrets.DOCKER_PAT}} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Build and push by digest | |
id: build | |
uses: docker/build-push-action@v6 | |
with: | |
platforms: ${{ matrix.platform }} | |
labels: ${{ steps.meta.outputs.labels }} | |
tags: ${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}} | |
outputs: type=image,push-by-digest=true,name-canonical=true,push=true | |
- name: Export digest | |
run: | | |
mkdir -p ${{ runner.temp }}/digests | |
digest="${{ steps.build.outputs.digest }}" | |
touch "${{ runner.temp }}/digests/${digest#sha256:}" | |
- name: Upload digest | |
uses: actions/upload-artifact@v4 | |
with: | |
name: digests-${{ env.PLATFORM_PAIR }} | |
path: ${{ runner.temp }}/digests/* | |
if-no-files-found: error | |
retention-days: 1 | |
docker_merge: | |
needs: docker_build | |
runs-on: ${{vars.BASE_OS}}-arm | |
steps: | |
- name: Download digests | |
uses: actions/download-artifact@v4 | |
with: | |
path: ${{ runner.temp }}/digests | |
pattern: digests-* | |
merge-multiple: true | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{vars.DOCKER_USER}} | |
password: ${{secrets.DOCKER_PAT}} | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}} | |
tags: | | |
type=raw,value=latest,enable={{is_default_branch}} | |
type=ref,event=branch | |
type=ref,event=pr | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
- name: Create manifest list and push | |
working-directory: ${{ runner.temp }}/digests | |
run: | | |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ | |
$(printf '${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}}@sha256:%s ' *) | |
- name: Inspect image | |
run: | | |
docker buildx imagetools inspect ${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}}:${{ steps.meta.outputs.version }} | |
outputs: | |
image-tag: ${{ steps.meta.outputs.version }} | |
docker_test: | |
if: ${{github.event.inputs.run_tests == 'true' || github.event.inputs.run_tests == null}} | |
needs: docker_merge | |
runs-on: ${{ matrix.os }} | |
strategy: | |
matrix: | |
include: | |
- os: ${{vars.BASE_OS}} | |
platform: linux/amd64 | |
- os: ${{vars.BASE_OS}}-arm | |
platform: linux/arm64 | |
steps: | |
- name: Run Docker and check output | |
shell: bash | |
run: | | |
# Run our container | |
docker run -d --name ${{env.IMAGE_NAME}} --rm ${{vars.DOCKER_USER}}/${{env.IMAGE_NAME}}:${{ needs.docker_merge.outputs.image-tag }} | |
sleep 5 # Wait for the container to start | |
# Check the output | |
output=$(docker exec ${{env.IMAGE_NAME}} wget -qO- localhost:8080) | |
if [[ "$output" != "Hello from image NODE:, POD:, CPU PLATFORM:${{matrix.platform}}" ]]; then | |
echo "Unexpected output: $output" | |
exit 1 | |
fi | |
echo "Expected output received: $output" |