-
Notifications
You must be signed in to change notification settings - Fork 0
124 lines (104 loc) · 3.27 KB
/
deploy.yml
File metadata and controls
124 lines (104 loc) · 3.27 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
name: Deploy to Docker Hub
on:
push:
branches: [ "main" ]
workflow_dispatch:
jobs:
run-test:
name: Run Test
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Run tests
run: ./gradlew test
build:
name: Build Java Project
runs-on: ubuntu-latest
if: |
!contains(github.event.head_commit.message, 'skip deploy') &&
!contains(github.event.pull_request.title, 'skip deploy') &&
!contains(github.event.pull_request.body, 'skip deploy')
permissions:
contents: read
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
- name: Verify build output
run: ls -al build/libs
- name: Archive build artifacts
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: build/libs/*.jar
push:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
needs: build
permissions:
contents: read
packages: write
actions: write
id-token: write
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build-artifacts
path: build-artifacts/
- name: Verify downloaded artifacts
run: ls -al build-artifacts
- name: Log in to Docker Hub with token
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Build and push Docker image
run: |
mkdir -p build/libs
mv build-artifacts/*.jar build/libs/
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/githubactionstudy:latest .
- name: Push Docker image
run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/githubactionstudy:latest
deploy:
name: Deploy to EC2
runs-on: ubuntu-latest
needs: push
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_EC2_HOST }}
username: ubuntu
key: ${{ secrets.AWS_EC2_SSH_KEY }}
script: |
echo "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" | docker login --username ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
cd /home/ubuntu
export SECRET_VALUE=${{ secrets.SECRET_VALUE}}
export DOCKER_HUB_USERNAME=${{ secrets.DOCKER_HUB_USERNAME }}
docker-compose down || true
docker rm -f $(docker ps -a -q) || true
docker-compose pull app
docker-compose up -d