Jenkinsfile Explained with Example
Agent
Let’s start with agent
where certain phase will be executed. It can be the entire pipeline
or certain stage
. At top level agent, we set it to none
so we need to set each stage what agent we want to use.
Let’s discus about the kind of agent below:
agent {
docker {
image 'alxibra/forstok-apigateway:0.0.1'
label 'slave'
}
}
It means we run our stage in docker environment with base image alxibra/forstok-apigateway:0.0.1
. label
means which server with certain label you want to execute. we have 2 servers to run Jenkins, we label our server with master
and slave
.
For more information about agent you can read the documentation about agent.
Environment
Nothing much we can explain here, because it is explained itself. But if you wonder where credential come from. We can go to Manage Jenkins > Security > Manage Credentials
AWS_KEY = credentials('AWS_KEY')
When
when
directive is used to which condition the stage should run. the when
condition below has meaning that the stage always running except in branch master
when {
not {
branch 'master'
}
}
For more information see this documentation.
Steps
steps
what is to be done to complete the stage
steps('test') {
sh 'bundle exec rspec'
}
The script above we need to run command bundle exec rspec
. We can have more than one command each steps
. for current condition we just need one command.
Parallel
We can run multiple stages parallelly. First we need to make parent stage
, inside the stage
put parallel
directive. Inside parallel
put how many stage
s you want to.
stage('QA'){
paralle {
stage('linter'){
.......
}
stage('test') {
........
}
}
}
for more information see this documentation
Post
post
section is additional step after certain stage is done or fail. We define 2 conditions the success
and failure
. They both send notification to our slack the status. More about conditions in post
see the documentation.
Now I try to explain the what is Jenkinsfile is doing in the file above

When branch is not master
we just run linter
and test parallelly without executing the build image
and deploy to production
.

Now we want to release our application, we need to deploy it to production. First we need to build the application, after building is completed now we can deploy it to production. if status deploy is success or failure, jenkins will send the status to slack.