-
Notifications
You must be signed in to change notification settings - Fork 2
84 lines (71 loc) · 3.04 KB
/
cicd-dev.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
name: Docker Hub Dev Deploy
run-name: ${{ github.head_ref }} 브랜치 작업 내용 병합 후 배포
on:
push:
branches:
- develop
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# develop 브랜치 체크아웃
- name: Checkout
uses: actions/checkout@v3
# application.yml 파일 생성
- name: make application.yml
run: |
# create application.yml
cd ./src/main
cd .resources
touch ./application.yml
echo "${{ secrets.APPLICATION_DEV_YML }}" >> ./application.yml
# 빌드 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
# docker image 빌드
- name: Build
run: docker build --no-cache -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:${{ secrets.DOCKERHUB_IMAGE_TAG }} .
# docker hub 로그인
- name: Docker Hub login
uses: docker/login-action@v3
with:
username: ${{ secretes.DOCKERHUB_USERNAME }}
password: ${{ secretes.DOCKERHUB_TOKEN_READ_WRITE }}
# docker hub에 image push
- name: docker image push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME}}:${{secrets.DOCKERHUB_IMAGE_TAG }}
# docker compose에 사용될 환경 변수들이 담긴 env 파일 세팅
- name: Set up environment variables
run: |
echo "DOCKER_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}" >> dev.env
echo "DOCKER_IMAGE_NAME=${{ secrets.DOCKERHUB_IMAGE_NAME }}" >> dev.env
echo "DOCKER_IMAGE_TAG=${{ secrets.DOCKERHUB_IMAGE_TAG }}" >> dev.env
echo "SPRING_OUTER_PORT=${{ secrets.SPRING_OUTER_PORT }}" >> dev.env
echo "SPRING_INNER_PORT=${{ secrets.SPRING_INNER_PORT }}" >> dev.env
# compose.yml, dev.env 파일 서버로 복사
- name: Copy compose.yml, dev.env
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "compose.yml, dev.env"
target: /home/ubuntu
# EC2에서 docker compose up
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /home/ubuntu
sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_TOKEN_READ_ONLY }}
sudo docker container stop naoman-dev
sudo docker container rm naoman-dev
sudo docker image rm ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME}}:${{secrets.DOCKERHUB_IMAGE_TAG }}
sudo docker compose --env-file dev.env -f compose.yml up -d
sudo docker container prune -f
sudo docker image prune -a -f