-
Notifications
You must be signed in to change notification settings - Fork 0
75 lines (65 loc) · 2.4 KB
/
docker-cicd.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: CI/CD Pipeline
on:
push:
branches:
- main # main 브랜치에 푸시될 때 파이프라인 실행
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'temurin'
- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Build with Gradle
run: ./gradlew clean build -x test
- name: Run Tests
run: ./gradlew test
- name: Extract build date
id: vars
run: echo "BUILD_DATE=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Build and push Docker image
run: |
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/example-project .
docker push ${{ secrets.DOCKER_HUB_USERNAME }}/example-project
deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
## deploy to production
- name: Deploy to prod
uses: appleboy/ssh-action@master
id: deploy-prod
if: contains(github.ref, 'main')
with:
host: ${{ secrets.SERVER_HOST }}
username: root
key: ${{ secrets.SERVER_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
docker network inspect app-network >/dev/null 2>&1 || docker network create app-network
# 실행 중인 'example-project' 컨테이너 중지 및 제거
existing_container=$(docker ps -q --filter "ancestor=${{ secrets.DOCKER_HUB_USERNAME }}/example-project")
if [[ -n "$existing_container" ]]; then
docker rm -f $existing_container
fi
docker image prune -f
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/example-project
docker run -d --network app-network -p 8084:8084 -e USE_PROFILE=prod ${{ secrets.DOCKER_HUB_USERNAME }}/example-project