feat: add gh wf #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: 🐳 Reusable Docker Build | ||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| version: | ||
| required: true | ||
| type: string | ||
| registry: | ||
| required: false | ||
| type: string | ||
| default: 'docker.io' | ||
| image_name: | ||
| required: false | ||
| type: string | ||
| default: "" | ||
| push: | ||
| required: false | ||
| type: boolean | ||
| default: true | ||
| secrets: | ||
| DOCKERHUB_USERNAME: | ||
| required: true | ||
| DOCKERHUB_TOKEN: | ||
| required: true | ||
| permissions: | ||
| contents: read | ||
| packages: write | ||
| security-events: write | ||
| env: | ||
| ORGANISATION: ${{ github.repository_owner }} | ||
| jobs: | ||
| build: | ||
| name: Build Docker Image | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| tags: ${{ steps.meta.outputs.tags }} | ||
| primary_tag: ${{ fromJSON(steps.meta.outputs.json).tags[0] }} | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - name: Resolve Image Name | ||
| id: prep | ||
| run: | | ||
| if [ -n "${{ inputs.image_name }}" ]; then | ||
| FULL_NAME="${{ inputs.image_name }}" | ||
| else | ||
| FULL_NAME="${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}" | ||
| fi | ||
| # Force lowercase for Docker Hub compatibility | ||
| FINAL_NAME=$(echo "$FULL_NAME" | tr '[:upper:]' '[:lower:]') | ||
| REPO_ONLY=$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]') | ||
| echo "image_full_name=$FINAL_NAME" >> $GITHUB_OUTPUT | ||
| echo "repo_only=$REPO_ONLY" >> $GITHUB_OUTPUT | ||
| echo "ℹ️ Resolved Image Name: $FINAL_NAME" | ||
| - name: Log in to ${{ inputs.registry }} | ||
| if: github.event_name != 'pull_request' | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| registry: ${{ inputs.registry }} | ||
| username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
| password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
| - name: Set up QEMU | ||
| uses: docker/setup-qemu-action@v3 | ||
| with: | ||
| platforms: linux/amd64,linux/arm64 | ||
| - name: Set up Docker Buildx | ||
| uses: docker/setup-buildx-action@v3 | ||
| - name: Generate metadata | ||
| id: meta | ||
| uses: docker/metadata-action@v5 | ||
| with: | ||
| images: ${{ inputs.registry }}/${{ steps.prep.outputs.image_full_name }} | ||
| tags: | | ||
| type=semver,pattern={{version}} | ||
| type=semver,pattern={{major}}.{{minor}} | ||
| type=ref,event=branch | ||
| type=raw,value=latest,enable={{is_default_branch}} | ||
| type=raw,value=${{ inputs.version }},enable=${{ inputs.version != '' }} | ||
| type=sha,prefix=sha- | ||
| labels: | | ||
| org.opencontainers.image.title=${{ github.event.repository.name }} | ||
| org.opencontainers.image.description=${{ github.event.repository.description }} | ||
| org.opencontainers.image.vendor=${{ env.ORGANISATION }} | ||
| org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} | ||
| org.opencontainers.image.licenses=MIT | ||
| - name: Build and push Docker image | ||
| uses: docker/build-push-action@v6 | ||
| with: | ||
| context: . | ||
| # Multi-arch support | ||
| platforms: linux/amd64,linux/arm64 | ||
| push: true | ||
| provenance: false | ||
| sbom: true | ||
| tags: ${{ steps.meta.outputs.tags }} | ||
| labels: ${{ steps.meta.outputs.labels }} | ||
| # GitHub Actions Cache (extremely fast) | ||
| cache-from: type=gha | ||
| cache-to: type=gha,mode=max | ||
| - name: Generate image summary | ||
| if: github.event_name != 'pull_request' | ||
| run: | | ||
| echo "## Docker Image Published" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "**Registry:** \`${{ inputs.registry }}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "**Image:** \`${{ steps.prep.outputs.image_full_name }}\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "### Tags" >> $GITHUB_STEP_SUMMARY | ||
| echo "\`\`\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY | ||
| echo "\`\`\`" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "### Pull Command" >> $GITHUB_STEP_SUMMARY | ||
| echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY | ||
| echo "docker pull ${{ inputs.registry }}/${{ steps.prep.outputs.image_full_name }}:latest" >> $GITHUB_STEP_SUMMARY | ||
| echo "\`\`\`" >> $GITHUB_STEP_SUMMARY | ||
| - name: Cleanup old Docker Hub tags | ||
| uses: data-science-at-scale/delete-docker-hub-tag@v0.6.1 | ||
| with: | ||
| repository: ${{ env.IMAGE_NAME }} | ||
| repository: ${{ steps.prep.outputs.repo_only }} | ||
| # For Docker Hub, use your username and a Personal Access Token | ||
| username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
| password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
| # Regex for tags to delete (e.g., all except latest and semver) | ||
| tag: '^sha-.*$' # Example: delete all git-sha tags | ||