1
- name : Build Docker image
1
+ name : Build and Publish Docker image
2
2
3
3
on :
4
+ pull_request :
5
+ branches :
6
+ - main
7
+
4
8
push :
5
9
branches :
6
10
- main
@@ -27,17 +31,14 @@ jobs:
27
31
fi
28
32
env :
29
33
version_pattern : " tags\\ /v[0-9]+\\ .[0-9]+\\ .[0-9]+"
30
- build-image :
34
+ build :
31
35
runs-on : ubuntu-latest
32
36
needs : prepare
33
37
environment :
34
38
name : cloudops
35
- outputs :
36
- BUILD_TAG : ${{ steps.build-push.outputs.build-tag }}
37
39
38
40
steps :
39
- - name : Echo tag
40
- id : echotag
41
+ - name : Docker image tag
41
42
env :
42
43
IMAGE_TAG : ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
43
44
run : |
@@ -54,14 +55,11 @@ jobs:
54
55
password : ${{ secrets.DOCKERHUB_TOKEN }}
55
56
56
57
- name : Extract metadata (tags, labels) for Docker
57
- id : meta
58
58
uses : docker/metadata-action@v3
59
59
with :
60
60
images : $GITHUB_REPOSITORY
61
61
62
-
63
- - name : Build and push to Docker Hub
64
- id : build-push
62
+ - name : Build Docker image
65
63
env :
66
64
IMAGE_TAG : ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
67
65
run : |
72
70
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
73
71
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > ./version.json
74
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
+
93
+ steps :
94
+ - uses : actions/download-artifact@v3
95
+ 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
102
+
103
+ - name : Login to Docker Hub
104
+ uses : docker/login-action@v1
105
+ with :
106
+ username : ${{ secrets.DOCKERHUB_USERNAME }}
107
+ password : ${{ secrets.DOCKERHUB_TOKEN }}
108
+
109
+ - name : Build Docker image
110
+ env :
111
+ IMAGE_TAG : ${{ needs.prepare.outputs.FULL_IMAGE_TAG }}
112
+ run : |
75
113
docker image tag $GITHUB_REPOSITORY:$IMAGE_TAG $GITHUB_REPOSITORY:latest
76
114
docker push $GITHUB_REPOSITORY:$IMAGE_TAG
77
115
docker push $GITHUB_REPOSITORY:latest
78
- echo "::set-output name=build-tag::$IMAGE_TAG"
79
- echo "::debug::Set the build-tag output as $IMAGE_TAG"
0 commit comments