Adding workflow for building docker images (PRs and release) #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: Build and Push Docker Images | |
| on: | |
| push: | |
| branches: | |
| - main | |
| tags: | |
| - 'v*.*.*' | |
| - '[0-9]+.[0-9]+.[0-9]+' | |
| pull_request: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| workflow_call: | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build-and-push: | |
| name: Build and Push Docker Image | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| id-token: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| submodules: recursive | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Verify registry login | |
| run: | | |
| echo "✓ Successfully authenticated to ${{ env.REGISTRY }}" | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| echo "ℹ️ PR build: Image will be built but not pushed to registry" | |
| else | |
| echo "ℹ️ Image will be built and pushed to registry" | |
| fi | |
| - name: Extract metadata (tags, labels) for Docker | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| # Tag with 'latest' on main branch | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| # Tag with version on release tags (e.g., v1.2.3 -> 1.2.3) | |
| type=semver,pattern={{version}} | |
| # Tag with major.minor on release tags (e.g., v1.2.3 -> 1.2) | |
| type=semver,pattern={{major}}.{{minor}} | |
| # Tag with major on release tags (e.g., v1.2.3 -> 1) | |
| type=semver,pattern={{major}} | |
| # Tag with PR number on pull requests | |
| type=ref,event=pr,prefix=pr- | |
| # Tag with branch name on branch pushes | |
| type=ref,event=branch | |
| # Tag with commit SHA for traceability | |
| type=sha,format=short | |
| labels: | | |
| org.opencontainers.image.title=Pulp Manager | |
| org.opencontainers.image.description=FastAPI-based orchestration and management for multiple Pulp 3 servers | |
| org.opencontainers.image.vendor=G-Research | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| platforms: linux/amd64 | |
| - name: Generate artifact attestation | |
| if: github.event_name != 'pull_request' | |
| uses: actions/attest-build-provenance@v1 | |
| with: | |
| subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| subject-digest: ${{ steps.meta.outputs.digest }} | |
| push-to-registry: true |