|
| 1 | +name: Build and Push Docker Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + tags: |
| 8 | + - 'v*.*.*' |
| 9 | + - '[0-9]+.[0-9]+.[0-9]+' |
| 10 | + pull_request: |
| 11 | + branches: |
| 12 | + - main |
| 13 | + workflow_dispatch: |
| 14 | + workflow_call: |
| 15 | + |
| 16 | +env: |
| 17 | + IMAGE_NAME: pulp-manager |
| 18 | + |
| 19 | +jobs: |
| 20 | + build-and-push: |
| 21 | + name: Build and Push Docker Image |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + packages: write |
| 26 | + id-token: write |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout code |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + submodules: recursive |
| 33 | + |
| 34 | + - name: Set up Docker Buildx |
| 35 | + uses: docker/setup-buildx-action@v3 |
| 36 | + |
| 37 | + - name: Determine registries to push to |
| 38 | + id: registries |
| 39 | + run: | |
| 40 | + # Always include ghcr.io |
| 41 | + registries="ghcr.io" |
| 42 | +
|
| 43 | + # Add docker.io and quay.io for release tags |
| 44 | + if [[ "${{ github.ref }}" == refs/tags/* ]]; then |
| 45 | + registries="ghcr.io docker.io quay.io" |
| 46 | + fi |
| 47 | +
|
| 48 | + echo "registries=${registries}" >> $GITHUB_OUTPUT |
| 49 | + echo "Will push to: ${registries}" |
| 50 | +
|
| 51 | + - name: Log in to GitHub Container Registry |
| 52 | + uses: docker/login-action@v3 |
| 53 | + with: |
| 54 | + registry: ghcr.io |
| 55 | + username: ${{ github.actor }} |
| 56 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 57 | + |
| 58 | + - name: Log in to Docker Hub |
| 59 | + if: startsWith(github.ref, 'refs/tags/') |
| 60 | + uses: docker/login-action@v3 |
| 61 | + with: |
| 62 | + registry: docker.io |
| 63 | + username: ${{ secrets.DOCKER_BOT_USERNAME }} |
| 64 | + password: ${{ secrets.DOCKER_BOT_PASSWORD }} |
| 65 | + |
| 66 | + - name: Log in to Quay.io |
| 67 | + if: startsWith(github.ref, 'refs/tags/') |
| 68 | + uses: docker/login-action@v3 |
| 69 | + with: |
| 70 | + registry: quay.io |
| 71 | + username: ${{ secrets.QUAY_BOT_USERNAME }} |
| 72 | + password: ${{ secrets.QUAY_BOT_PASSWORD }} |
| 73 | + |
| 74 | + - name: Determine tags |
| 75 | + id: tags |
| 76 | + run: | |
| 77 | + tags="" |
| 78 | +
|
| 79 | + if [[ "${{ github.ref }}" == refs/tags/* ]]; then |
| 80 | + # Release tag (e.g., v1.2.3) |
| 81 | + version="${{ github.ref_name }}" |
| 82 | + version="${version#v}" # Remove 'v' prefix |
| 83 | + major=$(echo $version | cut -d. -f1) |
| 84 | + minor=$(echo $version | cut -d. -f1-2) |
| 85 | + tags="${version} ${minor} ${major} latest" |
| 86 | + elif [ "${{ github.ref_name }}" == "main" ]; then |
| 87 | + # Main branch |
| 88 | + tags="main latest" |
| 89 | + elif [ "${{ github.event_name }}" == "pull_request" ]; then |
| 90 | + # PR |
| 91 | + tags="pr-${{ github.event.pull_request.number }}" |
| 92 | + else |
| 93 | + # Other branches |
| 94 | + tags="${{ github.ref_name }}" |
| 95 | + fi |
| 96 | +
|
| 97 | + # Add SHA tag for traceability |
| 98 | + sha_short=$(echo ${{ github.sha }} | cut -c1-7) |
| 99 | + tags="${tags} sha-${sha_short}" |
| 100 | +
|
| 101 | + echo "tags=${tags}" >> $GITHUB_OUTPUT |
| 102 | + echo "Will use tags: ${tags}" |
| 103 | +
|
| 104 | + - name: Build Docker image |
| 105 | + uses: docker/build-push-action@v5 |
| 106 | + with: |
| 107 | + context: . |
| 108 | + file: ./Dockerfile |
| 109 | + push: false |
| 110 | + load: true |
| 111 | + tags: pulp/${{ env.IMAGE_NAME }}:ci |
| 112 | + labels: | |
| 113 | + org.opencontainers.image.title=Pulp Manager |
| 114 | + org.opencontainers.image.description=FastAPI-based orchestration and management for multiple Pulp 3 servers |
| 115 | + org.opencontainers.image.vendor=Pulp |
| 116 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 117 | + org.opencontainers.image.revision=${{ github.sha }} |
| 118 | + cache-from: type=gha |
| 119 | + cache-to: type=gha,mode=max |
| 120 | + platforms: linux/amd64 |
| 121 | + |
| 122 | + - name: Push to registries |
| 123 | + run: | |
| 124 | + for registry in ${{ steps.registries.outputs.registries }}; do |
| 125 | + echo "Pushing to ${registry}..." |
| 126 | + for tag in ${{ steps.tags.outputs.tags }}; do |
| 127 | + echo " Tagging and pushing ${registry}/pulp/${{ env.IMAGE_NAME }}:${tag}" |
| 128 | + docker tag pulp/${{ env.IMAGE_NAME }}:ci ${registry}/pulp/${{ env.IMAGE_NAME }}:${tag} |
| 129 | + docker push ${registry}/pulp/${{ env.IMAGE_NAME }}:${tag} |
| 130 | + done |
| 131 | + done |
| 132 | +
|
| 133 | + - name: Generate artifact attestation |
| 134 | + if: github.event_name != 'pull_request' |
| 135 | + uses: actions/attest-build-provenance@v1 |
| 136 | + with: |
| 137 | + subject-name: ghcr.io/pulp/${{ env.IMAGE_NAME }} |
| 138 | + subject-digest: ${{ hashFiles('Dockerfile') }} |
| 139 | + push-to-registry: true |
0 commit comments