|
| 1 | +name: 🐳 Reusable Docker Build |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + registry: |
| 10 | + required: false |
| 11 | + type: string |
| 12 | + default: 'docker.io' |
| 13 | + image_name: |
| 14 | + required: false |
| 15 | + type: string |
| 16 | + default: "" |
| 17 | + push: |
| 18 | + required: false |
| 19 | + type: boolean |
| 20 | + default: true |
| 21 | + secrets: |
| 22 | + DOCKERHUB_USERNAME: |
| 23 | + required: true |
| 24 | + DOCKERHUB_TOKEN: |
| 25 | + required: true |
| 26 | + |
| 27 | +permissions: |
| 28 | + contents: read |
| 29 | + packages: write |
| 30 | + security-events: write |
| 31 | + |
| 32 | +env: |
| 33 | + ORGANISATION: ${{ github.repository_owner }} |
| 34 | + |
| 35 | + |
| 36 | +jobs: |
| 37 | + build: |
| 38 | + name: Build Docker Image |
| 39 | + runs-on: ubuntu-latest |
| 40 | + outputs: |
| 41 | + tags: ${{ steps.meta.outputs.tags }} |
| 42 | + primary_tag: ${{ fromJSON(steps.meta.outputs.json).tags[0] }} |
| 43 | + |
| 44 | + |
| 45 | + steps: |
| 46 | + - name: Checkout code |
| 47 | + uses: actions/checkout@v4 |
| 48 | + |
| 49 | + - name: Resolve Image Name |
| 50 | + id: prep |
| 51 | + run: | |
| 52 | + if [ -n "${{ inputs.image_name }}" ]; then |
| 53 | + FULL_NAME="${{ inputs.image_name }}" |
| 54 | + else |
| 55 | + FULL_NAME="${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}" |
| 56 | + fi |
| 57 | +
|
| 58 | + # Force lowercase for Docker Hub compatibility |
| 59 | + FINAL_NAME=$(echo "$FULL_NAME" | tr '[:upper:]' '[:lower:]') |
| 60 | + REPO_ONLY=$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]') |
| 61 | + |
| 62 | + echo "image_full_name=$FINAL_NAME" >> $GITHUB_OUTPUT |
| 63 | + echo "repo_only=$REPO_ONLY" >> $GITHUB_OUTPUT |
| 64 | +
|
| 65 | + echo "ℹ️ Resolved Image Name: $FINAL_NAME" |
| 66 | +
|
| 67 | + - name: Log in to ${{ inputs.registry }} |
| 68 | + if: github.event_name != 'pull_request' |
| 69 | + uses: docker/login-action@v3 |
| 70 | + with: |
| 71 | + registry: ${{ inputs.registry }} |
| 72 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 73 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 74 | + |
| 75 | + - name: Set up QEMU |
| 76 | + uses: docker/setup-qemu-action@v3 |
| 77 | + with: |
| 78 | + platforms: linux/amd64,linux/arm64 |
| 79 | + |
| 80 | + - name: Set up Docker Buildx |
| 81 | + uses: docker/setup-buildx-action@v3 |
| 82 | + |
| 83 | + - name: Generate metadata |
| 84 | + id: meta |
| 85 | + uses: docker/metadata-action@v5 |
| 86 | + with: |
| 87 | + images: ${{ inputs.registry }}/${{ steps.prep.outputs.image_full_name }} |
| 88 | + tags: | |
| 89 | + type=semver,pattern={{version}} |
| 90 | + type=semver,pattern={{major}}.{{minor}} |
| 91 | + type=ref,event=branch |
| 92 | + type=raw,value=latest,enable={{is_default_branch}} |
| 93 | + type=raw,value=${{ inputs.version }},enable=${{ inputs.version != '' }} |
| 94 | + type=sha,prefix=sha- |
| 95 | +
|
| 96 | + labels: | |
| 97 | + org.opencontainers.image.title=${{ github.event.repository.name }} |
| 98 | + org.opencontainers.image.description=${{ github.event.repository.description }} |
| 99 | + org.opencontainers.image.vendor=${{ env.ORGANISATION }} |
| 100 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 101 | + org.opencontainers.image.licenses=MIT |
| 102 | +
|
| 103 | + - name: Build and push Docker image |
| 104 | + uses: docker/build-push-action@v6 |
| 105 | + with: |
| 106 | + context: . |
| 107 | + # Multi-arch support |
| 108 | + platforms: linux/amd64,linux/arm64 |
| 109 | + push: true |
| 110 | + provenance: false |
| 111 | + sbom: true |
| 112 | + tags: ${{ steps.meta.outputs.tags }} |
| 113 | + labels: ${{ steps.meta.outputs.labels }} |
| 114 | + # GitHub Actions Cache (extremely fast) |
| 115 | + cache-from: type=gha |
| 116 | + cache-to: type=gha,mode=max |
| 117 | + |
| 118 | + - name: Generate image summary |
| 119 | + if: github.event_name != 'pull_request' |
| 120 | + run: | |
| 121 | + echo "## Docker Image Published" >> $GITHUB_STEP_SUMMARY |
| 122 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 123 | + echo "**Registry:** \`${{ inputs.registry }}\`" >> $GITHUB_STEP_SUMMARY |
| 124 | + echo "**Image:** \`${{ steps.prep.outputs.image_full_name }}\`" >> $GITHUB_STEP_SUMMARY |
| 125 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 126 | + echo "### Tags" >> $GITHUB_STEP_SUMMARY |
| 127 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 128 | + echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY |
| 129 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 130 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 131 | + echo "### Pull Command" >> $GITHUB_STEP_SUMMARY |
| 132 | + echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY |
| 133 | + echo "docker pull ${{ inputs.registry }}/${{ steps.prep.outputs.image_full_name }}:latest" >> $GITHUB_STEP_SUMMARY |
| 134 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 135 | +
|
| 136 | + - name: Cleanup old Docker Hub tags |
| 137 | + uses: data-science-at-scale/delete-docker-hub-tag@v0.6.1 |
| 138 | + with: |
| 139 | + repository: ${{ env.IMAGE_NAME }} |
| 140 | + repository: ${{ steps.prep.outputs.repo_only }} |
| 141 | + # For Docker Hub, use your username and a Personal Access Token |
| 142 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 143 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 144 | + # Regex for tags to delete (e.g., all except latest and semver) |
| 145 | + tag: '^sha-.*$' # Example: delete all git-sha tags |
0 commit comments