-
Notifications
You must be signed in to change notification settings - Fork 0
192 lines (159 loc) · 5.52 KB
/
deploy.yml
File metadata and controls
192 lines (159 loc) · 5.52 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
name: TrackMoney Pipeline
on:
push:
branches:
- main
env:
APP_NAME: trackmoney
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
- name: Cache Maven
uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-maven
- name: Start Docker Compose
run: docker compose up -d
- name: Build & Run Tests
run: mvn -B clean verify -Dspring.profiles.active=dev
- name: Upload Test Reports for Sonar
uses: actions/upload-artifact@v4
with:
name: test-reports
path: |
target/site/jacoco/jacoco.xml
target/checkstyle-result.xml
sonar-analysis:
needs: build-and-test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
- name: Cache Sonar
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Download Test Reports
uses: actions/download-artifact@v4
with:
name: test-reports
path: target/site
- name: Start Docker Compose
run: docker compose up -d
- name: SonarCloud Analysis
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
mvn clean verify sonar:sonar \
-Dsonar.projectKey=felipemelozx_TrackMoney \
-Dsonar.organization=felipemelozx-trackmoney \
-Dsonar.host.url=https://sonarcloud.io \
-Dsonar.login=${{ secrets.SONAR_TOKEN }} \
-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml \
-Dsonar.java.checkstyle.reportPaths=target/checkstyle-result.xml \
-Dsonar.coverage.exclusions=**/*Entity.java \
-Dsonar.exclusions=**/seed/**
build-and-push:
runs-on: ubuntu-latest
name: Build & Push
needs: [build-and-test, sonar-analysis]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Cache Maven dependencies
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Extract version from pom.xml
id: version
run: |
VERSION=$(./mvnw -q -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec)
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
echo "Version: $VERSION"
- name: Build application JAR
run: ./mvnw clean package -DskipTests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
${{ secrets.DOCKER_USERNAME }}/${{ env.APP_NAME }}:latest
${{ secrets.DOCKER_USERNAME }}/${{ env.APP_NAME }}:${{ steps.version.outputs.VERSION }}
labels: |
org.opencontainers.image.version=${{ steps.version.outputs.VERSION }}
org.opencontainers.image.revision=${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
VERSION=${{ steps.version.outputs.VERSION }}
deploy:
runs-on: self-hosted
name: Deploy
needs: build-and-push
steps:
- name: Checkout do Código
uses: actions/checkout@v4
- name: Deploy com Docker Compose local
run: |
APP_DIR="/opt/${{ env.APP_NAME }}"
mkdir -p $APP_DIR
cp docker-compose.prod.yaml $APP_DIR/docker-compose.prod.yaml
# Create .env file with all environment variables
cat <<EOF > $APP_DIR/.env
DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}
IMAGE_TAG=latest
APP_PORT=8080
SERVER_CONTEXT_PATH=/api/v1
SPRING_PROFILES_ACTIVE=prod
POSTGRES_USER=${{ secrets.POSTGRES_USER }}
POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}
POSTGRES_DB=${{ secrets.POSTGRES_DB }}
POSTGRES_PORT=5432
REDIS_PORT=6379
SPRING_MAIL_HOST=${{ secrets.SPRING_MAIL_HOST }}
SPRING_MAIL_PORT=${{ secrets.SPRING_MAIL_PORT }}
SPRING_MAIL_USERNAME=${{ secrets.SPRING_MAIL_USERNAME }}
SPRING_MAIL_PASSWORD=${{ secrets.SPRING_MAIL_PASSWORD }}
API_SECRET_KEY=${{ secrets.API_SECRET_KEY }}
FRONT_URL=${{ secrets.FRONT_URL }}
EOF
cd $APP_DIR
docker compose -f docker-compose.prod.yaml down
docker compose -f docker-compose.prod.yaml up -d --force-recreate --pull always
docker image prune -f