Allows to create pipelines to run unit/integration tests, code quality check, package your apps.
Jenkins - CI/CD tool.
https://hub.docker.com/r/jenkins/jenkins
https://github.com/jenkinsci/docker/blob/master/README.md
docker-compose up- Go to
localhost:8081and paste a passwordee85af5a404b467bb564eaf755864ccf. - Install suggested plugins.
- Create First Admin User.
- Manage Jenkins -> Global Tool Configuration -> Maven and Docker
- Create job -> Pipeline -> Poll SCM -> * * * * * (every minute will check if there are changes and run)
- Pipeline -> Pipeline script from SCM -> Git -> https://github.com/vyahello/devops-master-class -> ci_cd/jenkins/currency-exchange/Jenkinsfile.groovy (Script Path)
- We have created http://localhost:8081/job/jenkins-devops-pipeline pipeline
- Click 'Build Now'
node is a machine that runs your pipeline.
// Scripted pipeline
// runs your pipeline
node {
echo "Build"
echo "Test"
echo "Integration Test"
}// Declarative pipeline
pipeline {
// declare where your build is going to run, you can use docker image as agent
agent any
// add stages
stages {
stage('Build') {
steps {
echo "Build"
}
}
stage('Test') {
steps {
echo "Test"
}
}
stage('Integration Test') {
steps {
echo "Integration Test"
}
}
}
}Add post action
// action to execute after all stages
post {
always {
echo "Always run"
}
success {
echo "Run when you are successful"
}
failure {
echo "Run when you are fail"
}
} agent {
docker {
image 'maven:3.6.3'
}
}
stages {
stage('Build') {
steps {
sh "mvn --version"
echo "Build"
}
}https://www.jenkins.io/doc/book/pipeline/
Use Jenkins Pipeline Syntax feature.
For example to checkout code in pipeline:
checkout(
[$class: 'GitSCM',
branches: [[name: '*/master']],
extensions: [],
userRemoteConfigs:
[[url: 'https://github.com/vyahello/devops-master-class']]]
)Retry 5 times
retry(5) {
// some block
} environment {
dockerHome = tool 'myDocker'
mavenHome = tool 'myMaven'
PATH = "$dockerHome/bin:$mavenHome/bin:$PATH"
Check pom.xml file as a dependency file.
surefire is for unit tests and failsafe is for integration tests.
<surefire.version>2.22.1</surefire.version>
<failsafe.version>2.22.1</failsafe.version><groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.version}</version>Give Jenkins docker creds via Credentials -> Global Credentials -> Add Credentials and store as dockerHub.
stage('Build Docker image') {
steps {
// docker build -t vyahello/currency-exchange:$env.BUILD_TAG
script {
dockerImage = docker.build('vyahello/currency-exchange:${env.BUILD_TAG}')
}
}
}
stage('Push Docker image') {
steps {
script {
// used from credentials
docker.withRegistry('', 'dockerHub') {
dockerImage.push();
dockerImage.push('latest');
}
}
}
}