-
Notifications
You must be signed in to change notification settings - Fork 0
82 lines (70 loc) · 2.69 KB
/
cd.yml
File metadata and controls
82 lines (70 loc) · 2.69 KB
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
name: CD
on:
push:
branches: [ main ]
workflow_dispatch:
jobs:
continuous-delivery:
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: 'corretto'
- name: Set YML
env:
YAML_SECRET: ${{ secrets.APPLICATION_YAML }}
YAML_DIR: src/main/resources
YAML_FILE_NAME: application.yml
run: |
mkdir -p $YAML_DIR
echo $YAML_SECRET | base64 --decode > $YAML_DIR/$YAML_FILE_NAME
find src
- name: Gradle Caching
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: Get Short SHA
id: short_sha
run: |
echo "::set-output name=sha_short::$(echo ${{ github.sha }} | head -c 7)"
echo "Short SHA: ${{ steps.short_sha.outputs.sha_short }}"
- name: Print branch & tag information
run: |
echo "Current branch: ${{ github.event.inputs.branch || github.ref }}"
echo "Current tag: ${{ steps.short_sha.outputs.sha_short }}"
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash
- name: Run build
run: ./gradlew build -x test
shell: bash
- name: Docker Login and Push
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_IMAGE_TAG: test:${{ steps.short_sha.outputs.sha_short }}
run: |
echo $DOCKER_HUB_PASSWORD | docker login -u $DOCKER_HUB_USERNAME --password-stdin
docker build -t $DOCKER_IMAGE_TAG .
docker tag $DOCKER_IMAGE_TAG $DOCKER_HUB_USERNAME/$DOCKER_IMAGE_TAG
docker push $DOCKER_HUB_USERNAME/$DOCKER_IMAGE_TAG
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PEM_KEY }}
script: |
docker pull ${{ secrets.DOCKER_USERNAME }}/test:${{ steps.short_sha.outputs.sha_short }}
docker tag ${{ secrets.DOCKER_USERNAME }}/test:${{ steps.short_sha.outputs.sha_short }} test
docker rm -f test || true
docker run -d --name test -e TZ=Asia/Seoul -p 8080:8080 ${{ secrets.DOCKER_USERNAME }}/test:${{ steps.short_sha.outputs.sha_short }}