@@ -3,113 +3,49 @@ name: Build and Publish Docker image
3
3
on :
4
4
pull_request :
5
5
branches :
6
- - main
7
-
6
+ - main
8
7
push :
9
8
branches :
10
- - main
9
+ - main
11
10
tags :
12
- - v[0-9]+.[0-9]+.[0-9]+
11
+ - v[0-9]+.[0-9]+.[0-9]+
13
12
14
13
jobs :
15
- prepare :
14
+ build-and-publish :
16
15
runs-on : ubuntu-latest
17
- outputs :
18
- FULL_IMAGE_TAG : ${{ steps.tag.outputs.tag }}
19
16
steps :
20
- - name : Set Tag
21
- id : tag
22
- run : |
23
- export CI_COMMIT_SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-8)
24
- echo $CI_COMMIT_SHORT_SHA;
25
- echo ${GITHUB_REF##*/};
26
- echo $version_pattern;
27
- if [[ ${GITHUB_REF} =~ $version_pattern ]]; then
28
- echo "::set-output name=tag::${GITHUB_REF##*/}"
29
- else
30
- echo "::set-output name=tag::${GITHUB_REF##*/}-$CI_COMMIT_SHORT_SHA"
31
- fi
32
- env :
33
- version_pattern : " tags\\ /v[0-9]+\\ .[0-9]+\\ .[0-9]+"
34
- build :
35
- runs-on : ubuntu-latest
36
- needs : prepare
37
- environment :
38
- name : cloudops
17
+ - name : Check out the repo
18
+ uses : actions/checkout@v3
39
19
40
- steps :
41
- - name : Docker image tag
42
- env :
43
- IMAGE_TAG : ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
20
+ - name : Build `version.json` file
44
21
run : |
45
- echo "Building an image with the following tag:"
46
- echo $IMAGE_TAG
47
-
48
- - name : Checkout
49
- uses : actions/checkout@v2
50
-
51
- - name : Login to Docker Hub
52
- uses : docker/login-action@v1
53
- with :
54
- username : ${{ secrets.DOCKERHUB_USERNAME }}
55
- password : ${{ secrets.DOCKERHUB_TOKEN }}
56
-
57
- - name : Extract metadata (tags, labels) for Docker
58
- uses : docker/metadata-action@v3
59
- with :
60
- images : $GITHUB_REPOSITORY
61
-
62
- - name : Build Docker image
63
- env :
64
- IMAGE_TAG : ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
65
- run : |
66
- printf '{\n "commit": "%s",\n "version": "%s",\n "image_tag": "%s",\n "source": "%s",\n "build": "%s"\n}\n' \
22
+ printf '{\n "commit": "%s",\n "version": "%s",\n "source": "%s",\n "build": "%s"\n}\n' \
67
23
"$GITHUB_SHA" \
68
24
"$GITHUB_REF" \
69
- "$IMAGE_TAG" \
70
25
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
71
26
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > ./version.json
72
- docker build --file Dockerfile -t $GITHUB_REPOSITORY:$IMAGE_TAG .
73
-
74
- - name : Save image as artifact
75
- env :
76
- IMAGE_TAG : ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
77
- run : |
78
- docker save -o /tmp/container.tar.gz $GITHUB_REPOSITORY:$IMAGE_TAG
79
-
80
- - uses : actions/upload-artifact@v3
81
- with :
82
- name : container-image
83
- path : /tmp/container.tar.gz
84
- retention-days : 1 # Instead of default 90 days
85
-
86
- publish :
87
- runs-on : ubuntu-latest
88
- needs :
89
- - prepare
90
- - build
91
- if : ${{ !github.event.pull_request }} # Exclude pull-requests (see `on:` above)
92
27
93
- steps :
94
- - uses : actions/download-artifact@v3
28
+ - name : Extract metadata (tags, labels) for Docker
29
+ id : meta
30
+ uses : docker/metadata-action@v4
95
31
with :
96
- name : container-image
97
- path : /tmp/container.tar.gz
98
-
99
- - name : Load container from artifact
100
- run : |
101
- docker load -i /tmp/container.tar.gz
32
+ images : $GITHUB_REPOSITORY
33
+ # https://github.com/marketplace/actions/docker-metadata-action#tags-input
34
+ tags : |
35
+ type=semver,pattern={{version}}
36
+ type=raw,value=latest,enable={{is_default_branch}}
37
+ type=sha,enable={{is_default_branch}}
102
38
103
39
- name : Login to Docker Hub
104
- uses : docker/login-action@v1
40
+ if : github.event_name != 'pull_request'
41
+ uses : docker/login-action@v2
105
42
with :
106
43
username : ${{ secrets.DOCKERHUB_USERNAME }}
107
44
password : ${{ secrets.DOCKERHUB_TOKEN }}
108
45
109
- - name : Build Docker image
110
- env :
111
- IMAGE_TAG : ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
112
- run : |
113
- docker image tag $GITHUB_REPOSITORY:$IMAGE_TAG $GITHUB_REPOSITORY:latest
114
- docker push $GITHUB_REPOSITORY:$IMAGE_TAG
115
- docker push $GITHUB_REPOSITORY:latest
46
+ - name : Build and push
47
+ uses : docker/build-push-action@v3
48
+ with :
49
+ context : .
50
+ push : ${{ github.event_name != 'pull_request' }}
51
+ tags : ${{ steps.meta.outputs.tags }}
0 commit comments