|
| 1 | +name: Publish Docker image |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + push: |
| 7 | + branches: ["main"] |
| 8 | + paths: |
| 9 | + - 'Dockerfile.prebuilt' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +env: |
| 13 | + REGISTRY_IMAGE: middlewareeng/oss-base |
| 14 | + AWS_REGION: ap-south-1 |
| 15 | + RUNNER_SUBNET: subnet-0551adcc31939391b |
| 16 | + RUNNER_SG: sg-0805fa13c0f7f6e2c |
| 17 | + |
| 18 | +jobs: |
| 19 | + |
| 20 | + start-runner-arm: |
| 21 | + name: Start self-hosted ARM EC2 runner |
| 22 | + runs-on: ubuntu-latest |
| 23 | + outputs: |
| 24 | + label: ${{ steps.start-ec2-runner.outputs.label }} |
| 25 | + ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }} |
| 26 | + steps: |
| 27 | + - name: Configure AWS credentials |
| 28 | + uses: aws-actions/configure-aws-credentials@v1 |
| 29 | + with: |
| 30 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 31 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 32 | + aws-region: ${{ env.AWS_REGION }} |
| 33 | + |
| 34 | + - name: Start EC2 runner arm |
| 35 | + id: start-ec2-runner |
| 36 | + uses: machulav/ec2-github-runner@v2 |
| 37 | + with: |
| 38 | + mode: start |
| 39 | + github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} |
| 40 | + ec2-image-id: ami-07c3226e40486ca7f |
| 41 | + ec2-instance-type: t4g.medium |
| 42 | + subnet-id: ${{ env.RUNNER_SUBNET }} |
| 43 | + security-group-id: ${{ env.RUNNER_SG }} |
| 44 | + aws-resource-tags: > |
| 45 | + [ |
| 46 | + {"Key": "Name", "Value": "ec2-github-runner-arm64"}, |
| 47 | + {"Key": "GitHubRepository", "Value": "${{ github.repository }}"} |
| 48 | + ] |
| 49 | +
|
| 50 | + build: |
| 51 | + name: Build and Push Docker Image |
| 52 | + needs: start-runner-arm |
| 53 | + runs-on: ${{ matrix.runner }} |
| 54 | + strategy: |
| 55 | + matrix: |
| 56 | + include: |
| 57 | + - platform: linux/amd64 |
| 58 | + runner: ubuntu-latest |
| 59 | + - platform: linux/arm64 |
| 60 | + runner: ${{ needs.start-runner-arm.outputs.label }} |
| 61 | + |
| 62 | + steps: |
| 63 | + - name: Prepare |
| 64 | + run: | |
| 65 | + platform=${{ matrix.platform }} |
| 66 | + BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') |
| 67 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 68 | + echo "BUILD_DATE=${BUILD_DATE}" >> $GITHUB_ENV |
| 69 | +
|
| 70 | + - name: Checkout |
| 71 | + uses: actions/checkout@v4 |
| 72 | + with: |
| 73 | + ref: ${{ github.ref }} |
| 74 | + |
| 75 | + - name: Extract metadata (tags, labels) for Docker |
| 76 | + id: meta |
| 77 | + uses: docker/metadata-action@v5 |
| 78 | + with: |
| 79 | + images: ${{ env.REGISTRY_IMAGE }} |
| 80 | + labels: | |
| 81 | + org.opencontainers.image.source=${{ github.event.repository.clone_url }} |
| 82 | + org.opencontainers.image.created=${{ steps.meta.outputs.created }} |
| 83 | + org.opencontainers.image.revision=${{ github.sha }} |
| 84 | + org.opencontainers.image.licenses=${{ fromJson('["MIT"]') }} |
| 85 | + flavor: | |
| 86 | + latest=auto |
| 87 | +
|
| 88 | + - name: Set up QEMU |
| 89 | + uses: docker/setup-qemu-action@v3 |
| 90 | + |
| 91 | + - name: Set up Docker Buildx |
| 92 | + uses: docker/setup-buildx-action@v3 |
| 93 | + |
| 94 | + - name: Login to Docker Hub |
| 95 | + uses: docker/login-action@v3 |
| 96 | + with: |
| 97 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 98 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 99 | + |
| 100 | + - name: Print build metadata |
| 101 | + run: echo "${{ toJson(steps.meta.outputs) }}" |
| 102 | + |
| 103 | + - name: Build and push Docker image by digest |
| 104 | + id: build |
| 105 | + uses: docker/build-push-action@v5 |
| 106 | + with: |
| 107 | + context: . |
| 108 | + file: ./Dockerfile.prebuilt |
| 109 | + platforms: ${{ matrix.platform }} |
| 110 | + build-args: | |
| 111 | + BUILD_DATE=${{ env.BUILD_DATE }} |
| 112 | + MERGE_COMMIT_SHA=${{ github.sha }} |
| 113 | + tags: ${{ github.ref == 'refs/heads/main' && format('middlewareeng/oss-base:latest') || steps.meta.outputs.tags }} |
| 114 | + labels: ${{ steps.meta.outputs.labels }} |
| 115 | + outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=false,name-canonical=true,push=true |
| 116 | + |
| 117 | + - name: Print build output |
| 118 | + run: echo "${{ toJson(steps.build.outputs) }}" |
| 119 | + |
| 120 | + - name: Export digest |
| 121 | + run: | |
| 122 | + mkdir -p /tmp/digests |
| 123 | + digest="${{ steps.build.outputs.digest }}" |
| 124 | + touch "/tmp/digests/${digest#sha256:}" |
| 125 | +
|
| 126 | +
|
| 127 | + - name: Upload digest |
| 128 | + uses: actions/upload-artifact@v4 |
| 129 | + with: |
| 130 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 131 | + path: /tmp/digests/* |
| 132 | + if-no-files-found: error |
| 133 | + retention-days: 1 |
| 134 | + |
| 135 | + - name: delete local temp digest |
| 136 | + run: | |
| 137 | + rm -rf /tmp/digests |
| 138 | + merge: |
| 139 | + runs-on: ubuntu-latest |
| 140 | + needs: |
| 141 | + - build |
| 142 | + steps: |
| 143 | + - name: Download digests |
| 144 | + uses: actions/download-artifact@v4 |
| 145 | + with: |
| 146 | + path: /tmp/digests |
| 147 | + pattern: digests-* |
| 148 | + merge-multiple: true |
| 149 | + |
| 150 | + - name: Set up Docker Buildx |
| 151 | + uses: docker/setup-buildx-action@v3 |
| 152 | + |
| 153 | + - name: Docker meta |
| 154 | + id: meta |
| 155 | + uses: docker/metadata-action@v5 |
| 156 | + with: |
| 157 | + images: ${{ env.REGISTRY_IMAGE }} |
| 158 | + |
| 159 | + - name: Login to Docker Hub |
| 160 | + uses: docker/login-action@v3 |
| 161 | + with: |
| 162 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 163 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 164 | + |
| 165 | + - name: Create tag for new image |
| 166 | + id: tag |
| 167 | + run: | |
| 168 | + if [ "${{ github.ref }}" == "refs/heads/main" ]; then |
| 169 | + echo "DOCKER_TAG=-t middlewareeng/oss-base:latest" |
| 170 | + echo "DOCKER_TAG=-t middlewareeng/oss-base:latest" >> $GITHUB_ENV |
| 171 | + else |
| 172 | + echo "AUTOMATIC TAG" |
| 173 | + echo "DOCKER_TAG=$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON")" >> $GITHUB_ENV |
| 174 | + fi |
| 175 | +
|
| 176 | + - name: Create manifest list and push |
| 177 | + working-directory: /tmp/digests |
| 178 | + run: | |
| 179 | + docker buildx imagetools create ${{ env.DOCKER_TAG }} \ |
| 180 | + $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) |
| 181 | +
|
| 182 | + stop-runner-arm: |
| 183 | + name: Stop self-hosted EC2 arm runner |
| 184 | + needs: |
| 185 | + - start-runner-arm |
| 186 | + - build |
| 187 | + - merge |
| 188 | + runs-on: ubuntu-latest |
| 189 | + if: ${{ always() }} |
| 190 | + steps: |
| 191 | + - name: Configure AWS credentials |
| 192 | + uses: aws-actions/configure-aws-credentials@v1 |
| 193 | + with: |
| 194 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 195 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 196 | + aws-region: ${{ env.AWS_REGION }} |
| 197 | + - name: Stop EC2 runner |
| 198 | + uses: machulav/ec2-github-runner@v2 |
| 199 | + with: |
| 200 | + mode: stop |
| 201 | + github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} |
| 202 | + label: ${{ needs.start-runner-arm.outputs.label }} |
| 203 | + ec2-instance-id: ${{ needs.start-runner-arm.outputs.ec2-instance-id }} |
0 commit comments