-
Notifications
You must be signed in to change notification settings - Fork 0
66 lines (54 loc) · 1.89 KB
/
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
name: CI/CD
on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to deploy'
required: true
default: 'main'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.branch }}
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Generate application.yml
run: |
mkdir -p ./src/main/resources
echo "${{ secrets.CD_APPLICATION }}" > ./src/main/resources/application.yml
cat ./src/main/resources/application.yml
- name: Build Project
run: ./gradlew clean build -x test
- name: Login to Docker Hub
run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker Image
run: docker build -t yh0872/spoony-dev .
- name: Publish Image to Docker Hub
run: docker push yh0872/spoony-dev:latest
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Install SSH Client
run: sudo apt-get update && sudo apt-get install -y openssh-client
- name: Create SSH Key File
run: |
echo "${{ secrets.EC2_SSH_KEY }}" > ssh_key.pem
chmod 600 ssh_key.pem
- name: SSH into EC2 and Deploy
run: |
ssh -o StrictHostKeyChecking=no -i ssh_key.pem ubuntu@${{ secrets.EC2_PUBLIC_IP }} << 'EOF'
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
sudo docker pull yh0872/spoony-dev:latest
sudo docker rm -f spoony-dev-container || true
sudo docker run -d -p 8080:8080 --name spoony-dev-container yh0872/spoony-dev
EOF