Sample Jenkinsfile
This is a sample for a Jenkinsfile with following features in use:
- REST list parameter plugin usage for retrieving milestones from a gitlab project
- Parsing JSON from text parameter with Pipepline Utility Steps plugin to get the iid of a milestone
- Calling shell script with credentials
- Save passing credentials to shell script
agent any
parameters {
string(name: 'app', defaultValue: 'sample', description: 'Name of the app')
RESTList(
name: 'MILESTONE',
description: '',
restEndpoint: 'https://git.gitlab.com/api/v4/projects/234/milestones',
credentialId: 'CREDENTIAL_ID_IN_JENKINS',
mimeType: 'APPLICATION_JSON',
valueExpression: '$[*]',
displayExpression: '$.name',
cacheTime: 10, // optional
defaultValue: '', // optional
filter: '.*', // optional
valueOrder: 'ASC' // optional
)
choice(name: 'sampleChoice', choices: 'True\nFalse', description: 'Well just a sample choice. First entry is defaultValue.')
}
stages {
stage('Prepare landing zone') {
steps{
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'CREDENTIAL_ID_IN_JENKINS', url: 'git@git.gitlab.com:sample/repository.git']]])
}
}
stage('Excute whatever you like') {
steps {
script {
// parse the milestones retreived from gitlab
ms = readJSON returnPojo: true, text: env.MILESTONE
// save the milestone iid within the env
env.milestoneId = ms.iid
}
withCredentials([string(credentialsId: 'CREDENTIAL_ID_IN_JENKINS', variable: 'accesToken')]) {
println accesToken: accesToken
println app: app
println sampleChoice: sampleChoice
println milestoneId: milestoneId
sh ('printenv')
dir('out') {
sh ('pwd -P')
}
sh ('echo $accesToken $gitLabProjectMode $app $sampleChoice $milestoneId')
}
}
}
}
}