Categories
bigdata bigdata/flink bigdata/spark bigdata/topics cicd cicd/jenkins cicd/travis data-science docker git kubernetes kubernetes/minikube kubernetes/prometheus kubernetes/tools shell springbootTags
Summary Tips
- use workflow control to make your pipeline much more easy-understanding.
- use parameters to make you pipeline easy to migrate.
- use global variables to define your const variables.
1. Use Parameters to get your pipeline easy to migrate
1 | pipeline{ |
2. How to deal with errors
1. Error occurs during executing commands
Jenkins TRY_CATCH_FINALLY Syntax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
stage('Example') {
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed'
throw
}
/***
finally {
echo 'finally doing some clean jobs'
}
***/
}
2. Global Errors Handlers
use `post` [( Jenkins Post Syntax )](https://jenkins.io/doc/book/pipeline/syntax/#post) to control the global workflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pipeline {
agent any
stages {
stage('Example') {aborted
steps {
echo 'Hello World'
}
}
}
post {
always {
echo 'Always say Hello again!'
}
unsuccessful {
echo 'Doing some clean jobs'
}
aborted {
// send some alertsaborted
}
}
}
3. How to use the custom workspace
instead of $HOME/.jenkins/workspace
Jenkins Customs Workspace Syntax
1 | agent { |
4. How to define your global variables
to avoid duplicate codes?
1 | def current_time |
5. How to set your environment variables
?
Jenkins Environment Variables Syntax
There are two scopes of environment variables you can specify.
throughout the pipeline
take effect only in one stages.stage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19pipeline {
agent any
// throughout the pipeline
environment {
CC = 'clang'
}
stages {
stage('Example Username/Password') {
// only in one stage
environment {
SERVICE_CREDS = credentials('my-prefined-username-password')
}
steps {
sh 'echo "$SERVICE_CREDS"'
sh "echo \$SERVICE_CREDS"
}
}
}
}
6. Parallel steps
use parallel
feature in pipeline.stages.stage
1 | pipeline{ |
7. function usages
You can’t get variables defined in your pipelines in functions. But you can get access to the env parameters.
1 |
|