Skip to content

Update docker-image.yml #45

Update docker-image.yml

Update docker-image.yml #45

Workflow file for this run

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: Check and create VERSION file
run: |
if [ ! -f VERSION ]; then
echo "1.0.0" > VERSION
fi
- name: Increment version
id: vars
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
- name: Commit and push VERSION file
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add VERSION
git commit -m "Increment version to ${{ env.new_version }}"
git push
- 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:${{ env.new_version }}