-
Notifications
You must be signed in to change notification settings - Fork 0
131 lines (107 loc) · 4.23 KB
/
docker-ci.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
name: Java CI with Gradle
# 동작 조건 설정 : main 브랜치에 push가 발생할 경우 동작한다.
on:
push:
branches: [ "develop" ]
permissions:
contents: read
id-token: write
jobs:
# Spring Boot 애플리케이션을 빌드하여 도커허브에 푸시하는 과정
build-docker-image:
runs-on: ubuntu-22.04
steps:
- name: checkout
uses: actions/checkout@v4
# Google credentials
- name: google-credentials
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCP_CREDENTIAL_JSON }}'
project_id: '${{ secrets.GCP_PROJECT_ID }}'
#workload_identity_provider: '${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}'
#service_account: '${{ secrets.GCP_SERVICE_ACCOUNT }}'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v2'
- name: 'Use gcloud CLI'
run: 'gcloud info'
# 1. Java 세팅
- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
# - name: make application.yml
# run: |
# mkdir ./src/main/resources
# cd ./src/main/resources
# touch ./application.yml
#
# # GitHub-Actions 에서 설정한 값을 application.yml 파일에 쓰기
# echo "${{ secrets.PROPERTIES_YML }}" >> ./application.yml
# shell: bash
- name: make application.properties
run: |
cd ./src/main/resources
touch ./application.properties
# GitHub-Actions 에서 설정한 값을 application.properties 파일에 쓰기
echo "${{ secrets.APPLICATION_PROPERTIES }}" >> ./application.properties
shell: bash
# 환경 변수 설정 (Firebase)
- name: create-json
id: create-json
uses: jsdaniell/[email protected]
with:
name: "drugbox_firebase.json"
json: ${{ secrets.GCP_CREDENTIAL_JSON }}
dir: 'src/main/resources'
# 2. Spring Boot 애플리케이션 빌드
- name: Build with Gradle
run: |
chmod +x ./gradlew
./gradlew clean bootJar -x test
# 3. Docker 이미지 빌드
- name: docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/drugbox-grad .
# 4. DockerHub 로그인
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
# 5. Docker Hub 이미지 푸시
- name: docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/drugbox-grad
# 6. EC2 보안그룹에 Github Actions 추가
- name: Get Github action IP
id: ip
uses: haythem/[email protected]
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
- name: Add Github Actions IP to Security group
run: |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
# Deploy Images with Docker compose
- name: executing remote ssh commands using password
uses: appleboy/ssh-action@master
env:
APP: "drugbox-grad"
COMPOSE: "/home/ubuntu/compose/docker-compose.yml"
with:
username: ubuntu
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
port: ${{ secrets.EC2_SSH_PORT }}
envs: APP, COMPOSE
script_stop: true
script: |
sudo docker-compose -f $COMPOSE down
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/drugbox-grad
sudo docker-compose -f $COMPOSE up -d
- name: Remove Github Actions IP from security group
run: |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32