-
Notifications
You must be signed in to change notification settings - Fork 0
136 lines (133 loc) · 4.85 KB
/
CI-CD.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
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
name: CI/CD
on:
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main
release:
types: [published]
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
tag_version: ${{ steps.set-tag-version.outputs.TAG_VERSION }}
push_image: ${{ steps.set-push-image.outputs.PUSH_IMAGE }}
deploy: ${{ steps.set-deploy.outputs.DEPLOY }}
environment: ${{ steps.set-deploy.outputs.ENVIRONMENT }}
steps:
- name: Validate release target branch
if: ${{ github.event_name == 'release' }}
run: |
if [[ "${{ github.event.release.target_commitish }}" != "${{ github.event.repository.default_branch }}" ]]; then
echo "Release target branch is not the default branch"
exit 1
fi
- name: Set tag version
id: set-tag-version
shell: bash
run: |
if [ -n "${{ github.event.release.tag_name }}" ]; then
echo "Setting TAG_VERSION to ${{ github.event.release.tag_name }}"
echo "TAG_VERSION=${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT"
else
echo "Setting TAG_VERSION to ${{ github.sha }}"
echo "TAG_VERSION=${{ github.sha }}" >> "$GITHUB_OUTPUT"
fi
- name: Set push image
id: set-push-image
shell: bash
run: |
if [[ "${{ github.event_name }}" == "pull_request" && "${{ github.event.pull_request.merged }}" == "true" ]] ||
[[ "${{ github.event_name }}" == "release" && "${{ github.event.action }}" == "published" ]]; then
echo "Setting PUSH_IMAGE to true"
echo "PUSH_IMAGE=true" >> "$GITHUB_OUTPUT"
else
echo "Setting PUSH_IMAGE to false"
echo "PUSH_IMAGE=false" >> "$GITHUB_OUTPUT"
fi
- name: Set deploy
id: set-deploy
shell: bash
run: |
if [[ "${{ github.event_name }}" == "pull_request" && "${{ github.event.pull_request.merged }}" == "true" ]]; then
echo "Setting DEPLOY to true"
echo "Setting ENVIRONMENT to staging"
echo "DEPLOY=true" >> "$GITHUB_OUTPUT"
echo "ENVIRONMENT=STAGING" >> "$GITHUB_OUTPUT"
elif [[ "${{ github.event_name }}" == "release" && "${{ github.event.action }}" == "published" ]]; then
echo "Setting DEPLOY to true"
echo "Setting ENVIRONMENT to production"
echo "DEPLOY=true" >> "$GITHUB_OUTPUT"
echo "ENVIRONMENT=PRODUCTION" >> "$GITHUB_OUTPUT"
else
echo "Setting DEPLOY to false"
echo "DEPLOY=false" >> "$GITHUB_OUTPUT"
fi
build-and-test:
if: github.event_name != 'release'
runs-on: ubuntu-latest
needs: prepare
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'
- name: Compile
run: |
echo "Compiling the code"
./mvnw compile
- name: Test
run: |
echo "Running the tests"
./mvnw test
- name: Add coverage to PR
id: jacoco
uses: madrapps/[email protected]
with:
paths: |
${{ github.workspace }}/target/site/jacoco/jacoco.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 40
min-coverage-changed-files: 60
title: Coverage report
comment: true
only-changed-files: true
build-image-push:
if: always()
runs-on: ubuntu-latest
needs: [prepare, build-and-test]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata (tags, labels) for Docker
uses: docker/metadata-action@v3
with:
images: ${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }}
- name: Build and push image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
push: ${{ needs.prepare.outputs.push_image }}
tags: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }}
deploy:
runs-on: ubuntu-latest
needs: [prepare, build-image-push]
if: ${{ needs.prepare.outputs.deploy == 'true' }}
steps:
- name: Deploy
run: |
echo "Deploying the app to ${{ needs.prepare.outputs.environment }} using ${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }} image"