|
| 1 | +name: Container Build |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + release_version: |
| 7 | + description: Condition based on release branch build |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + platform_list: |
| 11 | + required: true |
| 12 | + description: platform name from input |
| 13 | + type: string # like linux/amd64,linux/arm64 |
| 14 | + |
| 15 | + secrets: |
| 16 | + docker-user: |
| 17 | + description: 'Secret token from caller workflow to access private packages' |
| 18 | + required: true |
| 19 | + docker-token: |
| 20 | + description: 'Secret token from caller workflow to access private packages' |
| 21 | + required: true |
| 22 | + |
| 23 | +env: |
| 24 | + REGISTRY: keyfactor |
| 25 | + |
| 26 | +jobs: |
| 27 | + setup: |
| 28 | + runs-on: ubuntu-latest |
| 29 | + outputs: |
| 30 | + platform_matrix: ${{ steps.vars.outputs.platform_matrix }} |
| 31 | + steps: |
| 32 | + - name: Create an array from platform_list input |
| 33 | + id: vars |
| 34 | + run: echo "platform_matrix=$(jq 'split(",")' -Rc <(echo '${{ inputs.platform_list }}'))" | tee -a $GITHUB_OUTPUT | tee -a $GITHUB_STEP_SUMMARY |
| 35 | + get-release-values: |
| 36 | + name: Get Release Properties |
| 37 | + runs-on: ubuntu-latest |
| 38 | + outputs: |
| 39 | + PUSH_IMAGE: ${{ steps.set-vars.outputs.PUSH_IMAGE }} |
| 40 | + steps: |
| 41 | + - name: Determine IMAGE_PUSH and VERSION |
| 42 | + id: set-vars |
| 43 | + run: | |
| 44 | + if [[ ("${{ github.event_name }}" == "pull_request") && "${{ inputs.release_version }}" != "" ]]; then |
| 45 | + echo "PUSH_IMAGE=true" | tee -a "$GITHUB_OUTPUT" | tee -a "$GITHUB_STEP_SUMMARY" |
| 46 | + echo "VERSION=${{ inputs.release_version }}" | tee -a "$GITHUB_OUTPUT" | tee -a "$GITHUB_STEP_SUMMARY" |
| 47 | + else |
| 48 | + echo "PUSH_IMAGE=false" | tee -a "$GITHUB_OUTPUT" | tee -a "$GITHUB_STEP_SUMMARY" |
| 49 | + echo "VERSION=0.0.0" | tee -a "$GITHUB_OUTPUT" | tee -a "$GITHUB_STEP_SUMMARY" |
| 50 | + fi |
| 51 | + run-container-build: |
| 52 | + name: Build Containers |
| 53 | + runs-on: ubuntu-latest |
| 54 | + needs: [setup,get-release-values] |
| 55 | + strategy: |
| 56 | + fail-fast: false |
| 57 | + matrix: |
| 58 | + platform: ${{ fromJson(needs.setup.outputs.platform_matrix) }} |
| 59 | + |
| 60 | + permissions: |
| 61 | + contents: read |
| 62 | + packages: write |
| 63 | + |
| 64 | + steps: |
| 65 | + - name: Prepare |
| 66 | + run: | |
| 67 | + platform=${{ matrix.platform }} |
| 68 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 69 | +
|
| 70 | + - name: Set IMAGE_NAME |
| 71 | + run: | |
| 72 | + echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" | tee -a ${GITHUB_ENV} |
| 73 | +
|
| 74 | + # Checkout code |
| 75 | + # https://github.com/keyfactor/checkout |
| 76 | + - name: Checkout code |
| 77 | + uses: keyfactor/checkout@v4 |
| 78 | + |
| 79 | + # Extract metadata (tags, labels) for Docker |
| 80 | + # https://github.com/docker/metadata-action |
| 81 | + - name: Extract Docker metadata |
| 82 | + id: meta |
| 83 | + uses: keyfactor/metadata-action@v5.5.1 |
| 84 | + with: |
| 85 | + images: ${{ env.IMAGE_NAME }} |
| 86 | + tags: | |
| 87 | + type=semver,pattern={{raw}},value=${{ inputs.release_version }} |
| 88 | +
|
| 89 | + # Set up QEMU |
| 90 | + # https://github.com/docker/setup-qemu-action |
| 91 | + - name: Set up QEMU |
| 92 | + uses: keyfactor/setup-qemu-action@v3.0.0 |
| 93 | + |
| 94 | + # Set up BuildKit Docker container builder to be able to build |
| 95 | + # multi-platform images and export cache |
| 96 | + # https://github.com/docker/setup-buildx-action |
| 97 | + - name: Set up Docker Buildx |
| 98 | + uses: keyfactor/setup-buildx-action@v3.3.0 |
| 99 | + |
| 100 | + # Login to Docker registry |
| 101 | + # https://github.com/docker/login-action |
| 102 | + - name: Log into registry ${{ env.REGISTRY }} |
| 103 | + uses: keyfactor/login-action@v3.2.0 |
| 104 | + with: |
| 105 | + # registry: ${{ env.REGISTRY }} # Remove for dockerhub |
| 106 | + username: ${{ secrets.docker-user }} |
| 107 | + password: ${{ secrets.docker-token }} |
| 108 | + |
| 109 | + # Build and push Docker image with Buildx |
| 110 | + # https://github.com/docker/build-push-action |
| 111 | + - name: Build and push Docker image |
| 112 | + id: build |
| 113 | + uses: keyfactor/build-push-action@v6.0.1 |
| 114 | + with: |
| 115 | + context: . |
| 116 | + platforms: ${{ matrix.platform }} |
| 117 | + labels: ${{ steps.meta.outputs.labels }} |
| 118 | + push: ${{ needs.get-release-values.outputs.PUSH_IMAGE == 'true' }} |
| 119 | + outputs: type=image,name=${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true |
| 120 | + |
| 121 | + # Export digest |
| 122 | + - name: Export digest |
| 123 | + run: | |
| 124 | + mkdir -p /tmp/digests |
| 125 | + digest="${{ steps.build.outputs.digest }}" |
| 126 | + touch "/tmp/digests/${digest#sha256:}" |
| 127 | +
|
| 128 | + # Upload digest |
| 129 | + - name: Upload digest |
| 130 | + uses: keyfactor/upload-artifact@v4 |
| 131 | + with: |
| 132 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 133 | + path: /tmp/digests/* |
| 134 | + if-no-files-found: error |
| 135 | + retention-days: 1 |
| 136 | + |
| 137 | + merge: |
| 138 | + runs-on: ubuntu-latest |
| 139 | + needs: [get-release-values, run-container-build] |
| 140 | + steps: |
| 141 | + - name: Set IMAGE_NAME |
| 142 | + run: | |
| 143 | + echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" | tee -a ${GITHUB_ENV} |
| 144 | + echo "PUSH_IMAGE = ${{ needs.get-release-values.outputs.PUSH_IMAGE }}" |
| 145 | +
|
| 146 | + # Download digests |
| 147 | + # https://github.com/actions/download-artifact |
| 148 | + - name: Download digests |
| 149 | + uses: keyfactor/download-artifact@v4.1.7 |
| 150 | + with: |
| 151 | + path: /tmp/digests |
| 152 | + pattern: digests-* |
| 153 | + merge-multiple: true |
| 154 | + |
| 155 | + # Set up BuildKit Docker container builder to be able to build |
| 156 | + # multi-platform images and export cache |
| 157 | + # https://github.com/docker/setup-buildx-action |
| 158 | + - name: Set up Docker Buildx |
| 159 | + uses: keyfactor/setup-buildx-action@v3.3.0 |
| 160 | + |
| 161 | + # Extract metadata (tags, labels) for Docker |
| 162 | + # https://github.com/docker/metadata-action |
| 163 | + - name: Extract Docker metadata |
| 164 | + id: meta |
| 165 | + uses: keyfactor/metadata-action@v5.5.1 |
| 166 | + with: |
| 167 | + images: ${{ env.IMAGE_NAME }} |
| 168 | + tags: | |
| 169 | + type=semver,pattern={{raw}},value=${{ inputs.release_version }} |
| 170 | +
|
| 171 | + # Login to Docker registry |
| 172 | + # https://github.com/docker/login-action |
| 173 | + - name: Log into registry ${{ env.REGISTRY }} |
| 174 | + uses: keyfactor/login-action@v3.2.0 |
| 175 | + with: |
| 176 | + # registry: ${{ env.REGISTRY }} # Remove for dockerhub |
| 177 | + username: ${{ secrets.docker-user }} |
| 178 | + password: ${{ secrets.docker-token }} |
| 179 | + |
| 180 | + # Create manifest list and push if: needs.get-release-values.outputs.PUSH_IMAGE == true |
| 181 | + - name: Create manifest list and push |
| 182 | + if: needs.get-release-values.outputs.PUSH_IMAGE == 'true' |
| 183 | + working-directory: /tmp/digests |
| 184 | + run: | |
| 185 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 186 | + $(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *) |
| 187 | +
|
| 188 | + - name: Inspect image |
| 189 | + if: needs.get-release-values.outputs.PUSH_IMAGE == 'true' |
| 190 | + run: | |
| 191 | + docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} |
0 commit comments