|
| 1 | +--- |
| 2 | +name: Build & Test |
| 3 | +# description: Build the final container image and run tests on it |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_call: |
| 7 | + inputs: |
| 8 | + registry: |
| 9 | + type: string |
| 10 | + description: Target registry to push the final image. |
| 11 | + default: ghcr.io |
| 12 | + namespace: |
| 13 | + description: Namespace of the container image. |
| 14 | + default: ansible |
| 15 | + type: string |
| 16 | + final_image: |
| 17 | + description: Name of the final image. |
| 18 | + default: community-ansible-dev-tools |
| 19 | + type: string |
| 20 | + push: |
| 21 | + description: If it should push the result of not. Accepts only true / false strings. |
| 22 | + default: ${{ github.event_name == 'release' && github.event.action == 'published' }} |
| 23 | + type: string |
| 24 | +jobs: |
| 25 | + build-test: |
| 26 | + runs-on: ${{ matrix.builder }} |
| 27 | + name: ${{ matrix.name }} |
| 28 | + strategy: |
| 29 | + fail-fast: false |
| 30 | + matrix: |
| 31 | + include: |
| 32 | + - builder: devtools-multiarch-builder |
| 33 | + platform: linux/amd64 |
| 34 | + name: amd64 |
| 35 | + - builder: devtools-arm64-runner |
| 36 | + platform: linux/arm64 |
| 37 | + name: arm64 |
| 38 | + services: |
| 39 | + registry: |
| 40 | + image: registry:2 |
| 41 | + ports: |
| 42 | + - 5000:5000 |
| 43 | + |
| 44 | + steps: |
| 45 | + - name: Check out repository |
| 46 | + uses: actions/checkout@v4 |
| 47 | + |
| 48 | + - name: Login to GitHub Container Registry |
| 49 | + uses: docker/login-action@v3 |
| 50 | + with: |
| 51 | + registry: ghcr.io |
| 52 | + username: ${{ github.actor }} |
| 53 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 54 | + |
| 55 | + - name: Prune docker system |
| 56 | + run: sudo ./final/docker-prune.sh |
| 57 | + |
| 58 | + - name: Prepare |
| 59 | + shell: bash |
| 60 | + run: | |
| 61 | + platform=${{ matrix.platform }} |
| 62 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 63 | + sudo apt install -y python3-pip python3-build pipx |
| 64 | +
|
| 65 | + - name: Set up Docker Buildx |
| 66 | + uses: docker/setup-buildx-action@v3 |
| 67 | + with: |
| 68 | + # network=host driver-opt needed to push to local registry |
| 69 | + driver-opts: network=host |
| 70 | + buildkitd-flags: --debug |
| 71 | + |
| 72 | + - name: Install or upgrade tools needed for the build and test |
| 73 | + shell: bash |
| 74 | + id: ansible-builder-install |
| 75 | + run: | |
| 76 | + set -ex |
| 77 | + python3 -m pipx install --force ansible-builder |
| 78 | + python3 -m build --outdir final/dist/ --wheel |
| 79 | +
|
| 80 | + - name: Create a build context and Containerfile for base EE |
| 81 | + shell: bash |
| 82 | + run: | |
| 83 | + ansible-builder create -f ${{ github.workspace }}/execution-environment.yml --output-filename Containerfile -v3 |
| 84 | +
|
| 85 | + - name: Build base image for ${{ matrix.platform }} |
| 86 | + uses: docker/build-push-action@v6 |
| 87 | + id: build-base |
| 88 | + with: |
| 89 | + context: context |
| 90 | + provenance: false |
| 91 | + file: context/Containerfile |
| 92 | + platforms: ${{ matrix.platform }} |
| 93 | + push: true |
| 94 | + tags: localhost:5000/${{ inputs.final_image }}-base:latest |
| 95 | + cache-from: type=gha,scope=build-${{ env.PLATFORM_PAIR }} |
| 96 | + cache-to: type=gha,scope=build-${{ env.PLATFORM_PAIR }} |
| 97 | + |
| 98 | + - name: Show available images & base image manifest |
| 99 | + shell: bash |
| 100 | + run: | |
| 101 | + curl -X GET http://localhost:5000/v2/${{ inputs.final_image }}-base/tags/list |
| 102 | + docker manifest inspect localhost:5000/${{ inputs.final_image }}-base --insecure -v |
| 103 | +
|
| 104 | + - name: Build final image for ${{ matrix.platform }} |
| 105 | + id: build-final |
| 106 | + uses: docker/build-push-action@v6 |
| 107 | + env: |
| 108 | + DOCKER_BUILD_SUMMARY: "false" |
| 109 | + with: |
| 110 | + context: ${{ github.workspace }}/final |
| 111 | + provenance: false |
| 112 | + file: ${{ github.workspace }}/final/Containerfile |
| 113 | + load: true |
| 114 | + tags: | |
| 115 | + ${{ inputs.namespace }}/${{ inputs.final_image }}:test |
| 116 | + build-contexts: | |
| 117 | + ${{ inputs.final_image }}-base=docker-image://localhost:5000/${{ inputs.final_image }}-base:latest |
| 118 | + platforms: ${{ matrix.platform }} |
| 119 | + cache-from: type=gha,scope=build-${{ env.PLATFORM_PAIR }} |
| 120 | + cache-to: type=gha,scope=build-${{ env.PLATFORM_PAIR }} |
| 121 | + |
| 122 | + - name: Squash image layers to save disk space |
| 123 | + shell: bash |
| 124 | + run: | |
| 125 | + python3 -m pipx install --force docker-squash |
| 126 | + docker-squash ${{ inputs.namespace }}/${{ inputs.final_image }}:test |
| 127 | +
|
| 128 | + - name: Run tests against the container |
| 129 | + shell: bash |
| 130 | + run: | |
| 131 | + python3 -m pipx install --force "tox>=4.0.0" |
| 132 | + tox -e test-image -- --container-engine docker --image-name ${{ inputs.namespace }}/${{ inputs.final_image }}:test |
| 133 | +
|
| 134 | + - name: Push the built image to ${{ inputs.registry }} by digest for ${{ matrix.platform }} |
| 135 | + id: push-final |
| 136 | + if: inputs.push == 'true' |
| 137 | + uses: docker/build-push-action@v6 |
| 138 | + with: |
| 139 | + context: ${{ github.workspace }}/final |
| 140 | + provenance: false |
| 141 | + file: ${{ github.workspace }}/final/Containerfile |
| 142 | + build-contexts: | |
| 143 | + ${{ inputs.final_image }}-base=docker-image://localhost:5000/${{ inputs.final_image }}-base:latest |
| 144 | + platforms: ${{ matrix.platform }} |
| 145 | + outputs: type=image,name=${{ inputs.registry }}/${{ inputs.namespace }}/${{ inputs.final_image }},push-by-digest=true,name-canonical=true,push=true |
| 146 | + |
| 147 | + - name: Export digest |
| 148 | + if: inputs.push == 'true' |
| 149 | + shell: bash |
| 150 | + run: | |
| 151 | + rm -rf /tmp/digests |
| 152 | + mkdir -p /tmp/digests |
| 153 | + digest="${{ steps.push-final.outputs.digest }}" |
| 154 | + touch "/tmp/digests/${digest#sha256:}" |
| 155 | +
|
| 156 | + - name: Upload digest |
| 157 | + if: inputs.push == 'true' |
| 158 | + uses: actions/upload-artifact@v4 |
| 159 | + with: |
| 160 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 161 | + path: /tmp/digests/* |
| 162 | + if-no-files-found: error |
| 163 | + retention-days: 1 |
| 164 | + |
| 165 | + # this step is ONLY needed for maintainence of self hosted runners |
| 166 | + - name: Cleanup docker |
| 167 | + shell: bash |
| 168 | + if: always() |
| 169 | + run: | |
| 170 | + docker system prune -af --volumes |
0 commit comments