- Jenkins
- maven
- github
- ArgoCD
properties([ parameters([ string(name: 'sonar.projectKey', defaultValue: 'com.appsecco:dvja'), string(name: 'sonar.host.url', defaultValue: 'http://34.64.237.112:9000'), string(name: 'sonar.login', defaultValue: '608cacd6bb83c50712ebb34c4cba377c841cdebb'), string(name: 'ARGOCD_DOMAIN', defaultValue: '34.67.162.44:30357'), string(name: 'ARGOCD_PW', defaultValue: 'argo-cd-argocd-server-6d5f98cf57-wmf46'), string(name: 'ARGOCD_APP_NAME', defaultValue: 'test'), string(name: 'tag_num', defaultValue: ''), ]) ]) pipeline { environment { slack_channel = '#studying' REGISTRY = 'nasa1515/wonseok' REGISTRY_IP = '34.64.237.112' REGISTRYCREDENTIAL = 'harbor' DOCKER_IMAGE = '' TAG_NUM = '' } agent any tools { maven 'mvn' } stages { stage('Git clone') { steps { git 'https://github.com/nasa1515/dvwa-web-was.git' } } stage('Build Test') { steps { sh 'mvn clean package -Dcheckstyle.skip -Dspotbugs.skip -Dpmd.skip' } } stage ('Dependency-Check Analysis') { steps { sh '/var/lib/jenkins/dependency-check/bin/dependency-check.sh --scan `pwd` --format XML --out /var/lib/jenkins/workspace/ci-build-pipeline/dependency-check-report --prettyPrint' dependencyCheckPublisher pattern: 'dependency-check-report/dependency-check-report.xml' } } stage('Sonarqube and Quality gate') { options { timeout(time: 5, unit: 'MINUTES') retry(2) } steps { withSonarQubeEnv('SonarQube Server') { sh "mvn sonar:sonar" } script { qualitygate = waitForQualityGate() if (qualitygate.status != "OK") { currentBuild.result = "FAILURE" } } } } stage('Docker image build') { steps{ script { DOCKER_IMAGE = docker.build registry } } } stage('Docker image push to Harbor') { steps{ script { docker.withRegistry('http://$REGISTRY_IP', REGISTRYCREDENTIAL) { DOCKER_IMAGE.push('${BUILD_NUMBER}') DOCKER_IMAGE.push("latest") } } sh 'docker rmi $REGISTRY:latest' sh 'docker rmi $REGISTRY_IP/$REGISTRY:$BUILD_NUMBER' sh 'docker rmi $REGISTRY_IP/$REGISTRY:latest' } } stage('Anchore analyse') { steps { catchError(buildResult: 'SUCCESS', stageResult: 'SUCCESS') { writeFile file: 'anchore_images', text: '34.64.237.112/nasa1515/wonseok' anchore name: 'anchore_images' } } } stage('Push Yaml'){ steps { script{ try { git url: "https://github.com/nasa1515/dvwa-helm", branch: "main", credentialsId: "github" sh "rm -rf /var/lib/jenkins/workspace/${env.JOB_NAME}/helm-service/values.yaml" sh """ cd helm-service #!/bin/bash cat>values.yaml<<-EOF # Default values for ghost. # This is a YAML-formatted file. # Declare variables to be passed into your templates. replicaCount: 3 image: repository: nasa1515/dvwa-web-was tag: $BUILD_NUMBER pullPolicy: "" value: ec95c258266b8e985848cae688effa2b namespace: cd-test name: app: app EOF""" sh "cat /var/lib/jenkins/workspace/${env.JOB_NAME}/helm-service/values.yaml" withCredentials([usernamePassword(credentialsId: 'github', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) { sh """ git add --all . git commit -m "Deploy ${env.JOB_NAME} ${env.BUILD_NUMBER}" git push https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/nasa1515/dvwa-helm """ } env.pushYamlResult=true } catch (error) { print(error) echo 'Remove Deploy Files' withCredentials([usernamePassword(credentialsId: 'github', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) { sh """ git reset --hard HEAD^ git push --force https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/nasa1515/dvwa-helm """ } env.pushYamlResult=false currentBuild.result = 'FAILURE' } } } } stage('Argo Deploy'){ steps { script{ try { withEnv(["PATH=/usr/local/bin:$PATH"]) { sh""" #!/bin/bash expect << EOF spawn argocd login --grpc-web $ARGOCD_DOMAIN expect "WARNING: server certificate had error: x509: cannot validate certificate for 34.67.162.44 because it doesn't contain any IP SANs. Proceed insecurely (y/n)?" send "y\r"; expect "Username:" send "admin\r"; expect "Password:" send "$ARGOCD_PW\r"; expect eof EOF argocd app get $ARGOCD_APP_NAME argocd app sync $ARGOCD_APP_NAME """ } } catch (error) { print(error) currentBuild.result = 'FAILURE' } } } } } post { success { slackSend (channel: SLACK_CHANNEL, color: 'good', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})") } failure { slackSend (channel: SLACK_CHANNEL, color: 'bad', message: "FAILURE: '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})") } } }
pipeline { agent any tools { maven 'mvn' } stages { stage('Git clone') { steps { git 'https://github.com/nasa1515/dvwa.git' } } stage('Build Test') { steps { sh 'mvn clean package -Dcheckstyle.skip -Dspotbugs.skip -Dpmd.skip' } } } }