-
Notifications
You must be signed in to change notification settings - Fork 0
145 lines (124 loc) · 4.59 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
139
140
141
142
143
144
145
name: FINDY CI-CD Workflow
on:
push:
branches: [ "main", "develop" ]
permissions:
contents: read
jobs:
integration:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Java JDK 설정
- name: Setup Java JDK
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'temurin'
# 환경별 yml 파일 생성
- name: make application.yml
if: contains(github.ref, 'develop')
run: |
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.YML }}" > ./application.yml
# 환경별 yml 파일 생성 - oauth
- name: make application-oauth.yml
if: contains(github.ref, 'develop')
run: |
cd ./src/main/resources
touch ./application-oauth.yml
echo "${{ secrets.YML_OAUTH }}" > ./application-oauth.yml
# 환경별 yml 파일 생성 - dev
- name: make application-dev.yml
if: contains(github.ref, 'develop')
run: |
cd ./src/main/resources
touch ./application-dev.yml
echo "${{ secrets.YML_DEV }}" > ./application-dev.yml
# Gradle 패키지 캐시
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
# gradlew 실행 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Spring Boot 애플리케이션 빌드
- name: Build with Gradle
uses: gradle/gradle-build-action@v3
with:
arguments: clean bootJar
# Docker Hub에 로그인
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Metadata action
- name: Metadata action
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ secrets.DOCKER_REPO }}
# Docker image Build & push
- name: Build and push Docker image
if: contains(github.ref, 'develop')
id: docker_build
uses: docker/build-push-action@v3
with:
context: .
file: ./docker/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}-${{ github.run_number }}
labels: ${{ steps.meta.outputs.labels }}
deploy:
runs-on: ubuntu-latest
needs: integration
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set environment variables for Docker Compose
run: |
echo "DOCKER_REPO=${{ secrets.DOCKER_REPO }}" >> $GITHUB_ENV
echo "GITHUB_RUN_NUMBER=${{ github.run_number }}" >> $GITHUB_ENV
- name: Check docker-compose.yml file
run: |
if [ ! -f docker/docker-compose.yml ]; then
echo "Error: docker/docker-compose.yml not found."
exit 1
fi
- name: Copy docker-compose into instance
uses: appleboy/scp-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PEM_KEY }}
source: "docker/docker-compose.yml"
target: "/home/${{ secrets.USERNAME }}/docker/docker-compose.yml"
strip_components: 3
overwrite: true
debug: true
- name: Deploy to EC2 dev
uses: appleboy/ssh-action@master
if: contains(github.ref, 'develop')
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PEM_KEY }}
script: |
cd /home/${{ secrets.USERNAME }}
export DOCKER_REPO=${{ secrets.DOCKER_REPO }}
export GITHUB_RUN_NUMBER=${{ github.run_number }}
echo "DOCKER_REPO=${DOCKER_REPO}"
echo "GITHUB_RUN_NUMBER=${GITHUB_RUN_NUMBER}"
envsubst < <(echo "${{ secrets.DOCKER_COMPOSE_DEV }}") > docker-compose.yml
echo ${{ secrets.DOCKER_PASSWORD }} | sudo docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
if [ "$(sudo docker ps -q)" ]; then sudo docker stop $(sudo docker ps -q); fi
if [ "$(sudo docker ps -a -q)" ]; then sudo docker rm $(sudo docker ps -a -q); fi
sudo docker pull ${{ secrets.DOCKER_REPO }}:develop-${{ github.run_number }}
sudo docker-compose -f docker-compose.yml up -d
sudo docker image prune -f
debug: true