-
Notifications
You must be signed in to change notification settings - Fork 1
149 lines (123 loc) · 4.21 KB
/
docker-image.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
137
138
139
140
141
142
143
144
145
146
147
148
149
name: Docker Image CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
- name: Cache Maven packages
uses: actions/cache@v2
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
run: mvn -B package --file pom.xml
test:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
- name: Cache Maven packages
uses: actions/cache@v2
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Test with Maven
run: mvn test
sonarcloud-analysis:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
- name: SonarCloud analysis
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=${{ secrets.SONAR_PROJECTKEY }} -Dsonar.organization=${{ secrets.SONAR_ORGANIZATION }} -Dsonar.host.url=https://sonarcloud.io -Dsonar.token=${{ secrets.SONAR_TOKEN }}
- name: Install xmllint
run: sudo apt-get install -y libxml2-utils
- name: Check JaCoCo Coverage
run: |
JACOCO_XML_PATH=target/site/jacoco/jacoco.xml
COVERAGE=$(xmllint --xpath 'string(//report/counter[@type="INSTRUCTION"]/@covered)' $JACOCO_XML_PATH)
MISSED=$(xmllint --xpath 'string(//report/counter[@type="INSTRUCTION"]/@missed)' $JACOCO_XML_PATH)
TOTAL=$(echo "$COVERAGE + $MISSED" | bc)
PERCENTAGE=$(echo "scale=2; $COVERAGE / $TOTAL * 100" | bc)
echo "Code coverage percentage: $PERCENTAGE%"
if (( $(echo "$PERCENTAGE < 80" | bc -l) )); then
echo "Code coverage is below 80%: $PERCENTAGE%"
exit 1
fi
deploy-to-dockerhub:
needs: sonarcloud-analysis
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Read current version
id: get_version
run: echo "VERSION=$(cat version.txt)" >> $GITHUB_ENV
- name: Increment patch version
id: increment_version
run: |
VERSION=${{ env.VERSION }}
IFS='.' read -ra ADDR <<< "$VERSION"
PATCH=$((ADDR[2] + 1))
NEW_VERSION="${ADDR[0]}.${ADDR[1]}.$PATCH"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
echo $NEW_VERSION > version.txt
shell: bash
- name: Commit new version
env:
PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
git config --global user.name 'github-actions'
git config --global user.email '[email protected]'
git remote set-url origin https://x-access-token:${{ secrets.PAT_TOKEN }}@github.com/FIAP-Tech-Chalenge/ms-pedido-api.git
git add version.txt
git commit -m "chore: bump version to ${{ env.NEW_VERSION }}"
git push origin HEAD:main
- name: Debugging Information
run: |
git remote -v
git status
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: |
wolwer/ms-pedido-api:${{ env.NEW_VERSION }}