-
Notifications
You must be signed in to change notification settings - Fork 3
75 lines (62 loc) · 2.75 KB
/
deploy_production.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
name: Deploy Production Server to AWS EC2 using Docker
on:
push:
branches:
- main
env:
DOCKER_IMAGE_NAME: ${{ secrets.PRODUCTION_DOCKER_IMAGE_NAME }}
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_SSH_USER: ec2-user
PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
CONTAINER_NAME_BLUE: ${{ secrets.CONTAINER_NAME_BLUE }}
CONTAINER_NAME_GREEN: ${{ secrets.CONTAINER_NAME_GREEN }}
BLUE_PORT: ${{ secrets.BLUE_PORT }}
GREEN_PORT: ${{ secrets.GREEN_PORT }}
jobs:
build-and-push-docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Set up application-blue.yml
run: echo "${{ secrets.BLUE_APPLICATION }}" > ./src/main/resources/application-blue.yml
- name: Set up application-green.yml
run: echo "${{ secrets.GREEN_APPLICATION }}" > ./src/main/resources/application-green.yml
- name: Build with Gradle
run: ./gradlew build -x test
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{ env.DOCKER_IMAGE_NAME }}:latest
- name: Login to Docker Hub using Access Token
run: echo "${{ secrets.DOCKER_HUB_TOKEN }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Push the Docker image
run: docker push ${{ env.DOCKER_IMAGE_NAME }}:latest
deploy-to-ec2:
needs: build-and-push-docker
runs-on: ubuntu-latest
steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ env.EC2_HOST }}
username: ${{ env.EC2_SSH_USER }}
key: ${{ env.PRIVATE_KEY }}
script: |
if sudo docker ps -a -f name=${{ env.CONTAINER_NAME_BLUE }} | grep -q "${{ env.CONTAINER_NAME_BLUE }}"; then
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }}
sudo docker run --name ${{ env.CONTAINER_NAME_GREEN }} -d -p ${{ env.GREEN_PORT }}:${{ env.GREEN_PORT }} -e SPRING_PROFILES_ACTIVE=green -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }}:latest
sleep 30
sudo docker stop ${{ env.CONTAINER_NAME_BLUE }}
sudo docker rm ${{ env.CONTAINER_NAME_BLUE }}
else
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }}
sudo docker run --name ${{ env.CONTAINER_NAME_BLUE }} -d -p ${{ env.BLUE_PORT }}:${{ env.BLUE_PORT }} -e SPRING_PROFILES_ACTIVE=blue -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }}:latest
sleep 30
sudo docker stop ${{ env.CONTAINER_NAME_GREEN }}
sudo docker rm ${{ env.CONTAINER_NAME_GREEN }}
fi
sudo systemctl reload nginx