|
| 1 | +name: Build test application containers |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + agent: |
| 7 | + default: 'node' |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + registry: |
| 11 | + default: 'harbor.nbfc.io' |
| 12 | + required: false |
| 13 | + type: string |
| 14 | + workflow_dispatch: |
| 15 | + inputs: |
| 16 | + agent: |
| 17 | + default: 'node' |
| 18 | + required: false |
| 19 | + type: string |
| 20 | + registry: |
| 21 | + default: 'harbor.nbfc.io' |
| 22 | + required: false |
| 23 | + type: string |
| 24 | + |
| 25 | +env: |
| 26 | + REGISTRY: ${{ github.event.inputs.registry || 'harbor.nbfc.io' }} |
| 27 | + REGISTRY_IMAGE: ${{ github.event.inputs.registry || 'harbor.nbfc.io' }}/mlsysops/test-app |
| 28 | + RUNNER_ARCH_MAP: '[{"amd64":"x86_64", "arm64":"aarch64", "arm":"armv7l"}]' |
| 29 | + |
| 30 | +jobs: |
| 31 | + build: |
| 32 | + name: Build Docker Image |
| 33 | + runs-on: ${{ format('{0}-{1}', 'base-dind-2204', matrix.arch) }} |
| 34 | + strategy: |
| 35 | + matrix: |
| 36 | + arch: ["arm64", "amd64"] |
| 37 | + fail-fast: false |
| 38 | + outputs: |
| 39 | + digest-amd64: ${{ steps.set-outputs.outputs.digest-amd64 }} |
| 40 | + digest-arm64: ${{ steps.set-outputs.outputs.digest-arm64 }} |
| 41 | + |
| 42 | + steps: |
| 43 | + - name: Checkout repo |
| 44 | + uses: actions/checkout@v4 |
| 45 | + |
| 46 | + - name: Login to registry ${{ env.REGISTRY }} |
| 47 | + uses: docker/login-action@v3 |
| 48 | + with: |
| 49 | + registry: ${{ env.REGISTRY }} |
| 50 | + username: ${{ secrets.HARBOR_USER }} |
| 51 | + password: ${{ secrets.HARBOR_SECRET }} |
| 52 | + |
| 53 | + - name: Set up Docker Buildx |
| 54 | + uses: docker/setup-buildx-action@v3 |
| 55 | + |
| 56 | + - name: Extract Docker metadata |
| 57 | + id: meta |
| 58 | + uses: docker/metadata-action@v5 |
| 59 | + with: |
| 60 | + images: ${{ env.REGISTRY_IMAGE }} |
| 61 | + tags: | |
| 62 | + type=sha,prefix=${{ matrix.arch }}- |
| 63 | + type=ref,event=branch,prefix=${{ matrix.arch }}- |
| 64 | +
|
| 65 | + - name: Build and push ${{ matrix.arch }} image |
| 66 | + id: build-and-push |
| 67 | + uses: docker/build-push-action@v6 |
| 68 | + with: |
| 69 | + context: ./tests/application |
| 70 | + tags: ${{ steps.meta.outputs.tags }} |
| 71 | + labels: ${{ steps.meta.outputs.labels }} |
| 72 | + platforms: linux/${{ matrix.arch }} |
| 73 | + push: true |
| 74 | + file: ./tests/application/Dockerfile |
| 75 | + provenance: false |
| 76 | + build-args: | |
| 77 | + ARCHTAG=${{ fromJson(env.RUNNER_ARCH_MAP)[0][matrix.arch] }} |
| 78 | + BRANCH=${{ github.event.ref_name || github.ref_name }} |
| 79 | +
|
| 80 | + - name: Set ${{ matrix.arch }} digest output |
| 81 | + id: set-outputs |
| 82 | + run: | |
| 83 | + # Workaround for https://github.com/actions/runner/issues/2499 |
| 84 | + echo "digest-${{ matrix.arch }}=${{ steps.build-and-push.outputs.digest }}" \ |
| 85 | + >> "$GITHUB_OUTPUT" |
| 86 | + shell: bash |
| 87 | + |
| 88 | + create-manifest: |
| 89 | + name: Create Merged Docker Image Manifest |
| 90 | + needs: [build] |
| 91 | + runs-on: 'base-dind-2204-amd64' |
| 92 | + outputs: |
| 93 | + digest-merged: ${{ steps.inspect.outputs.digest-merged }} |
| 94 | + |
| 95 | + steps: |
| 96 | + - name: Checkout repo |
| 97 | + uses: actions/checkout@v4 |
| 98 | + |
| 99 | + - name: Login to registry ${{ inputs.REGISTRY }} |
| 100 | + uses: docker/login-action@v3 |
| 101 | + with: |
| 102 | + registry: ${{ env.REGISTRY }} |
| 103 | + username: ${{ secrets.HARBOR_USER }} |
| 104 | + password: ${{ secrets.HARBOR_SECRET }} |
| 105 | + |
| 106 | + - name: Set up Docker Buildx |
| 107 | + uses: docker/setup-buildx-action@v3 |
| 108 | + |
| 109 | + - name: Extract Docker metadata |
| 110 | + id: meta |
| 111 | + uses: docker/metadata-action@v5 |
| 112 | + with: |
| 113 | + images: ${{ env.REGISTRY_IMAGE }} |
| 114 | + tags: | |
| 115 | + type=sha |
| 116 | + type=ref,event=branch |
| 117 | + type=raw,value=latest |
| 118 | +
|
| 119 | + - name: Create and push manifest |
| 120 | + run: | |
| 121 | + docker buildx imagetools create \ |
| 122 | + $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< \ |
| 123 | + "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 124 | + ${{ env.REGISTRY_IMAGE }}@${{ needs.build.outputs.digest-amd64 }} \ |
| 125 | + ${{ env.REGISTRY_IMAGE }}@${{ needs.build.outputs.digest-arm64 }} |
| 126 | + shell: bash |
| 127 | + |
| 128 | + - name: Inspect merged image |
| 129 | + id: inspect |
| 130 | + run: | |
| 131 | + docker buildx imagetools inspect \ |
| 132 | + ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }} |
| 133 | + digest=$(docker buildx imagetools inspect \ |
| 134 | + ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }} \ |
| 135 | + --format '{{json .Manifest}}' | jq -r '.digest') |
| 136 | + if [[ -z "${digest}" ]]; then |
| 137 | + echo "Could not get merged image digest" |
| 138 | + exit 1 |
| 139 | + fi |
| 140 | + echo "digest-merged=${digest}" >> "$GITHUB_OUTPUT" |
| 141 | + shell: bash |
| 142 | + |
| 143 | + sign: |
| 144 | + name: Sign Docker Images |
| 145 | + needs: [build, create-manifest] |
| 146 | + runs-on: 'base-dind-2204-amd64' |
| 147 | + permissions: |
| 148 | + contents: read |
| 149 | + id-token: write |
| 150 | + |
| 151 | + steps: |
| 152 | + - name: Install Cosign |
| 153 | + uses: sigstore/cosign-installer@v3 |
| 154 | + |
| 155 | + - name: Verify Cosign installation |
| 156 | + run: cosign version |
| 157 | + |
| 158 | + - name: Login to registry ${{ env.REGISTRY }} |
| 159 | + uses: docker/login-action@v3 |
| 160 | + with: |
| 161 | + registry: ${{ env.REGISTRY }} |
| 162 | + username: ${{ secrets.HARBOR_USER }} |
| 163 | + password: ${{ secrets.HARBOR_SECRET }} |
| 164 | + |
| 165 | + - name: Sign published Docker images |
| 166 | + env: |
| 167 | + DIGESTS: >- |
| 168 | + ${{ needs.create-manifest.outputs.digest-merged }} |
| 169 | + ${{ needs.build.outputs.digest-amd64 }} |
| 170 | + ${{ needs.build.outputs.digest-arm64 }} |
| 171 | + run: | |
| 172 | + for digest in ${DIGESTS}; do |
| 173 | + cosign sign --yes ${{ env.REGISTRY_IMAGE }}@${digest} \ |
| 174 | + -a "repo=${{ github.repository }}" \ |
| 175 | + -a "workflow=${{ github.workflow }}" \ |
| 176 | + -a "ref=${{ github.sha }}" \ |
| 177 | + -a "author=Nubificus LTD" |
| 178 | + done |
| 179 | + shell: bash |
0 commit comments