Pipeline 开发工具
选择任意pipeline类型的作业,点击“流水线语法”即可进入pipeline开发工具页面。
片段生成器
有些pipeline代码不是自己去写的,是要工具帮我们生成的,只需要知道怎么生成就行了
Jenkins会安装很多插件,有些插件会为你提供一些方法,也就是代码化的方法让你去使用片段生成器(下面就是安装好的插件为我们提供的所有功能)
流水线代码片段生成器, 非常好用。在这里可以找到每个插件以及Jenkins内置的方法的使用方法。使用片段生成器可以根据个人需要生成方法,有些方法来源于插件,则需要先安装相关的插件才能使用哦。
使用checkout生成器下载gitlab代码
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'dc735f39-1cef-4a12-a499-6c873b82adcc', url: 'http://192.168.179.100/devops/demo-hello-service.git']]])
pipeline {
agent any
stages {
stage('getCode') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'dc735f39-1cef-4a12-a499-6c873b82adcc', url: 'http://192.168.179.100/devops/demo-hello-service.git']]])
}
}
}
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on build-01 in /data/cicd/jenkinsagent/workspace/pipeline-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (getCode)
[Pipeline] checkout
The recommended git tool is: NONE
using credential dc735f39-1cef-4a12-a499-6c873b82adcc
Cloning the remote Git repository
Avoid second fetch
Checking out Revision dc647d4ef52aadabf75db582393156decc4ca6a3 (refs/remotes/origin/master)
Commit message: "Update version"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Cloning repository http://192.168.179.100/devops/demo-hello-service.git
> git init /data/cicd/jenkinsagent/workspace/pipeline-test # timeout=10
Fetching upstream changes from http://192.168.179.100/devops/demo-hello-service.git
> git --version # timeout=10
> git --version # 'git version 2.7.4'
using GIT_ASKPASS to set credentials git-lab-admin-user
> git fetch --tags --progress http://192.168.179.100/devops/demo-hello-service.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url http://192.168.179.100/devops/demo-hello-service.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f dc647d4ef52aadabf75db582393156decc4ca6a3 # timeout=10
Finished: SUCCESS
使用sh生成器生成shell脚本
pipeline {
agent any
stages {
stage('getCode') {
steps {
sh 'pwd'
}
}
}
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (getCode)
[Pipeline] sh
+ pwd
/var/jenkins_home/workspace/pipeline-test
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
声明式语法生成器
脚本方式和声明方式不同的地方:
如果使用脚本方式的语法和声明式差不多也是node{ stages{......} },但是用不到一些特性比如post标签,即构建后的操作,因为不支持脚本方式,说白了,脚本方式都得自己去写代码。
如果是脚本方式的,需要自己去写代码去判断流水线的状态,然后去做什么动作,声明方式是官方非常推荐的做法,更加结构化,更加简单,有些功能就已经帮我们实现好了。直接在里面写就行了,就不需要为了一些功能写额外的代码了。
可以在声明方式的里面嵌入脚本方式的代码,默认情况下是不支持的,怎么才可以支持,添加script,这样就可以写脚本,更加灵活
可以生成声明式流水线语法的语句块。
pipeline {
agent any
stages {
stage('getCode') {
steps {
script{
sh '''
date
pwd
echo "hh"
'''
}
}
}
}
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (getCode)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ date
Tue May 18 11:36:36 UTC 2021
+ pwd
/var/jenkins_home/workspace/pipeline-test
+ echo hh
hh
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
stages {
stage('build') {
steps {
// One or more steps need to be included within the steps block.
}
}
}