@@ -3,13 +3,15 @@ name: Build and publish a Docker image
33on :
44 push :
55 branches : ['main']
6+ tags :
7+ - ' v*'
68
79env :
810 REGISTRY : ghcr.io
911 IMAGE_NAME : ${{ github.repository }}
1012
1113jobs :
12- build-and-push-image :
14+ build-and-push-image-testing :
1315 runs-on : ubuntu-latest
1416 permissions :
1517 contents : read
@@ -31,18 +33,60 @@ jobs:
3133 uses : docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
3234 with :
3335 images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
36+ tags : |
37+ type=raw,value=test,enable=true
3438
3539 - name : Build and push Docker image
3640 uses : docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
3741 with :
3842 context : .
3943 push : true
44+ build-args : |
45+ BUILD_MODE=development
4046 tags : ${{ steps.meta.outputs.tags }}
4147 labels : ${{ steps.meta.outputs.labels }}
42-
48+
49+ build-and-push-image-production :
50+ runs-on : ubuntu-latest
51+ if : startsWith(github.ref, 'refs/tags/v')
52+ permissions :
53+ contents : read
54+ packages : write
55+
56+ steps :
57+ - name : Checkout repository
58+ uses : actions/checkout@v2
59+
60+ - name : Log in to the Container registry
61+ uses : docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
62+ with :
63+ registry : ${{ env.REGISTRY }}
64+ username : ${{ github.actor }}
65+ password : ${{ secrets.GITHUB_TOKEN }}
66+
67+ - name : Extract metadata (tags, labels) for Docker
68+ id : meta
69+ uses : docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
70+ with :
71+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
72+ tags : |
73+ type=ref,event=tag,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
74+ type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
75+ type=raw,value=test,enable=true
76+
77+ - name : Build and push Docker image
78+ uses : docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
79+ with :
80+ context : .
81+ push : true
82+ build-args : |
83+ BUILD_MODE=production
84+ tags : ${{ steps.meta.outputs.tags }}
85+ labels : ${{ steps.meta.outputs.labels }}
86+
4387 deploy-testing :
4488 name : Deploy Testing
45- needs : build-and-push-image
89+ needs : build-and-push-image-testing
4690 runs-on : [self-hosted, Linux]
4791 environment :
4892 name : Testing
@@ -55,11 +99,36 @@ jobs:
5599 steps :
56100 - name : Run docker container
57101 run : |
58- docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main
102+ docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:test
59103 docker stop ${{ env.CONTAITER_NAME }} || true && docker rm ${{ env.CONTAITER_NAME }} || true
60104 docker run \
61105 --detach \
62106 --restart on-failure:3 \
63107 --network=web \
64108 --name ${{ env.CONTAITER_NAME }} \
65- ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main
109+ ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:test
110+
111+ deploy-production :
112+ name : Deploy Production
113+ needs : build-and-push-image-production
114+ if : startsWith(github.ref, 'refs/tags/v')
115+ runs-on : [self-hosted, Linux]
116+ environment :
117+ name : Production
118+ url : https://timetable.ui.profcomff.com/
119+ env :
120+ CONTAITER_NAME : com_profcomff_ui_timetable
121+ permissions :
122+ packages : read
123+
124+ steps :
125+ - name : Run docker container
126+ run : |
127+ docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
128+ docker stop ${{ env.CONTAITER_NAME }} || true && docker rm ${{ env.CONTAITER_NAME }} || true
129+ docker run \
130+ --detach \
131+ --restart always \
132+ --network=web \
133+ --name ${{ env.CONTAITER_NAME }} \
134+ ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
0 commit comments