-
Notifications
You must be signed in to change notification settings - Fork 2
78 lines (68 loc) · 2.61 KB
/
github-actions.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
# github repository actions 페이지에 나타날 이름
name: CI/CD using github actions & docker
# event trigger
# main이나 develop 브랜치에 push가 되었을 때 실행
on:
push:
branches: [ "main"]
permissions:
contents: read
jobs:
CI-CD:
runs-on: ubuntu-latest
steps:
# JDK setting - github actions에서 사용할 JDK 설정 (프로젝트나 AWS의 java 버전과 달라도 무방)
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
# gradle caching - 빌드 시간 향상
- 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-
# 환경별 yml 파일 생성(1) - application.properties
- name: make application.properties
if: |
contains(github.ref, 'main') ||
contains(github.ref, 'develop')
run: |
mkdir ./src/main/resources
cd ./src/main/resources # resources 폴더로 이동
touch ./application.properties # application.properties 생성
echo "${{ secrets.YML }}" > ./application.properties # github actions에서 설정한 값을 application.properties 파일에 쓰기
shell: bash
# gradle build
- name: Build with Gradle
run: ./gradlew build -x test
# docker build & push to production
- name: Docker build & push to prod
if: contains(github.ref, 'main')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/conteswt .
docker push ${{ secrets.DOCKER_USERNAME }}/conteswt
## deploy to production
- name: Deploy to prod
uses: appleboy/ssh-action@master
id: deploy-prod
if: contains(github.ref, 'main')
with:
host: ${{ secrets.HOST_PROD }} # EC2 퍼블릭 IPv4 DNS
username: ubuntu
key: ${{ secrets.PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
sudo docker ps
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/conteswt
sudo docker stop conteswt
sudo docker rm conteswt
sudo docker run -it -d -p 8080:8080 --name conteswt ${{ secrets.DOCKER_USERNAME }}/conteswt
sudo docker image prune -f