|
| 1 | +name: Docker Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ['main'] |
| 6 | + release: |
| 7 | + types: [created] |
| 8 | + |
| 9 | +env: |
| 10 | + # Use docker.io for Docker Hub if empty |
| 11 | + REGISTRY: ghcr.io |
| 12 | + # github.repository as <account>/<repo> |
| 13 | + IMAGE_NAME: ${{ github.repository }} |
| 14 | + |
| 15 | +jobs: |
| 16 | + build: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + packages: write |
| 21 | + id-token: write |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout repository |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + # Login against a Docker registry |
| 28 | + # https://github.com/docker/login-action |
| 29 | + - name: Log into registry ${{ env.REGISTRY }} |
| 30 | + uses: docker/login-action@v3 |
| 31 | + with: |
| 32 | + registry: ${{ env.REGISTRY }} |
| 33 | + username: ${{ github.actor }} |
| 34 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + |
| 36 | + # Extract metadata (tags, labels) for Docker |
| 37 | + # https://github.com/docker/metadata-action |
| 38 | + - name: Extract Docker metadata |
| 39 | + id: meta |
| 40 | + uses: docker/metadata-action@v5 |
| 41 | + with: |
| 42 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 43 | + |
| 44 | + # Conditional tagging based on the event type (main push or release) |
| 45 | + - name: Determine image tags |
| 46 | + id: tags |
| 47 | + run: | |
| 48 | + if [[ "${{ github.event_name }}" == "release" ]]; then |
| 49 | + # Tag as `latest` and `vX.X.X` for releases |
| 50 | + echo "TAGS=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:v${{ github.event.release.tag_name }}" >> $GITHUB_ENV |
| 51 | + else |
| 52 | + # Tag as `nightly` for pushes to main |
| 53 | + echo "TAGS=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly" >> $GITHUB_ENV |
| 54 | + shell: bash |
| 55 | + |
| 56 | + # Build and push Docker image with Buildx |
| 57 | + # https://github.com/docker/build-push-action |
| 58 | + - name: Build and push Docker image |
| 59 | + id: build-and-push |
| 60 | + uses: docker/build-push-action@v6 |
| 61 | + with: |
| 62 | + context: . |
| 63 | + tags: ${{ env.TAGS }} |
| 64 | + labels: ${{ steps.meta.outputs.labels }} |
| 65 | + cache-from: type=gha |
| 66 | + cache-to: type=gha,mode=max |
0 commit comments