-
Notifications
You must be signed in to change notification settings - Fork 0
88 lines (75 loc) · 3.94 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
name: Build And Deploy for Develop Api Server
on:
push:
branches: ['develop']
env:
ACTIVE_PROFILE: "develop"
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
DOCKERHUB_API_IMAGE_NAME: ${{ secrets.DOCKERHUB_API_IMAGE_NAME }}
DOCKERHUB_ADMIN_IMAGE_NAME: ${{ secrets.DOCKERHUB_ADMIN_IMAGE_NAME }}
DOCKERHUB_WEB_IMAGE_NAME: ${{ secrets.DOCKERHUB_WEB_IMAGE_NAME }}
jobs:
build-and-push:
runs-on: ubuntu-latest
strategy:
matrix:
java-version: [ 17 ]
outputs:
image_tag: ${{ steps.image_tag.outputs.image_tag }}
steps:
- name: Check Out The Repository
uses: actions/checkout@v3
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java-version }}
distribution: 'corretto'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Execute Gradle build
run: |
./gradlew :cvs-api:build --no-daemon
./gradlew :cvs-admin:build --no-daemon
./gradlew :cvs-web:build --no-daemon
- name: Make image tag
run: echo "IMAGE_TAG=$ACTIVE_PROFILE-${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build docker image and push image
run: |
docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_TOKEN
docker build --build-arg PROFILE=$ACTIVE_PROFILE -t $DOCKERHUB_USERNAME/$DOCKERHUB_API_IMAGE_NAME:${{env.IMAGE_TAG}} ./cvs-api
docker build --build-arg PROFILE=$ACTIVE_PROFILE -t $DOCKERHUB_USERNAME/$DOCKERHUB_ADMIN_IMAGE_NAME:${{env.IMAGE_TAG}} ./cvs-admin
docker build --build-arg PROFILE=$ACTIVE_PROFILE -t $DOCKERHUB_USERNAME/$DOCKERHUB_WEB_IMAGE_NAME:${{env.IMAGE_TAG}} ./cvs-web
docker push $DOCKERHUB_USERNAME/$DOCKERHUB_API_IMAGE_NAME:${{env.IMAGE_TAG}}
docker push $DOCKERHUB_USERNAME/$DOCKERHUB_ADMIN_IMAGE_NAME:${{env.IMAGE_TAG}}
docker push $DOCKERHUB_USERNAME/$DOCKERHUB_WEB_IMAGE_NAME:${{env.IMAGE_TAG}}
- name: Pass Image tag value
id: image_tag
run: echo "image_tag=${{env.IMAGE_TAG}}" >> $GITHUB_OUTPUT
deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Set image tag from Output
run: echo "IMAGE_TAG=${{ needs.build-and-push.outputs.image_tag }}" >> $GITHUB_ENV
- name: SSH remote and Run Docker container
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_IP }}
username: ${{ secrets.REMOTE_USERNAME }}
key: ${{ secrets.REMOTE_SSH_KEY }}
port: ${{ secrets.REMOTE_SSH_PORT }}
script: |
sudo touch .env
echo "${{ secrets.ENV_VARS }}" | sudo tee .env > /dev/null
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_API_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_ADMIN_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_WEB_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker stop $(docker ps -a -q)
docker run --restart=unless-stopped -d -p 8080:8080 --env-file .env --add-host=host.docker.internal:host-gateway ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_API_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker run --restart=unless-stopped -d -p 10001:10001 --env-file .env --add-host=host.docker.internal:host-gateway ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_ADMIN_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker run --restart=unless-stopped -d -p 10002:10002 --env-file .env --add-host=host.docker.internal:host-gateway ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_WEB_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker rm $(docker ps --filter 'status=exited' -a -q)
docker image prune -a -f