-
Notifications
You must be signed in to change notification settings - Fork 1
87 lines (71 loc) · 2.9 KB
/
deploy.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
name: CICD Test
run-name: Running
on:
push:
branches:
- deploy
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'
- name: Set environment values
run: |
touch ./src/main/resources/.env
echo "${{ secrets.ENV }}" > ./src/main/resources/.env
shell: bash
- name: Load environment variables and run the application
run: |
export $(cat ./src/main/resources/.env | xargs)
- name: Build with Gradle
run: gradle clean build bootjar -x test
- name: Build and push Docker image
run: |
# Log in to Docker Hub
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
# Build the Docker image with the 'latest' tag
docker build -t ${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }} .
# Push the Docker image to Docker Hub
docker push ${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
- name: Clean up dangling Docker images locally
run: |
# Clean up any dangling images after the build
sudo docker image prune -f
- name: Test SSH connection
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
echo "Testing SSH connection"
uptime
- name: Deploy to EC2 Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
echo "Docker Hub에 로그인"
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
echo "최신 Docker 이미지 Pull"
sudo docker pull ${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
echo "기존 컨테이너가 있을 경우 중지 및 제거"
sudo docker ps -q -f name=${{ secrets.DOCKERHUB_CONTAINERNAME }} | xargs -r sudo docker stop
sudo docker ps -a -q -f name=${{ secrets.DOCKERHUB_CONTAINERNAME }} | xargs -r sudo docker rm
echo "환경 변수를 사용하여 새로운 컨테이너 실행"
sudo docker run -d \
-p 8080:8080 \
--name ${{ secrets.DOCKERHUB_CONTAINERNAME }} \
${{ secrets.DOCKERHUB_CONTAINERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest
echo "서버에서 오래된 Docker 이미지 정리"
sudo docker image prune -f