|
| 1 | +name: 'Build and Push Docker Images' |
| 2 | +description: 'Builds multi-arch Docker images with caching and SemVer tagging using official Docker actions' |
| 3 | + |
| 4 | +inputs: |
| 5 | + dockerfile: |
| 6 | + description: 'Path to Dockerfile (relative to repository root)' |
| 7 | + required: true |
| 8 | + image-name: |
| 9 | + description: 'Docker image name (without registry prefix)' |
| 10 | + required: true |
| 11 | + registry: |
| 12 | + description: 'Docker registry (docker.io or ghcr.io)' |
| 13 | + required: false |
| 14 | + default: 'docker.io' |
| 15 | + username: |
| 16 | + description: 'Docker registry username' |
| 17 | + required: true |
| 18 | + password: |
| 19 | + description: 'Docker registry password/token' |
| 20 | + required: true |
| 21 | + platforms: |
| 22 | + description: 'Target platforms for multi-arch build' |
| 23 | + required: false |
| 24 | + default: 'linux/amd64,linux/arm64' |
| 25 | + push: |
| 26 | + description: 'Whether to push the image (true/false)' |
| 27 | + required: false |
| 28 | + default: 'false' |
| 29 | + build-args: |
| 30 | + description: 'Build arguments as KEY=VALUE pairs (one per line)' |
| 31 | + required: false |
| 32 | + default: '' |
| 33 | + |
| 34 | + cache-scope: |
| 35 | + description: 'Cache scope for build cache' |
| 36 | + required: false |
| 37 | + default: 'default' |
| 38 | + |
| 39 | +outputs: |
| 40 | + image-id: |
| 41 | + description: 'Image ID of the built image' |
| 42 | + value: ${{ steps.build.outputs.imageid }} |
| 43 | + digest: |
| 44 | + description: 'Image digest of the built image' |
| 45 | + value: ${{ steps.build.outputs.digest }} |
| 46 | + metadata: |
| 47 | + description: 'Build result metadata' |
| 48 | + value: ${{ steps.build.outputs.metadata }} |
| 49 | + |
| 50 | +runs: |
| 51 | + using: 'composite' |
| 52 | + steps: |
| 53 | + - name: Set up QEMU |
| 54 | + uses: docker/setup-qemu-action@v3 |
| 55 | + with: |
| 56 | + platforms: all |
| 57 | + |
| 58 | + - name: Set up Docker Buildx |
| 59 | + uses: docker/setup-buildx-action@v3 |
| 60 | + with: |
| 61 | + driver-opts: | |
| 62 | + network=host |
| 63 | +
|
| 64 | + - name: Log in to Docker Registry |
| 65 | + if: inputs.push == 'true' |
| 66 | + uses: docker/login-action@v3 |
| 67 | + with: |
| 68 | + registry: ${{ inputs.registry == 'docker.io' && '' || inputs.registry }} |
| 69 | + username: ${{ inputs.username }} |
| 70 | + password: ${{ inputs.password }} |
| 71 | + |
| 72 | + - name: Extract metadata |
| 73 | + id: meta |
| 74 | + uses: docker/metadata-action@v5 |
| 75 | + with: |
| 76 | + images: ${{ inputs.image-name }} |
| 77 | + tags: | |
| 78 | + # For releases: apply SemVer tags and latest (excludes pre-releases automatically) |
| 79 | + type=semver,pattern={{version}} |
| 80 | + type=semver,pattern={{major}}.{{minor}} |
| 81 | + type=semver,pattern={{major}} |
| 82 | + type=semver,pattern=latest |
| 83 | + # For branches: branch name as tag |
| 84 | + type=ref,event=branch |
| 85 | + # For PRs: pr-<number> |
| 86 | + type=ref,event=pr |
| 87 | + # SHA for unique identification |
| 88 | + type=sha,prefix=sha-,format=short |
| 89 | + labels: | |
| 90 | + org.opencontainers.image.title=${{ inputs.image-name }} |
| 91 | + org.opencontainers.image.description=PgWatch - PostgreSQL monitoring solution |
| 92 | + org.opencontainers.image.vendor=Cybertec PostgreSQL International GmbH |
| 93 | + org.opencontainers.image.licenses=BSD-3-Clause |
| 94 | +
|
| 95 | + - name: Build and push Docker image |
| 96 | + id: build |
| 97 | + uses: docker/build-push-action@v5 |
| 98 | + with: |
| 99 | + context: . |
| 100 | + file: ${{ inputs.dockerfile }} |
| 101 | + platforms: ${{ inputs.platforms }} |
| 102 | + push: ${{ inputs.push == 'true' }} |
| 103 | + tags: ${{ steps.meta.outputs.tags }} |
| 104 | + labels: ${{ steps.meta.outputs.labels }} |
| 105 | + build-args: ${{ inputs.build-args }} |
| 106 | + cache-from: type=gha,scope=${{ inputs.cache-scope }} |
| 107 | + cache-to: type=gha,mode=max,scope=${{ inputs.cache-scope }} |
| 108 | + provenance: false |
| 109 | + sbom: false |
| 110 | + |
| 111 | + - name: Output image details |
| 112 | + shell: bash |
| 113 | + run: | |
| 114 | + echo "🐳 Built image: ${{ inputs.image-name }}" |
| 115 | + echo "📋 Tags:" |
| 116 | + echo "${{ steps.meta.outputs.tags }}" | sed 's/^/ - /' |
| 117 | + echo "🔍 Digest: ${{ steps.build.outputs.digest }}" |
| 118 | + echo "🆔 Image ID: ${{ steps.build.outputs.imageid }}" |
0 commit comments