diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 000000000..d657a63b2 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,18 @@ +name: Docker Image CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build the Docker image + run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..e0f15db2e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic" +} \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index 56745c21a..67b6dc449 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,11 +2,34 @@ pipeline { agent any stages { - stage('Build Artifact') { - steps { - sh "mvn clean package -DskipTests=true" - archive 'target/*.jar' //so that they can be downloaded later - } - } + + stage('Build Artifact - Maven') { + steps { + sh "mvn clean package -DskipTests=true" + archive 'target/*.jar' + } + } + + stage('Unit Tests - JUnit and Jacoco') { + steps { + sh "mvn test" + } + post { + always { + junit 'target/surefire-reports/*.xml' + jacoco execPattern: 'target/jacoco.exec' + } + } + } + + stage('Docker Build and Push') { + steps { + withDockerRegistry([credentialsId: "docker-hub", url: ""]) { + sh 'printenv' + sh 'docker build --tag devsecops9849/devsecopspipeline:""$GIT_COMMIT"" .' + sh 'docker push devsecops9849/devsecopspipeline:""$GIT_COMMIT""' + } + } } -} \ No newline at end of file + } +} diff --git a/pom.xml b/pom.xml index 8e71c7ac2..8661ebca0 100644 --- a/pom.xml +++ b/pom.xml @@ -45,8 +45,28 @@ org.springframework.boot spring-boot-maven-plugin - + + + org.jacoco + jacoco-maven-plugin + 0.8.5 + + + + prepare-agent + + + + report + test + + report + + + + - - + + + \ No newline at end of file diff --git a/src/test/java/com/devsecops/NumericApplicationTests.java b/src/test/java/com/devsecops/NumericApplicationTests.java index 3e0ae20a4..8d6eb9011 100644 --- a/src/test/java/com/devsecops/NumericApplicationTests.java +++ b/src/test/java/com/devsecops/NumericApplicationTests.java @@ -1,7 +1,6 @@ package com.devsecops; -import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; @@ -21,35 +20,36 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -//import org.junit.Test; + +import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -@RunWith(SpringRunner.class) +/*~~(Unable to find runtime dependencies beginning with: 'junit-jupiter-api')~~>*/@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc -public class NumericApplicationTests { +class NumericApplicationTests { @Autowired private MockMvc mockMvc; @Test - public void smallerThanOrEqualToFiftyMessage() throws Exception { + void smallerThanOrEqualToFiftyMessage() throws Exception { this.mockMvc.perform(get("/compare/49")).andDo(print()).andExpect(status().isOk()) .andExpect(content().string("Smaller than or equal to 50")); } @Test - public void greaterThanFiftyMessage() throws Exception { + void greaterThanFiftyMessage() throws Exception { this.mockMvc.perform(get("/compare/51")).andDo(print()).andExpect(status().isOk()) .andExpect(content().string("Greater than 50")); } - + @Test - public void welcomeMessage() throws Exception { - this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()); + void welcomeMessage() throws Exception { + this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()); } - + } \ No newline at end of file