|
| 1 | +name: Build and Push Docker Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + paths: |
| 7 | + - 'docker/**' |
| 8 | + - 'nginx/**' |
| 9 | + - '.github/workflows/docker-build-push.yml' |
| 10 | + tags: |
| 11 | + - 'v*' |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +env: |
| 15 | + REGISTRY: ghcr.io |
| 16 | + IMAGE_NAME: ${{ github.repository }} |
| 17 | + |
| 18 | +jobs: |
| 19 | + build-and-push: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + packages: write |
| 24 | + |
| 25 | + strategy: |
| 26 | + matrix: |
| 27 | + include: |
| 28 | + - dockerfile: docker/Dockerfile.migrate.nonroot |
| 29 | + image-name: migrate-nonroot |
| 30 | + context: . |
| 31 | + - dockerfile: docker/Dockerfile.openrestry |
| 32 | + image-name: openresty-base |
| 33 | + context: . |
| 34 | + - dockerfile: docker/Dockerfile.openresty.nonroot |
| 35 | + image-name: openresty-nonroot |
| 36 | + context: . |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: Maximize build space |
| 40 | + uses: easimon/maximize-build-space@master |
| 41 | + with: |
| 42 | + root-reserve-mb: 1024 |
| 43 | + swap-size-mb: 2048 |
| 44 | + remove-dotnet: 'true' |
| 45 | + remove-android: 'true' |
| 46 | + remove-haskell: 'true' |
| 47 | + remove-codeql: 'true' |
| 48 | + remove-docker-images: 'true' |
| 49 | + |
| 50 | + - name: Checkout repository |
| 51 | + uses: actions/checkout@v4 |
| 52 | + |
| 53 | + - name: Set up Docker Buildx |
| 54 | + id: buildx |
| 55 | + uses: docker/setup-buildx-action@v3 |
| 56 | + with: |
| 57 | + driver-opts: | |
| 58 | + image=moby/buildkit:buildx-stable-1 |
| 59 | + network=host |
| 60 | +
|
| 61 | + - name: Log in to Container Registry |
| 62 | + uses: docker/login-action@v3 |
| 63 | + with: |
| 64 | + registry: ${{ env.REGISTRY }} |
| 65 | + username: ${{ github.actor }} |
| 66 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 67 | + |
| 68 | + - name: Extract metadata |
| 69 | + id: meta |
| 70 | + uses: docker/metadata-action@v5 |
| 71 | + with: |
| 72 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/${{ matrix.image-name }} |
| 73 | + tags: | |
| 74 | + type=ref,event=tag |
| 75 | + type=raw,value=latest,enable={{is_default_branch}} |
| 76 | +
|
| 77 | + - name: Build and push Docker image |
| 78 | + uses: docker/build-push-action@v5 |
| 79 | + with: |
| 80 | + context: ${{ matrix.context }} |
| 81 | + file: ${{ matrix.dockerfile }} |
| 82 | + push: true |
| 83 | + tags: ${{ steps.meta.outputs.tags }} |
| 84 | + labels: ${{ steps.meta.outputs.labels }} |
| 85 | + platforms: linux/amd64 |
| 86 | + cache-from: type=gha |
| 87 | + cache-to: type=gha,mode=max |
| 88 | + build-args: | |
| 89 | + BUILDKIT_INLINE_CACHE=1 |
| 90 | + builder: ${{ steps.buildx.outputs.name }} |
| 91 | + shm-size: 2g |
| 92 | + |
| 93 | + build-summary: |
| 94 | + needs: build-and-push |
| 95 | + runs-on: ubuntu-latest |
| 96 | + if: always() |
| 97 | + steps: |
| 98 | + - name: Build Summary |
| 99 | + run: | |
| 100 | + echo "## Docker Build Summary" >> $GITHUB_STEP_SUMMARY |
| 101 | + echo "Built and pushed the following images:" >> $GITHUB_STEP_SUMMARY |
| 102 | + echo "- migrate-nonroot" >> $GITHUB_STEP_SUMMARY |
| 103 | + echo "- openresty-base" >> $GITHUB_STEP_SUMMARY |
| 104 | + echo "- openresty-nonroot" >> $GITHUB_STEP_SUMMARY |
| 105 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 106 | + echo "Images are available at: ghcr.io/${{ github.repository }}" >> $GITHUB_STEP_SUMMARY |
0 commit comments