Skip to content

Create CI/CD for OBS #1

Create CI/CD for OBS

Create CI/CD for OBS #1

Workflow file for this run

name: Java CI/CD Pipeline with Maven
on:
push:
branches: [ "main", "staging" ]
pull_request:
branches: [ "main", "staging" ]
jobs:
# 1. Build and Static Code Analysis Stage
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn clean install
- name: Run PMD Check
run: mvn pmd:pmd
continue-on-error: true # Optional: Continue if PMD fails to not block the pipeline
- name: Upload PMD Report
if: success() || failure()
uses: actions/upload-artifact@v2
with:
name: pmd-report
path: target/site/pmd.html # Adjust path as needed
# 2. Code Quality Check with SonarQube
quality-check:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: SonarQube Scan
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn sonar:sonar -Dsonar.login=${{ secrets.SONAR_TOKEN }}
# 3. Test Stage
test:
runs-on: ubuntu-latest
needs: quality-check
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Run Unit Tests
run: mvn test
- name: Run Integration Tests
run: mvn verify
# 4. Deployment Stage
deploy:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker Image
run: docker build -t ${{ secrets.DOCKER_USERNAME }}/online-banking-service:${{ github.sha }} .
- name: Push Docker Image
run: docker push ${{ secrets.DOCKER_USERNAME }}/online-banking-service:${{ github.sha }}
- name: Deploy to Staging
if: github.ref == 'refs/heads/staging'
run: |
echo "Deploying to staging environment"
# Add deployment script or command here
- name: Deploy to Production
if: github.ref == 'refs/heads/main'
run: |
echo "Deploying to production environment"
# Add deployment script or command here
# 5. Notifications
notifications:
runs-on: ubuntu-latest
needs: [build, quality-check, test, deploy]
steps:
- name: Notify on Success or Failure
if: always()
run: |
if [ "${{ job.status }}" == "success" ]; then
echo "Build and deployment successful."
else
echo "Build or deployment failed."
fi