diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..386bff4 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,90 @@ +name: CICD +on: + push: + branches: + - dev + - main + pull_request: + branches: + - dev + - main + +jobs: + 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: Java version + run: java --version + + - name: Build with Maven + run: mvn -B -DskipTests clean package + + - name: mvn test + run: mvn test + + security-scan: + runs-on: ubuntu-latest + needs: build + steps: + - uses: actions/checkout@v4 + - name: Trivy Installation + run: | + sudo apt-get install wget apt-transport-https gnupg lsb-release + wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null + echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list + sudo apt-get update + sudo apt-get install trivy + + - name: Trivy scan + run: trivy fs --format table -o fs-report.json . + + - name: gitleaks scan + run: sudo apt install gitleaks -y + + - name: Gitleaks Code Scan + run: gitleaks detect source . -r gitleaks-report.json -f json + + sonar-quality-gate: + runs-on: ubuntu-latest + needs: security-scan + steps: + - name: checkout the code + uses: actions/checkout@v4 + + - name: set up jdk 17 + uses: actions/setup-java@v4 + with: + java-version: 17 + distribution: 'temurin' + cache: maven + + - name: Cache SonarQube packages + uses: actions/cache@v4 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + - name: Cache Maven packages + uses: actions/cache@v4 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Build and analyze + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=devopslearnc_Github-Actions-Examples + + buils_docker_image_and_push: + runs-on: ubuntu-latest + needs: sonar-quality-gate + steps: + - uses: actions/checkout@v4 \ No newline at end of file diff --git a/.github/workflows/multi-stage.yml b/.github/workflows/multi-stage.yml index 7f83525..aad688a 100644 --- a/.github/workflows/multi-stage.yml +++ b/.github/workflows/multi-stage.yml @@ -5,10 +5,10 @@ on: - dev pull_request: branches: - - main + - dev jobs: push_event_check: runs-on: ubuntu-latest steps: - name: check the dev - run: echo "matched to dev branch" \ No newline at end of file + run: echo "matched to dev branch date" \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1e9ed41 --- /dev/null +++ b/pom.xml @@ -0,0 +1,77 @@ + + 4.0.0 + com.mycompany.app + my-app + jar + 1.0-SNAPSHOT + my-app + https://maven.apache.org + + + org.junit.jupiter + junit-jupiter-api + 5.12.2 + test + + + + UTF-8 + murali-sonar-practice + https://sonarcloud.io + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.14.0 + + 17 + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.4.2 + + + + true + lib/ + com.mycompany.app.App + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + 3.5.0 + + + enforce-maven + + enforce + + + + + [3.9.2,) + + + [17,) + + + + + + + + + \ No newline at end of file diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..ea440db --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,3 @@ +sonar.projectKey=devopslearnc_Github-Actions-Examples +sonar.organization=devopslearnc +sonar.host.url=https://sonarcloud.io diff --git a/src/com/mycompany/app/App.java b/src/com/mycompany/app/App.java new file mode 100644 index 0000000..ef418dc --- /dev/null +++ b/src/com/mycompany/app/App.java @@ -0,0 +1,15 @@ +package com.mycompany.app; +public class App { + + private static final String MESSAGE = "Hello World!"; + + public App() {} + + public static void main(String[] args) { + System.out.println(MESSAGE); + } + + public String getMessage() { + return MESSAGE; + } +} \ No newline at end of file diff --git a/src/com/mycompany/app/AppTest.java b/src/com/mycompany/app/AppTest.java new file mode 100644 index 0000000..7121f5a --- /dev/null +++ b/src/com/mycompany/app/AppTest.java @@ -0,0 +1,22 @@ +package com.mycompany.app; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +public class AppTest +{ + @Test + public void testAppConstructor() { + App app1 = new App(); + App app2 = new App(); + assertEquals(app1.getMessage(), app2.getMessage()); + } + + @Test + public void testAppMessage() + { + App app = new App(); + assertEquals("Hello World!", app.getMessage()); + } +}