-
Notifications
You must be signed in to change notification settings - Fork 1
138 lines (120 loc) · 4.89 KB
/
ci-cd.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
name: CI-CD
on:
push:
branches: [ "main", "dev"]
jobs:
CI-CD:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: make application.yml
if: contains(github.ref, 'main') || contains(github.ref, 'dev')
run: |
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.YML }}" > ./application.yml
shell: bash
- name: make application-dev.yml
if: contains(github.ref, 'dev')
run: |
cd ./src/main/resources
touch ./application-dev.yml
echo "${{ secrets.YML_DEV }}" > ./application-dev.yml
shell: bash
- name: make application-prod.yml
if: contains(github.ref, 'main')
run: |
cd ./src/main/resources
touch ./application-prod.yml
echo "${{ secrets.YML_PROD }}" > ./application-prod.yml
shell: bash
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build -x test
- name: docker build and push to dev
if: contains(github.ref, 'dev')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile.dev -t ${{ secrets.DOCKER_REPO }}/clubber-backend-dev .
docker push ${{ secrets.DOCKER_REPO }}/clubber-backend-dev
- name: docker build and push to dev nginx
if: contains(github.ref, 'dev')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile.nginx.dev -t ${{ secrets.DOCKER_REPO }}/clubber-nginx-dev .
docker push ${{ secrets.DOCKER_REPO }}/clubber-nginx-dev
- name: docker build and push to prod
if: contains(github.ref, 'main')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile.prod -t ${{ secrets.DOCKER_REPO }}/clubber-backend-prod .
docker push ${{ secrets.DOCKER_REPO }}/clubber-backend-prod
- name: docker build and push to prod nginx
if: contains(github.ref, 'main')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile.nginx.prod -t ${{ secrets.DOCKER_REPO }}/clubber-nginx-prod .
docker push ${{ secrets.DOCKER_REPO }}/clubber-nginx-prod
- name: Copy docker-compose.yml to EC2 DEV
if: contains(github.ref, 'dev')
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST_DEV }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY_DEV }}
source: ./docker-compose-dev.yml
target: /home/ubuntu
- name: Copy docker-compose.yml to EC2 PROD
if: contains(github.ref, 'main')
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST_PROD }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY_PROD }}
source: ./docker-compose-prod.yml
target: /home/ubuntu
- name: Deploy to dev
if: contains(github.ref, 'dev')
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST_DEV }}
username: ubuntu
key: ${{ secrets.SSH_KEY_DEV }}
script: |
cd /home/ubuntu
sudo docker rm -f $(docker ps -qa)
sudo docker pull ${{ secrets.DOCKER_REPO }}/clubber-backend-dev
sudo docker pull ${{ secrets.DOCKER_REPO }}/clubber-front-dev
sudo docker pull ${{ secrets.DOCKER_REPO }}/clubber-nginx-dev
sudo docker-compose -f docker-compose-dev.yml up -d
sudo docker image prune -f
- name: Deploy to prod
if: contains(github.ref, 'main')
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST_PROD }}
username: ubuntu
key: ${{ secrets.SSH_KEY_PROD }}
script: |
cd /home/ubuntu
sudo docker rm -f $(docker ps -qa)
sudo docker pull ${{ secrets.DOCKER_REPO }}/clubber-backend-prod
sudo docker pull ${{ secrets.DOCKER_REPO }}/clubber-front-prod
sudo docker pull ${{ secrets.DOCKER_REPO }}/clubber-nginx-prod
sudo docker-compose -f docker-compose-prod.yml up -d
sudo docker image prune -f