|
| 1 | +# Inspired from: https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-docker-images |
| 2 | +name: Create and publish Docker image to GHCR |
| 3 | + |
| 4 | +# Configures this workflow to run every time a change is pushed to the branch called `release`. |
| 5 | +on: |
| 6 | + workflow_dispatch: |
| 7 | + push: |
| 8 | + branches: ['production'] |
| 9 | + |
| 10 | +jobs: |
| 11 | + build-and-push-image: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. |
| 14 | + permissions: |
| 15 | + contents: read |
| 16 | + packages: write |
| 17 | + id-token: write |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + # Uses the `docker/login-action` action to log in to the Github Container Registry |
| 24 | + - name: Log in to the Container registry |
| 25 | + uses: docker/login-action@v3 |
| 26 | + with: |
| 27 | + registry: ghcr.io |
| 28 | + username: ${{ github.actor }} |
| 29 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + |
| 31 | + # This step uses `docker/metadata-action` to extract tags and labels that will be applied to the specified image. |
| 32 | + # The `id` "meta" allows the output of this step to be referenced in a subsequent step. |
| 33 | + # The `images` value provides the base name for the tags and labels. |
| 34 | + - name: Extract metadata (tags, labels) for Docker |
| 35 | + id: meta |
| 36 | + uses: docker/metadata-action@v5 |
| 37 | + with: |
| 38 | + images: ghcr.io/amfoss/amMentor-backend |
| 39 | + tags: | |
| 40 | + # set latest tag for master branch |
| 41 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }},priority=2000 |
| 42 | + type=schedule,pattern={{date 'YYYYMMDD'}} |
| 43 | + type=ref,event=tag |
| 44 | + type=ref,event=pr |
| 45 | + type=sha |
| 46 | +
|
| 47 | + # This step uses the `docker/build-push-action` action to build the image. If the build succeeds, it pushes the image to GitHub Packages. |
| 48 | + - name: Build and push Docker image |
| 49 | + id: push |
| 50 | + uses: docker/build-push-action@v6 |
| 51 | + with: |
| 52 | + context: . |
| 53 | + push: true |
| 54 | + tags: ${{ steps.meta.outputs.tags }} |
| 55 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments