Additional Variables #3
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: | |
| USER: ${{secrets.DOCKER_USER}} | |
| IMAGE_NAME: docker-test | |
| jobs: | |
| docker_build: | |
| runs-on: ${{ matrix.os }} | |
| strategy: | |
| matrix: | |
| include: | |
| - os: ${{vars.BASE_OS}} | |
| arch: amd64 | |
| - os: ${{vars.BASE_OS}}-arm | |
| arch: arm64 | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{env.USER}} | |
| password: ${{secrets.DOCKER_PAT}} | |
| - name: Build and push | |
| run: | | |
| docker build --tag ${{env.USER}}/${{env.IMAGE_NAME}}:${{matrix.arch}} . | |
| docker push ${{env.USER}}/${{env.IMAGE_NAME}}:${{matrix.arch}} | |
| docker_manifest: | |
| needs: docker_build | |
| runs-on: ${{vars.BASE_OS}}-arm | |
| steps: | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{env.USER}} | |
| password: ${{secrets.DOCKER_PAT}} | |
| - name: Create and push manifest | |
| run: | | |
| docker manifest create ${{env.USER}}/${{env.IMAGE_NAME}} ${{env.USER}}/${{env.IMAGE_NAME}}:amd64 ${{env.USER}}/${{env.IMAGE_NAME}}:arm64 | |
| docker manifest push ${{env.USER}}/${{env.IMAGE_NAME}} | |
| docker_test: | |
| if: ${{github.event.inputs.run_tests == 'true' || github.event.inputs.run_tests == null}} | |
| needs: docker_manifest | |
| 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 ${{env.USER}}/${{env.IMAGE_NAME}}:latest | |
| 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" |