|
| 1 | +name: Build Image and Push to Registries |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + DOCKERFILE_LOCATION: |
| 7 | + required: false |
| 8 | + type: string |
| 9 | + default: "./Dockerfile" |
| 10 | + AWS_REGION: |
| 11 | + required: false |
| 12 | + type: string |
| 13 | + default: "us-east-1" |
| 14 | + AWS_ECR_REGISTRY: |
| 15 | + required: false |
| 16 | + type: string |
| 17 | + default: "707767160287.dkr.ecr.us-east-1.amazonaws.com" |
| 18 | + DOCKERFILE_BUILD_CONTEXT: |
| 19 | + required: false |
| 20 | + type: string |
| 21 | + default: "." |
| 22 | + OVERRIDE_REPO_NAME: |
| 23 | + required: false |
| 24 | + type: string |
| 25 | + default: "" |
| 26 | + OVERRIDE_TAG_NAME: |
| 27 | + required: false |
| 28 | + type: string |
| 29 | + default: "" |
| 30 | + USE_QUAY_ONLY: |
| 31 | + required: false |
| 32 | + type: boolean |
| 33 | + default: false |
| 34 | + BUILD_PLATFORMS: |
| 35 | + required: false |
| 36 | + type: string |
| 37 | + default: "linux/amd64, linux/arm64" |
| 38 | + secrets: |
| 39 | + ECR_AWS_ACCESS_KEY_ID: |
| 40 | + required: true |
| 41 | + ECR_AWS_SECRET_ACCESS_KEY: |
| 42 | + required: true |
| 43 | + QUAY_USERNAME: |
| 44 | + required: true |
| 45 | + QUAY_ROBOT_TOKEN: |
| 46 | + required: true |
| 47 | + |
| 48 | +jobs: |
| 49 | + build: |
| 50 | + name: Build Images |
| 51 | + strategy: |
| 52 | + fail-fast: false |
| 53 | + matrix: |
| 54 | + include: |
| 55 | + - platform: linux/amd64 |
| 56 | + runner: ubuntu-22.04 |
| 57 | + - platform: linux/arm64 |
| 58 | + runner: ubuntu-22.04-arm |
| 59 | + runs-on: ${{ matrix.runner || 'ubuntu-22.04' }} |
| 60 | + steps: |
| 61 | + - name: Prepare |
| 62 | + run: | |
| 63 | + platform=${{ matrix.platform }} |
| 64 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 65 | +
|
| 66 | +
|
| 67 | + - name: Checkout |
| 68 | + uses: actions/checkout@v4 |
| 69 | + with: |
| 70 | + persist-credentials: false |
| 71 | + fetch-depth: 0 |
| 72 | + |
| 73 | + - name: Set up QEMU |
| 74 | + uses: docker/setup-qemu-action@v3 |
| 75 | + |
| 76 | + - name: Set up Docker Buildx |
| 77 | + |
| 78 | + |
| 79 | + - name: Set Variables |
| 80 | + shell: bash |
| 81 | + run: | |
| 82 | + echo "OVERRIDE_REPO_NAME = ${{ inputs.OVERRIDE_REPO_NAME }}" |
| 83 | + echo "OVERRIDE_TAG_NAME = ${{ inputs.OVERRIDE_TAG_NAME }}" |
| 84 | +
|
| 85 | + if [[ -z "${{ inputs.OVERRIDE_TAG_NAME }}" ]] |
| 86 | + then |
| 87 | + echo "No OVERRIDE_TAG_NAME input provided, defaulting to current branch/tag name..." |
| 88 | + echo "IMAGE_TAG=$(echo ${GITHUB_REF#refs/*/} | tr / _)" |
| 89 | + echo "IMAGE_TAG=$(echo ${GITHUB_REF#refs/*/} | tr / _)" >> $GITHUB_ENV |
| 90 | + else |
| 91 | + echo "OVERRIDE_TAG_NAME provided, using it for IMAGE_TAG..." |
| 92 | + echo "IMAGE_TAG=${{ inputs.OVERRIDE_TAG_NAME }}" |
| 93 | + echo "IMAGE_TAG=${{ inputs.OVERRIDE_TAG_NAME }}" >> $GITHUB_ENV |
| 94 | + fi |
| 95 | +
|
| 96 | + if [[ -z "${{ inputs.OVERRIDE_REPO_NAME }}" ]] |
| 97 | + then |
| 98 | + echo "No OVERRIDE_REPO_NAME input provided, defaulting to repo name..." |
| 99 | + echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}')" |
| 100 | + echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}')" >> $GITHUB_ENV |
| 101 | + else |
| 102 | + echo "OVERRIDE_REPO_NAME provided, using it for REPO_NAME..." |
| 103 | + echo "REPO_NAME=${{ inputs.OVERRIDE_REPO_NAME }}" |
| 104 | + echo "REPO_NAME=${{ inputs.OVERRIDE_REPO_NAME }}" >> $GITHUB_ENV |
| 105 | + fi |
| 106 | +
|
| 107 | + # https://github.com/docker/login-action#quayio |
| 108 | + - name: Login to Quay.io |
| 109 | + uses: docker/login-action@v3 |
| 110 | + with: |
| 111 | + registry: quay.io |
| 112 | + username: ${{ secrets.QUAY_USERNAME }} |
| 113 | + password: ${{ secrets.QUAY_ROBOT_TOKEN }} |
| 114 | + |
| 115 | + # https://github.com/docker/login-action#aws-public-elastic-container-registry-ecr |
| 116 | + - name: Login to ECR |
| 117 | + uses: docker/login-action@v3 |
| 118 | + with: |
| 119 | + registry: ${{ inputs.AWS_ECR_REGISTRY }} |
| 120 | + username: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }} |
| 121 | + password: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }} |
| 122 | + env: |
| 123 | + AWS_REGION: ${{ inputs.AWS_REGION }} |
| 124 | + |
| 125 | + - name: Extract metadata |
| 126 | + id: meta |
| 127 | + uses: docker/metadata-action@v5 |
| 128 | + with: |
| 129 | + images: | |
| 130 | + quay.io/cdis/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} |
| 131 | + ${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} |
| 132 | +
|
| 133 | + - name: Set image repositories |
| 134 | + id: set-repos |
| 135 | + run: | |
| 136 | + if [ "${{ inputs.QUAY_ONLY }}" = "true" ]; then |
| 137 | + echo "repos=quay.io/cdis/${{ env.REPO_NAME }}" >> $GITHUB_ENV |
| 138 | + else |
| 139 | + echo "repos=quay.io/cdis/${{ env.REPO_NAME }},${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}" >> $GITHUB_ENV |
| 140 | + fi |
| 141 | +
|
| 142 | + - name: Build and push by digest |
| 143 | + id: build |
| 144 | + uses: docker/build-push-action@v6 |
| 145 | + # You may get ECR-push errors when first adding the workflow to a github repo. |
| 146 | + # If so, run the following in dev/qa to create the ECR repository: |
| 147 | + # qaplanetv1@cdistest_dev_admin:~$ aws ecr create-repository --repository-name "gen3/<repo name>" --image-scanning-configuration scanOnPush=true |
| 148 | + with: |
| 149 | + context: ${{ inputs.DOCKERFILE_BUILD_CONTEXT }} |
| 150 | + file: ${{ inputs.DOCKERFILE_LOCATION }} |
| 151 | + labels: ${{ steps.meta.outputs.labels }} |
| 152 | + cache-from: type=gha |
| 153 | + cache-to: type=gha,mode=max |
| 154 | + platforms: ${{ matrix.platform }} |
| 155 | + outputs: type=image,"name=${{ env.repos }}",push-by-digest=true,name-canonical=true,push=true |
| 156 | + |
| 157 | + - name: Export digest |
| 158 | + run: | |
| 159 | + mkdir -p ${{ runner.temp }}/digests |
| 160 | + digest="${{ steps.build.outputs.digest }}" |
| 161 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 162 | +
|
| 163 | + - name: Upload digest |
| 164 | + uses: actions/upload-artifact@v4 |
| 165 | + with: |
| 166 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 167 | + path: ${{ runner.temp }}/digests/* |
| 168 | + if-no-files-found: error |
| 169 | + retention-days: 1 |
| 170 | + merge: |
| 171 | + runs-on: ubuntu-latest |
| 172 | + needs: |
| 173 | + - build |
| 174 | + steps: |
| 175 | + - name: Download digests |
| 176 | + uses: actions/download-artifact@v4 |
| 177 | + with: |
| 178 | + path: ${{ runner.temp }}/digests |
| 179 | + pattern: digests-* |
| 180 | + merge-multiple: true |
| 181 | + |
| 182 | + # https://github.com/docker/login-action#quayio |
| 183 | + - name: Login to Quay.io |
| 184 | + uses: docker/login-action@v3 |
| 185 | + with: |
| 186 | + registry: quay.io |
| 187 | + username: ${{ secrets.QUAY_USERNAME }} |
| 188 | + password: ${{ secrets.QUAY_ROBOT_TOKEN }} |
| 189 | + |
| 190 | + # https://github.com/docker/login-action#aws-public-elastic-container-registry-ecr |
| 191 | + - name: Login to ECR |
| 192 | + uses: docker/login-action@v3 |
| 193 | + with: |
| 194 | + registry: ${{ inputs.AWS_ECR_REGISTRY }} |
| 195 | + username: ${{ secrets.ECR_AWS_ACCESS_KEY_ID }} |
| 196 | + password: ${{ secrets.ECR_AWS_SECRET_ACCESS_KEY }} |
| 197 | + env: |
| 198 | + AWS_REGION: ${{ inputs.AWS_REGION }} |
| 199 | + |
| 200 | + - name: Set Variables |
| 201 | + shell: bash |
| 202 | + run: | |
| 203 | + echo "OVERRIDE_REPO_NAME = ${{ inputs.OVERRIDE_REPO_NAME }}" |
| 204 | + echo "OVERRIDE_TAG_NAME = ${{ inputs.OVERRIDE_TAG_NAME }}" |
| 205 | +
|
| 206 | + if [[ -z "${{ inputs.OVERRIDE_TAG_NAME }}" ]] |
| 207 | + then |
| 208 | + echo "No OVERRIDE_TAG_NAME input provided, defaulting to current branch/tag name..." |
| 209 | + echo "IMAGE_TAG=$(echo ${GITHUB_REF#refs/*/} | tr / _)" |
| 210 | + echo "IMAGE_TAG=$(echo ${GITHUB_REF#refs/*/} | tr / _)" >> $GITHUB_ENV |
| 211 | + else |
| 212 | + echo "OVERRIDE_TAG_NAME provided, using it for IMAGE_TAG..." |
| 213 | + echo "IMAGE_TAG=${{ inputs.OVERRIDE_TAG_NAME }}" |
| 214 | + echo "IMAGE_TAG=${{ inputs.OVERRIDE_TAG_NAME }}" >> $GITHUB_ENV |
| 215 | + fi |
| 216 | +
|
| 217 | + if [[ -z "${{ inputs.OVERRIDE_REPO_NAME }}" ]] |
| 218 | + then |
| 219 | + echo "No OVERRIDE_REPO_NAME input provided, defaulting to repo name..." |
| 220 | + echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}')" |
| 221 | + echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}')" >> $GITHUB_ENV |
| 222 | + else |
| 223 | + echo "OVERRIDE_REPO_NAME provided, using it for REPO_NAME..." |
| 224 | + echo "REPO_NAME=${{ inputs.OVERRIDE_REPO_NAME }}" |
| 225 | + echo "REPO_NAME=${{ inputs.OVERRIDE_REPO_NAME }}" >> $GITHUB_ENV |
| 226 | + fi |
| 227 | +
|
| 228 | + - name: Set up Docker Buildx |
| 229 | + uses: docker/setup-buildx-action@v3 |
| 230 | + |
| 231 | + - name: Docker meta |
| 232 | + id: meta |
| 233 | + uses: docker/metadata-action@v5 |
| 234 | + with: |
| 235 | + images: | |
| 236 | + ${{ inputs.USE_QUAY_ONLY != 'true' && format('{0}/gen3/{1}', inputs.AWS_ECR_REGISTRY, env.REPO_NAME) || '' }} |
| 237 | + quay.io/cdis/${{ env.REPO_NAME }} |
| 238 | + tags: | |
| 239 | + type=ref,event=branch |
| 240 | + type=ref,event=pr |
| 241 | + type=semver,pattern={{version}} |
| 242 | + type=semver,pattern={{major}}.{{minor}} |
| 243 | +
|
| 244 | + - name: Create manifest list and push to registries |
| 245 | + working-directory: ${{ runner.temp }}/digests |
| 246 | + run: | |
| 247 | + # Push to Quay |
| 248 | + docker buildx imagetools create \ |
| 249 | + -t quay.io/cdis/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} \ |
| 250 | + $(printf 'quay.io/cdis/${{ env.REPO_NAME }}@sha256:%s ' *) |
| 251 | +
|
| 252 | + # Conditionally push to ECR |
| 253 | + if [ "${{ inputs.USE_QUAY_ONLY }}" != "true" ]; then |
| 254 | + docker buildx imagetools create \ |
| 255 | + -t ${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} \ |
| 256 | + $(printf '${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}@sha256:%s ' *) |
| 257 | + fi |
| 258 | +
|
| 259 | + - name: Inspect image |
| 260 | + run: | |
| 261 | + docker buildx imagetools inspect \ |
| 262 | + quay.io/cdis/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} |
| 263 | +
|
| 264 | + - name: Inspect image (ECR) |
| 265 | + if: ${{ !inputs.USE_QUAY_ONLY }} |
| 266 | + run: | |
| 267 | + docker buildx imagetools inspect \ |
| 268 | + ${{ inputs.AWS_ECR_REGISTRY }}/gen3/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} |
0 commit comments