updating the path: #6
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: Build, Test and Deploy Java App | |
on: [push] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Check out the repository to get the source code | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
# Step 2: Set up JDK 11, as the app is written in Java 11 | |
- name: Set up JDK 11 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'temurin' | |
java-version: '11' | |
# Step 3: Build the Java app using Maven | |
- name: Build Java app with Maven | |
run: mvn clean package | |
- name: Build with Maven | |
run: mvn -B package -f pom.xml | |
# Step 4: Upload the build artifacts (packaged app file) for use in other jobs | |
- name: Upload build artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: artifact | |
path: target/*.war | |
test: | |
runs-on: ubuntu-latest | |
needs: build # This job depends on the build job and runs after it | |
steps: | |
# Step 1: Check out the repository to get the source code | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
# Step 2: Set up JDK 11, as the app is written in Java 11 | |
- name: Set up JDK 11 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'temurin' | |
java-version: '11' # Corrected the single quote at the end | |
# Step 3: Download the build artifacts from the build job | |
- name: Download build artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
name: artifact | |
# Step 4: Set up and run SonarQube in a Docker container | |
- name: Set up SonarQube | |
run: | | |
docker pull sonarqube # Pull SonarQube image from Docker Hub | |
docker run -d --name sonarqube -p 9000:9000 sonarqube # Run SonarQube in detached mode on port 9000 | |
# Step 5: Wait for SonarQube to start (30 seconds delay to ensure it's ready) | |
- name: Wait for SonarQube to start | |
run: sleep 30 | |
# Step 6: Run SonarQube scan on the Java app project to check code quality | |
- name: Run SonarQube scan | |
run: | | |
sonar-scanner \ | |
-Dsonar.projectKey=my-web-app \ | |
-Dsonar.sources=./src \ | |
-Dsonar.host.url=http://localhost:9000 \ | |
-Dsonar.login=admin \ | |
-Dsonar.password=admin | |
# Step 7: Check SonarQube Quality Gate status (added this step to fail if gate does not pass) | |
- name: Check Quality Gate status | |
run: | | |
# Fetch the Quality Gate result for the project | |
STATUS=$(curl -s -u admin:admin "http://localhost:9000/api/qualitygates/project_status?projectKey=my-web-app" | jq -r .projectStatus.status) | |
if [ "$STATUS" != "OK" ]; then | |
echo "Quality Gate failed!" | |
exit 1 # Fail the workflow if the Quality Gate status is not OK | |
else | |
echo "Quality Gate passed!" | |
fi | |
env: | |
# Install jq, a JSON parser, to easily parse the Quality Gate API response | |
JQ: sudo apt-get install -y jq | |
deploy: | |
runs-on: ubuntu-latest | |
needs: test # This job depends on the test job and runs after it | |
steps: | |
# Step 1: Check out the repository to get the source code | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
# Step 2: Download the build artifacts from the build job | |
- name: Download build artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
name: web-app | |
# Step 3: Build Docker image for the app | |
- name: Build Docker image | |
run: | | |
docker build -t my-web-app . | |
# list all images | |
- name: List all images | |
run: docker images -a | |
# Step 4: Deploy the Docker container to run the app on port 80 | |
- name: Deploy Docker container | |
run: | | |
docker run -d -p 80:8080 my-web-app |