|
1 | 1 | name: Docker |
2 | 2 |
|
3 | 3 | # This will run when: |
4 | | -# - when new code is pushed to master/develop to push the tags |
| 4 | +# - when new code is pushed to main/develop to push the tags |
5 | 5 | # latest and develop |
6 | 6 | # - when a pull request is created and updated to make sure the |
7 | 7 | # Dockerfile is still valid. |
8 | | -# To be able to push to dockerhub, this execpts the following |
| 8 | +# To be able to push to dockerhub, this expects the following |
9 | 9 | # secrets to be set in the project: |
10 | 10 | # - DOCKERHUB_USERNAME : username that can push to the org |
11 | 11 | # - DOCKERHUB_PASSWORD : password asscoaited with the username |
12 | 12 | on: |
| 13 | + # Trigger a build for every new push to the main branch |
13 | 14 | push: |
14 | 15 | branches: |
15 | 16 | - master |
|
18 | 19 |
|
19 | 20 | pull_request: |
20 | 21 |
|
21 | | - # Trigger the workflow on release activity |
| 22 | + # Trigger a build for every new release |
22 | 23 | release: |
23 | | - # Only use the types keyword to narrow down the activity types that will trigger your workflow. |
24 | 24 | types: |
25 | 25 | - published |
26 | 26 | - edited |
27 | 27 | - created |
28 | 28 |
|
29 | | -# Certain actions will only run when this is the master repo. |
| 29 | +# Certain actions will only run when this is the main repo. |
30 | 30 | env: |
31 | | - MASTER_REPO: nds-org/workbench-apiserver-python |
| 31 | + MAIN_REPO: nds-org/workbench-apiserver-python |
32 | 32 | DOCKERHUB_ORG: ndslabs |
33 | 33 |
|
34 | 34 | jobs: |
35 | 35 | docker: |
36 | 36 | runs-on: ubuntu-latest |
37 | | - strategy: |
38 | | - fail-fast: true |
39 | | - matrix: |
40 | | - name: |
41 | | - - api |
42 | | - include: |
43 | | - - name: api |
44 | | - FOLDER: . |
45 | | - IMAGE: apiserver |
46 | | - |
47 | 37 | steps: |
48 | | - - uses: actions/checkout@v3 |
| 38 | + - name: Set up Docker Buildx |
| 39 | + uses: docker/setup-buildx-action@v2 |
49 | 40 |
|
50 | | - # calculate some variables that are used later |
51 | | - - name: github branch |
52 | | - run: | |
53 | | - if [ "${{ github.event.release.target_commitish }}" != "" ]; then |
54 | | - BRANCH="${{ github.event.release.target_commitish }}" |
55 | | - else |
56 | | - BRANCH=${GITHUB_REF##*/} |
57 | | - fi |
58 | | - echo "GITHUB_BRANCH=${BRANCH}" >> $GITHUB_ENV |
59 | | - |
60 | | - # Commit was for main/release branch, build a new version |
61 | | - if [ "$BRANCH" == "master" -o "$BRANCH" == "main" ]; then |
62 | | - version="$(cat openapi/swagger-v1.yml | grep "^ version:" | awk '{print $2}')" |
63 | | - echo "VERSION=$(version)" >> $GITHUB_ENV |
64 | | - tags="latest" |
65 | | - oldversion="" |
66 | | - while [ "${oldversion}" != "${version}" ]; do |
67 | | - oldversion="${version}" |
68 | | - tags="${tags},${version}" |
69 | | - version=${version%.*} |
70 | | - done |
71 | | - echo "TAGS=${tags}" >> $GITHUB_ENV |
72 | | - else |
73 | | - echo "VERSION=$BRANCH" >> $GITHUB_ENV |
74 | | - echo "TAGS=$BRANCH" >> $GITHUB_ENV |
75 | | - fi |
76 | | -
|
77 | | - # build the docker image, this will always run to make sure |
78 | | - # the Dockerfile still works. |
79 | | - - name: Build image |
80 | | - uses: elgohr/Publish-Docker-Github-Action@2.22 |
81 | | - env: |
82 | | - BRANCH: ${{ env.GITHUB_BRANCH }} |
83 | | - VERSION: ${{ env.VERSION }} |
84 | | - BUILDNUMBER: ${{ github.run_number }} |
85 | | - GITSHA1: ${{ github.sha }} |
86 | | - with: |
87 | | - registry: docker.pkg.github.com |
88 | | - name: ${{ github.repository_owner }}/${{ github.event.repository.name }}/${{ matrix.IMAGE }} |
89 | | - username: ${{ github.actor }} |
90 | | - password: ${{ secrets.GITHUB_TOKEN }} |
91 | | - context: ${{ matrix.FOLDER }} |
92 | | - tags: "${{ env.TAGS }}" |
93 | | - buildargs: BRANCH,VERSION,BUILDNUMBER,GITSHA1 |
94 | | - no_push: true |
| 41 | + - uses: actions/checkout@v3 |
95 | 42 |
|
96 | | - # this will publish to github container registry |
97 | | - - name: Publish to GitHub |
98 | | - if: github.event_name != 'pull_request' && github.repository == env.MASTER_REPO |
99 | | - uses: elgohr/Publish-Docker-Github-Action@2.22 |
100 | | - env: |
101 | | - BRANCH: ${{ env.GITHUB_BRANCH }} |
102 | | - VERSION: ${{ env.VERSION }} |
103 | | - BUILDNUMBER: ${{ github.run_number }} |
104 | | - GITSHA1: ${{ github.sha }} |
| 43 | + - name: Docker meta |
| 44 | + id: meta |
| 45 | + uses: docker/metadata-action@v4 |
105 | 46 | with: |
106 | | - registry: ghcr.io |
107 | | - name: ${{ github.repository_owner }}/${{ matrix.IMAGE }} |
108 | | - username: ${{ secrets.GHCR_USERNAME }} |
109 | | - password: ${{ secrets.GHCR_PASSWORD }} |
110 | | - context: ${{ matrix.FOLDER }} |
111 | | - tags: "${{ env.TAGS }}" |
112 | | - buildargs: BRANCH,VERSION,BUILDNUMBER,GITSHA1 |
| 47 | + images: | |
| 48 | + ndslabs/apiserver |
| 49 | + tags: | |
| 50 | + # set latest tag for default branch |
| 51 | + type=raw,value=latest,enable={{is_default_branch}} |
| 52 | + type=ref,event=branch |
| 53 | + type=ref,event=pr |
| 54 | + type=ref,event=tag |
| 55 | + type=semver,pattern={{version}} |
| 56 | + type=semver,pattern={{major}}.{{minor}} |
| 57 | + type=semver,pattern={{major}} |
113 | 58 |
|
114 | | - # this will publish to dockerhub |
115 | | - - name: Publish to Docker Hub |
116 | | - if: github.event_name != 'pull_request' && github.repository == env.MASTER_REPO |
117 | | - uses: elgohr/Publish-Docker-Github-Action@2.22 |
118 | | - env: |
119 | | - BRANCH: ${{ env.GITHUB_BRANCH }} |
120 | | - VERSION: ${{ env.VERSION }} |
121 | | - BUILDNUMBER: ${{ github.run_number }} |
122 | | - GITSHA1: ${{ github.sha }} |
| 59 | + - name: Login to DockerHub |
| 60 | + uses: docker/login-action@v2 |
123 | 61 | with: |
124 | | - name: ${{ env.DOCKERHUB_ORG }}/${{ matrix.IMAGE }} |
125 | 62 | username: ${{ secrets.DOCKERHUB_USERNAME }} |
126 | 63 | password: ${{ secrets.DOCKERHUB_PASSWORD }} |
127 | | - context: ${{ matrix.FOLDER }} |
128 | | - tags: "${{ env.TAGS }}" |
129 | | - buildargs: BRANCH,VERSION,BUILDNUMBER,GITSHA1 |
| 64 | + |
| 65 | + - name: Build and push |
| 66 | + uses: docker/build-push-action@v3 |
| 67 | + with: |
| 68 | + context: . |
| 69 | + platforms: linux/amd64,linux/arm64 |
| 70 | + push: true |
| 71 | + tags: ${{ steps.meta.outputs.tags }} |
| 72 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments