|
| 1 | +name: Build and Publish to GHCR |
| 2 | + |
| 3 | +# 1. Trigger the workflow on push to main branch |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: [ "main" ] |
| 7 | + workflow_dispatch: # Allows you to run this manually from the Actions tab |
| 8 | + |
| 9 | +env: |
| 10 | + REGISTRY: ghcr.io |
| 11 | + # github.repository returns "owner/repo" (e.g., "yourname/sam3-project") |
| 12 | + IMAGE_NAME: ${{ github.repository }} |
| 13 | + |
| 14 | +jobs: |
| 15 | + build-and-push: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + # 2. Permissions required to write to GHCR |
| 19 | + permissions: |
| 20 | + contents: read |
| 21 | + packages: write |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout repository |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + # 3. Set up Docker Buildx (Critical for caching and platform support) |
| 28 | + - name: Set up Docker Buildx |
| 29 | + uses: docker/setup-buildx-action@v3 |
| 30 | + |
| 31 | + # 4. Log in to the Container registry |
| 32 | + - name: Log in to the Container registry |
| 33 | + uses: docker/login-action@v3 |
| 34 | + with: |
| 35 | + registry: ${{ env.REGISTRY }} |
| 36 | + username: ${{ github.actor }} |
| 37 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + |
| 39 | + # 5. Extract metadata (tags, labels) for Docker |
| 40 | + # This automatically tags the image as 'latest' on main branch push |
| 41 | + - name: Extract metadata (tags, labels) for Docker |
| 42 | + id: meta |
| 43 | + uses: docker/metadata-action@v5 |
| 44 | + with: |
| 45 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 46 | + tags: | |
| 47 | + type=raw,value=latest,enable={{is_default_branch}} |
| 48 | + type=sha,prefix=sha- |
| 49 | +
|
| 50 | + # 6. Build and push Docker image |
| 51 | + - name: Build and push Docker image |
| 52 | + uses: docker/build-push-action@v5 |
| 53 | + with: |
| 54 | + context: . |
| 55 | + file: ./Dockerfile |
| 56 | + push: true |
| 57 | + # FORCE linux/amd64 to ensure NVIDIA compatibility |
| 58 | + platforms: linux/amd64 |
| 59 | + tags: ${{ steps.meta.outputs.tags }} |
| 60 | + labels: ${{ steps.meta.outputs.labels }} |
| 61 | + # 7. Enable caching to speed up future builds |
| 62 | + cache-from: type=gha |
| 63 | + cache-to: type=gha,mode=max |
0 commit comments