One More interesting groovy snippet to make us understand step synax
- Steps use the shorthand notation for mapping syntax in Groovy
def printmessage(Map config=[:]){
println("message is ${config.message} and author is ${config.author}")
}
printmessage message: "How are you", author: "Khaja"
printmessage message: "Doing good", author: "Anonymous", test: true
- Now Lets Navigate to Here for git steps
git "https://github.com/shaikkhajaibrahim/spring-petclinic.git"
# only required option
git url: "https://github.com/shaikkhajaibrahim/spring-petclinic.git", branch: "Dev"
- For complete list of steps Refer Here
Triggers using Pipelines
- Build Triggers used in the free style project can be defined from pipeline as well
- Refer Here for official docs
- In this lets assume we have a project in jenkins called as dummy. lets write the declarative pipeline to trigger the current project jenkins build after succesful execution of dummy project, the syntax of pipleine will be
pipeline {
agent any
triggers {
upstream(upstreamProjects: 'dummy', threshold: hudson.model.Result.SUCCESS)
}
stages {
stage('Source'){
steps {
git 'https://github.com/dummyrepos/spring-petclinic.git'
}
}
stage('Package'){
steps {
sh 'mvn package'
}
}
}
}
- The console output of dummy project looks like
- If we navigate to our projects (declarative spc) view
Build Jobs from post build of upstream
- From free style project Navigate to postbuild and then build the projects
- From Jenkinsfile you can use build step of pipeline
Taking inputs from users in between build
- Lets take the input from the user to continue to next step
- Refer Here for documentation
- pipeline after making changes
pipeline {
agent {label 'MASTER'}
triggers {
upstream(upstreamProjects: 'dummy', threshold: hudson.model.Result.SUCCESS)
}
stages {
stage('Source'){
steps {
git 'https://github.com/dummyrepos/spring-petclinic.git'
}
}
stage('Package'){
steps {
sh 'mvn package'
input 'continue to next step?'
archiveArtifacts 'target/*.jar'
}
}
}
}
- Screenshot of approval
Parameters in Jenkins
- In Free style projects
- In declarative pipeline, we have parameters section Refer Here, pipleline after adding one string parameter looks as shown below
pipeline {
agent {label 'MASTER'}
triggers {
upstream(upstreamProjects: 'dummy', threshold: hudson.model.Result.SUCCESS)
}
parameters {
string(name: 'BRANCH_FOR_BUILD', defaultValue: 'master', description: 'Enter the branch to be built')
}
stages {
stage('Source'){
steps {
git url: 'https://github.com/dummyrepos/spring-petclinic.git', branch: "${params.BRANCH_FOR_BUILD}"
}
}
stage('Package'){
steps {
sh 'mvn package'
}
}
}
}
-
This pipeline will change the jenkins build of the project as shown below
-
Also refer to the options section Refer Here
Frequently used Steps in Jenkins
- Notifications: Refer Here
- Stash Refer Here and unstash Refer Here
- Refer Below example for stash and unstash
pipeline { agent any stages { stage('build') { agent {label: 'MAVEN'} steps { git url: 'https://github.com/dummyrepos/spring-petclinic.git' sh 'mvn package' stash name: 'spc-jar', includes: 'target/spring-petclinic.jar' } } stage('deploy') { agent {label: 'Ansible'} steps { unstash 'spc-jar' git url: 'https://github.com/dummyrepos/spring-petclinic-ansible.git sh 'ansible-playbook -i hosts deploy.yaml' } } } }
Build Promotions using Maven Repository (Artifactory)
-
Refer Here for configuring artifactory
-
Project which we build from ci/cd will create some artifacts and they need to preserved for future usage.
-
But how do i preserve the builds, we need repository for storing binary file. For that there are lot of options
- Network Drives (Shared Drives) => Every day after build is completed in jenkins copy the build to some network drive
- Cloud Storage like S3 or Azure blob storage
- JFrog(Artifactory)
- Nexus
-
In this series we will be using Artifactory, Refer Here for installation
-
So our story with artifacts look as shown below
-
Navigate to artifactory post installation on port 8081 and you will be asked to reset password and then select the repository
-
Setting up for settings.xml to be downloaded
-
Make changes in settings.xml and copy the file to ~/.m2/settings.xml and do mvn package for some project
-
Maven goal install will copy the artifact and pom.xml into ~/.m2/repository/<suitable> and maven goal deploy will copy the the artifact and pom.xml in the remote repository
- Install will copy into ~/.m2 (env M2_HOME)
- To deploy the artifact we need to make changes in pom.xml Refer here
- Install will copy into ~/.m2 (env M2_HOME)
-
Doing the same (Resolving and deploying from jenkins) Refer the Document
-
Refer Here for pipeline Jenkins job and Refer Here for examples
Remaining Topics (Contd on 23/May)
- Sonar Qube intergration
- Git Branching Strategy and other topics of git
- Azure DevOps