jenkinsfile example

jenkinsfile example

Jenkinsfile contain a pipeline configuration. Jenkins will look this file and based on the steps it will execute the build. below Jenkinsfile example are show how to use them.

Jenkinsfile: and Pipelines  Together is a new concept. This file creates in your repo, which contains your pipeline configuration. Jenkins will look for this file in your branches and execute the build according to the stages defined in there.

This is possible to have your pipeline configuration together with your project and under version control.

Pipeline Groovy

In CentOS and Linux Jenkinsfile placed  under the default folder (replace “onclick360” with the name of your job):

/var/lib/jenkins/workspace/onclick360@script/Jenkinsfile

jenkinsfile example :-

1. Jenkinsfile for Hello World.

#!/usr/bin/env groovy
node {
echo 'Hello World'
}

2. Groovy variable into a string argument

#!/usr/bin/env groovy
node {
stage('Execute shell') {
var = "name"
sh "echo Hello ${var}"
}
}

3. Jenkinsfile Build and test a Maven project

# Jenkinsfile
# Maven project Build and test.
#!/usr/bin/env groovy

node { git url: 'http://github.com/username/repolist.git' def mvn = tool 'M3' sh "${mvn}/bin/mvn -B -Dmaven.test.failure.ignore verify" step([$class: 'JUnitResultArchiver', testResults: '**/target/foobar/TEST-*.xml']) }

4. Jenkinsfile Verify a Maven project.

# Jenkinsfile 
# Maven project node
#!/usr/bin/env groovy
{ git url: 'http://github.com/username/repolist.git' def mvn = tool 'M3' sh "${mvn}/bin/mvn -B verify" }

5. Run build in parallel mode.

#!/usr/bin/env groovy
node ( 'slave1' ) {
parallel (
linux: {
sh './linux_script.bash -t linux_amd64'
},
windows: {
sh './windows_script.bash -t windows_amd64'
}
)
    }

 

Jenkinsfile provides a number of immediate advantages:

  • Code audit and review on the pipeline.
  • Once Jenkinsfile tested for some build, it can be reusable to another project.
  • Jenkinsfile can be viewed and modify if needed by others members of projects.
  • All code use stage by stage process like Compile stage, Build stage, Deploy stage and testing stage and this all stage can automate at one place.
  • It uses Groovy language which is easy to understand and can do a lot of automation which may difficult if use Jenkins build GUI.
  • By using Groovy language we can use the simple syntax to use parallel branches for pipelines.

Hope you enjoy this Article.

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *