-
Notifications
You must be signed in to change notification settings - Fork 0
88 lines (74 loc) · 2.74 KB
/
main.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
name: Deploy gradle in EC2
on:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
## Create secret yml files before build
- name: create application-common, local, test.yml
shell: bash
run: |
touch ./src/main/resources/application-common.yml
echo "${{secrets.APPLICATION_COMMON}}" > ./src/main/resources/application-common.yml
touch ./src/main/resources/application-local.yml
echo "${{secrets.APPLICATION_LOCAL}}" > ./src/main/resources/application-local.yml
touch ./src/main/resources/application-test.yml
echo "${{secrets.APPLICATION_TEST}}" > ./src/main/resources/application-test.yml
## Gradle
- name: Cache gradle packages
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant execute permission for gradlew
run: chmod +x gradlew
## Build without test
- name: Build with Gradle
run: ./gradlew clean build -x test
## Docker
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: true
tags: ${{ secrets.DOCKERHUB_REPO }}:latest
deploy:
needs: build # Run after build
runs-on: ubuntu-latest
steps:
- name: connect EC2 instance and deploy docker images to main server
uses: appleboy/[email protected]
with:
host: ${{ secrets.AWS_EC2_HOST_IP }}
username: ${{ secrets.AWS_EC2_USERNAME }}
key: ${{ secrets.AWS_SSH_KEY }}
## kill container -> remove container -> remove image -> pull new image -> run
script: |
sudo docker kill ${{ secrets.PROJECT_NAME }}
sudo docker rm ${{ secrets.PROJECT_NAME }}
sudo docker rmi ${{ secrets.DOCKERHUB_REPO }}
sudo docker pull ${{ secrets.DOCKERHUB_REPO }}:latest
sudo docker run -d --name ${{ secrets.PROJECT_NAME }} -p 8080:8080 ${{ secrets.DOCKERHUB_REPO }}:latest