|
| 1 | +# Create and publish a Docker image on github |
| 2 | +name: Create and publish a Docker image |
| 3 | + |
| 4 | +# Configures this workflow to run every time a change |
| 5 | +# is tagged with a '-v' in between (e.g.: metastore-v1.0.0). |
| 6 | +on: |
| 7 | + push: |
| 8 | + # Publish `main` as Docker `latest` image. |
| 9 | +# branches: |
| 10 | +# - main |
| 11 | + |
| 12 | + # Publish `v1.2.3` tags as releases. |
| 13 | + tags: |
| 14 | + # - v* |
| 15 | + - '*-v*' |
| 16 | + |
| 17 | +# Defines two custom environment variables for the workflow. |
| 18 | +# These are used for the Container registry domain, and a |
| 19 | +# name for the Docker image that this workflow builds. |
| 20 | +env: |
| 21 | + REGISTRY: ghcr.io |
| 22 | + IMAGE_NAME: ${{ github.repository }} |
| 23 | + |
| 24 | +# Two jobs for creating and pushing Docker image |
| 25 | +# - build-and-push-image -> triggered by commits on main and tagging with semantic version (e.g.: v1.2.3) |
| 26 | +# - build-and-push-image-of-branch -> triggered by tags matching '*-v*' (e.g.: Version_1-v1.2.3) |
| 27 | +jobs: |
| 28 | + build-and-push-image: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + if: ${{ contains(github.ref_name, '-') == failure() }} |
| 31 | + # Sets the permissions granted to the `GITHUB_TOKEN` |
| 32 | + # for the actions in this job. |
| 33 | + permissions: |
| 34 | + contents: read |
| 35 | + packages: write |
| 36 | + # |
| 37 | + steps: |
| 38 | + - name: Checkout repository |
| 39 | + uses: actions/checkout@v4 |
| 40 | + # Uses the `docker/login-action` action to log in to the Container |
| 41 | + # registry using the account and password that will publish the packages. |
| 42 | + # Once published, the packages are scoped to the account defined here. |
| 43 | + - name: Log in to the Container registry |
| 44 | + uses: docker/login-action@v3 |
| 45 | + with: |
| 46 | + registry: ${{ env.REGISTRY }} |
| 47 | + username: ${{ github.actor }} |
| 48 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 49 | + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) |
| 50 | + # to extract tags and labels that will be applied to the specified image. |
| 51 | + # The `id` "meta" allows the output of this step to be referenced in a |
| 52 | + # subsequent step. The `images` value provides the base name for the tags |
| 53 | + # and labels. |
| 54 | + - name: Extract metadata (tags, labels) for Docker |
| 55 | + id: meta |
| 56 | + uses: docker/metadata-action@v5 |
| 57 | + with: |
| 58 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 59 | + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. |
| 60 | + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. |
| 61 | + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. |
| 62 | + - name: Build and push Docker image |
| 63 | + uses: docker/build-push-action@v5 |
| 64 | + with: |
| 65 | + context: . |
| 66 | + push: true |
| 67 | + tags: ${{ steps.meta.outputs.tags }} |
| 68 | + labels: ${{ steps.meta.outputs.labels }} |
| 69 | + build-and-push-image-of-branch: |
| 70 | + runs-on: ubuntu-latest |
| 71 | + if: contains(github.ref_name, '-') |
| 72 | + # Sets the permissions granted to the `GITHUB_TOKEN` |
| 73 | + # for the actions in this job. |
| 74 | + permissions: |
| 75 | + contents: read |
| 76 | + packages: write |
| 77 | + # |
| 78 | + steps: |
| 79 | + - name: Split first part |
| 80 | + env: |
| 81 | + TAG: ${{ github.ref_name }} |
| 82 | + id: split |
| 83 | + run: echo "branch=${TAG%-v*}" >> $GITHUB_OUTPUT |
| 84 | + - name: Test variable |
| 85 | + run: | |
| 86 | + echo ${{ steps.split.outputs.branch }} |
| 87 | + - name: Checkout repository |
| 88 | + uses: actions/checkout@v4 |
| 89 | + # Uses the `docker/login-action` action to log in to the Container |
| 90 | + # registry using the account and password that will publish the packages. |
| 91 | + # Once published, the packages are scoped to the account defined here. |
| 92 | + - name: Log in to the Container registry |
| 93 | + uses: docker/login-action@v3 |
| 94 | + with: |
| 95 | + registry: ${{ env.REGISTRY }} |
| 96 | + username: ${{ github.actor }} |
| 97 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) |
| 99 | + # to extract tags and labels that will be applied to the specified image. |
| 100 | + # The `id` "meta" allows the output of this step to be referenced in a |
| 101 | + # subsequent step. The `images` value provides the base name for the tags |
| 102 | + # and labels. |
| 103 | + - name: Extract metadata (tags, labels) for Docker |
| 104 | + id: meta-branch |
| 105 | + uses: docker/metadata-action@v5 |
| 106 | + with: |
| 107 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{steps.split.outputs.branch}} |
| 108 | + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. |
| 109 | + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. |
| 110 | + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. |
| 111 | + - name: Build and push Docker image |
| 112 | + uses: docker/build-push-action@v5 |
| 113 | + with: |
| 114 | + context: . |
| 115 | + push: true |
| 116 | + tags: ${{ steps.meta-branch.outputs.tags }} |
| 117 | + labels: ${{ steps.meta-branch.outputs.labels }} |
| 118 | + |
0 commit comments