|
| 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: Checkout repository |
| 40 | + uses: actions/checkout@v4 |
| 41 | + |
| 42 | + - name: Set up Docker Buildx |
| 43 | + id: buildx |
| 44 | + uses: docker/setup-buildx-action@v3 |
| 45 | + with: |
| 46 | + driver-opts: | |
| 47 | + image=moby/buildkit:buildx-stable-1 |
| 48 | + network=host |
| 49 | +
|
| 50 | + - name: Log in to Container Registry |
| 51 | + uses: docker/login-action@v3 |
| 52 | + with: |
| 53 | + registry: ${{ env.REGISTRY }} |
| 54 | + username: ${{ github.actor }} |
| 55 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + |
| 57 | + - name: Extract metadata |
| 58 | + id: meta |
| 59 | + uses: docker/metadata-action@v5 |
| 60 | + with: |
| 61 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/${{ matrix.image-name }} |
| 62 | + tags: | |
| 63 | + type=ref,event=tag |
| 64 | + type=raw,value=latest,enable={{is_default_branch}} |
| 65 | +
|
| 66 | + - name: Build and push Docker image |
| 67 | + uses: docker/build-push-action@v5 |
| 68 | + with: |
| 69 | + context: ${{ matrix.context }} |
| 70 | + file: ${{ matrix.dockerfile }} |
| 71 | + push: true |
| 72 | + tags: ${{ steps.meta.outputs.tags }} |
| 73 | + labels: ${{ steps.meta.outputs.labels }} |
| 74 | + platforms: linux/amd64 |
| 75 | + cache-from: type=gha |
| 76 | + cache-to: type=gha,mode=max |
| 77 | + build-args: | |
| 78 | + BUILDKIT_INLINE_CACHE=1 |
| 79 | + builder: ${{ steps.buildx.outputs.name }} |
| 80 | + shm-size: 2g |
| 81 | + provenance: false |
| 82 | + sbom: false |
| 83 | + |
| 84 | + copy-bitnami-images: |
| 85 | + runs-on: ubuntu-latest |
| 86 | + permissions: |
| 87 | + contents: read |
| 88 | + packages: write |
| 89 | + |
| 90 | + strategy: |
| 91 | + matrix: |
| 92 | + include: |
| 93 | + - source-image: bitnamilegacy/postgresql-repmgr:17.6.0-debian-12-r2 |
| 94 | + target-name: postgresql-repmgr |
| 95 | + target-tag: 17.6.0-debian-12-r2 |
| 96 | + - source-image: bitnamilegacy/pgpool:4.6.3-debian-12-r0 |
| 97 | + target-name: pgpool |
| 98 | + target-tag: 4.6.3-debian-12-r0 |
| 99 | + |
| 100 | + steps: |
| 101 | + - name: Log in to Container Registry |
| 102 | + uses: docker/login-action@v3 |
| 103 | + with: |
| 104 | + registry: ${{ env.REGISTRY }} |
| 105 | + username: ${{ github.actor }} |
| 106 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 107 | + |
| 108 | + - name: Copy and push Bitnami image |
| 109 | + run: | |
| 110 | + # Pull the source image |
| 111 | + docker pull ${{ matrix.source-image }} |
| 112 | + |
| 113 | + # Tag it for our registry with specific version |
| 114 | + docker tag ${{ matrix.source-image }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/${{ matrix.target-name }}:${{ matrix.target-tag }} |
| 115 | + |
| 116 | + # Push to our registry |
| 117 | + docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/${{ matrix.target-name }}:${{ matrix.target-tag }} |
| 118 | +
|
| 119 | + build-summary: |
| 120 | + needs: [build-and-push, copy-bitnami-images] |
| 121 | + runs-on: ubuntu-latest |
| 122 | + if: always() |
| 123 | + steps: |
| 124 | + - name: Build Summary |
| 125 | + run: | |
| 126 | + echo "## Docker Build Summary" >> $GITHUB_STEP_SUMMARY |
| 127 | + echo "Built and pushed the following images:" >> $GITHUB_STEP_SUMMARY |
| 128 | + echo "- migrate-nonroot" >> $GITHUB_STEP_SUMMARY |
| 129 | + echo "- openresty-base" >> $GITHUB_STEP_SUMMARY |
| 130 | + echo "- openresty-nonroot" >> $GITHUB_STEP_SUMMARY |
| 131 | + echo "- postgresql-repmgr" >> $GITHUB_STEP_SUMMARY |
| 132 | + echo "- pgpool" >> $GITHUB_STEP_SUMMARY |
| 133 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 134 | + echo "Images are available at: ghcr.io/${{ github.repository }}" >> $GITHUB_STEP_SUMMARY |
| 135 | +
|
| 136 | + cleanup-untagged: |
| 137 | + needs: [build-and-push, copy-bitnami-images] |
| 138 | + runs-on: ubuntu-latest |
| 139 | + if: always() |
| 140 | + permissions: |
| 141 | + contents: read |
| 142 | + packages: write |
| 143 | + steps: |
| 144 | + - name: Delete untagged container images |
| 145 | + uses: actions/delete-package-versions@v5 |
| 146 | + with: |
| 147 | + package-name: '${{ github.event.repository.name }}/migrate-nonroot' |
| 148 | + package-type: 'container' |
| 149 | + min-versions-to-keep: 1 |
| 150 | + delete-only-untagged-versions: true |
| 151 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 152 | + |
| 153 | + - name: Delete untagged openresty-base images |
| 154 | + uses: actions/delete-package-versions@v5 |
| 155 | + with: |
| 156 | + package-name: '${{ github.event.repository.name }}/openresty-base' |
| 157 | + package-type: 'container' |
| 158 | + min-versions-to-keep: 1 |
| 159 | + delete-only-untagged-versions: true |
| 160 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 161 | + |
| 162 | + - name: Delete untagged openresty-nonroot images |
| 163 | + uses: actions/delete-package-versions@v5 |
| 164 | + with: |
| 165 | + package-name: '${{ github.event.repository.name }}/openresty-nonroot' |
| 166 | + package-type: 'container' |
| 167 | + min-versions-to-keep: 1 |
| 168 | + delete-only-untagged-versions: true |
| 169 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 170 | + |
| 171 | + - name: Delete untagged postgresql-repmgr images |
| 172 | + uses: actions/delete-package-versions@v5 |
| 173 | + with: |
| 174 | + package-name: '${{ github.event.repository.name }}/postgresql-repmgr' |
| 175 | + package-type: 'container' |
| 176 | + min-versions-to-keep: 1 |
| 177 | + delete-only-untagged-versions: true |
| 178 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 179 | + |
| 180 | + - name: Delete untagged pgpool images |
| 181 | + uses: actions/delete-package-versions@v5 |
| 182 | + with: |
| 183 | + package-name: '${{ github.event.repository.name }}/pgpool' |
| 184 | + package-type: 'container' |
| 185 | + min-versions-to-keep: 1 |
| 186 | + delete-only-untagged-versions: true |
| 187 | + token: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments