Update docker-image.yml #42
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
# Step to check and create the VERSION file if it does not exist | ||
- name: Check and create VERSION file | ||
run: | | ||
if [ ! -f VERSION ]; then | ||
echo "1.0.0" > VERSION | ||
fi | ||
# Step to read and increment the version from a file | ||
- name: Increment version | ||
id: increment_version | ||
run: | | ||
current_version=$(cat VERSION) | ||
echo "Current version: $current_version" | ||
major=$(echo $current_version | cut -d. -f1) | ||
minor=$(echo $current_version | cut -d. -f2) | ||
patch=$(echo $current_version | cut -d. -f3) | ||
new_patch=$((patch + 1)) | ||
new_version="$major.$minor.$new_patch" | ||
echo "New version: $new_version" | ||
echo "new_version=$new_version" >> $GITHUB_ENV | ||
echo $new_version > VERSION | ||
env: | ||
new_version: ${{ steps.increment_version.outputs.new_version }} | ||
outputs: | ||
new_version: ${{ steps.increment_version.outputs.new_version }} | ||
- 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: Build and push Docker image | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: . | ||
file: ./Dockerfile | ||
push: true | ||
tags: | | ||
wolwer/ms-pedido-api:${{ needs.build.outputs.new_version }} |