|
1 | | -name: Docker |
| 1 | +# Create and publish a Docker image on github |
| 2 | +name: Create and publish a Docker image |
2 | 3 |
|
| 4 | +# Configures this workflow to run every time a change |
| 5 | +# is pushed to the 'main' branch. |
3 | 6 | on: |
4 | 7 | push: |
5 | | - # Publish `master` as Docker `latest` image. |
| 8 | + # Publish `main` as Docker `latest` image. |
6 | 9 | branches: |
7 | 10 | - main |
8 | 11 |
|
9 | 12 | # Publish `v1.2.3` tags as releases. |
10 | 13 | tags: |
11 | 14 | - v* |
| 15 | + - '*-v*' |
12 | 16 |
|
13 | | - # Run tests for PRs on main branch only. |
14 | | - pull_request: |
15 | | - branches: |
16 | | - - main |
17 | | - |
| 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. |
18 | 20 | env: |
19 | | - # TODO: Change variable to your image's name. |
20 | | - IMAGE_NAME: indexing-service |
21 | | - # JDK version used for building jar file |
22 | | - currentBuildVersion: 17 |
| 21 | + REGISTRY: ghcr.io |
| 22 | + IMAGE_NAME: ${{ github.repository }} |
23 | 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) |
24 | 27 | jobs: |
25 | | - # Run tests. |
26 | | - # See also https://docs.docker.com/docker-hub/builds/automated-testing/ |
27 | | - test: |
| 28 | + build-and-push-image: |
28 | 29 | runs-on: ubuntu-latest |
29 | | - |
| 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 | + # |
30 | 37 | steps: |
31 | | - - name: Checkout repo |
32 | | - uses: actions/checkout@v4 |
33 | | - - name: Set up OpenJDK version ... |
34 | | - uses: actions/setup-java@v4 |
35 | | - with: |
36 | | - distribution: 'zulu' |
37 | | - java-version: ${{ env.currentBuildVersion }} |
38 | | - - name: Pull elasticsearch image from docker |
39 | | - run: docker pull elasticsearch:7.9.3 |
40 | | - - name: Install python |
41 | | - run: sudo apt-get install --assume-yes python3 python3-setuptools python3-pip |
42 | | - - name: Update pip |
43 | | - run: pip3 install --upgrade pip |
44 | | - - name: Install libraries via pip (xmltodict and wget) |
45 | | - run: pip3 install xmltodict wget |
46 | | - - name: Grant execute permission for gradlew |
47 | | - run: chmod +x gradlew |
48 | | - - name: Run tests |
49 | | - run: ./gradlew jacocoTestReport |
50 | | -# docker build . --file Dockerfile |
51 | | - # Push image to GitHub Packages. |
52 | | - # See also https://docs.docker.com/docker-hub/builds/ |
53 | | - push: |
54 | | - # Ensure test job passes before pushing image. |
55 | | - needs: test |
56 | | - |
| 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: |
57 | 70 | runs-on: ubuntu-latest |
58 | | - if: github.event_name == 'push' |
59 | | - |
| 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 | + # |
60 | 78 | steps: |
61 | | - - name: Checkout repo |
62 | | - uses: actions/checkout@v4 |
63 | | - |
64 | | - - name: Build image |
65 | | - run: | |
66 | | - docker build . --file Dockerfile --tag $IMAGE_NAME |
67 | | - - name: Log into registry |
68 | | - run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin |
69 | | - |
70 | | - - name: Push image |
71 | | - run: | |
72 | | - IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME |
73 | | - # Change all uppercase to lowercase |
74 | | - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') |
75 | | - # Strip git ref prefix from version |
76 | | - VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') |
77 | | - # Strip "v" prefix from tag name |
78 | | - [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') |
79 | | - # Use Docker `latest` tag convention |
80 | | - [ "$VERSION" == "main" ] && VERSION=latest |
81 | | - echo IMAGE_ID=$IMAGE_ID |
82 | | - echo VERSION=$VERSION |
83 | | - docker tag $IMAGE_NAME $IMAGE_ID:$VERSION |
84 | | - docker push $IMAGE_ID:$VERSION |
85 | | - - name: Docker meta |
86 | | - id: docker_meta |
87 | | - uses: crazy-max/ghaction-docker-meta@v5 |
88 | | - with: |
89 | | - # list of Docker images to use as base name for tags |
90 | | - images: | |
91 | | - kitdm/indexing-service |
92 | | - # add git short SHA as Docker tag |
93 | | - tag-sha: true |
94 | | - - name: Set up QEMU |
95 | | - uses: docker/setup-qemu-action@v3 |
96 | | - |
97 | | - - name: Set up Docker Buildx |
98 | | - uses: docker/setup-buildx-action@v3 |
99 | | - - name: Login to DockerHub |
100 | | - uses: docker/login-action@v3 |
101 | | - with: |
102 | | - username: ${{ secrets.DOCKER_USERNAME }} |
103 | | - password: ${{ secrets.DOCKER_PASSWORD }} |
104 | | - |
105 | | - - name: Push to Docker Hub |
106 | | - uses: docker/build-push-action@v5 |
107 | | - with: |
108 | | - context: . |
109 | | - push: true |
110 | | - tags: ${{ steps.docker_meta.outputs.tags }} |
111 | | - labels: ${{ steps.docker_meta.outputs.labels }} |
| 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