Jenkinsfile introduce
1. Jenkinsfile 基本语法
1.1. Defining a Pipeline
Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins.
A Pipeline can be created in one of the following ways:
- Through Blue Ocean - after setting up a Pipeline project in Blue Ocean, the Blue Ocean UI helps you write your Pipeline’s
Jenkinsfileand commit it to source control. - Through the classic UI - you can enter a basic Pipeline directly in Jenkins through the classic UI.
- In SCM - you can write a
Jenkinsfilemanually, which you can commit to your project’s source control repository.
1.2. Using Environmental Variables
Environment variables can be set globally or per stage.
Setting environment variables per stage means they will only apply to the stage in which they’re defined.
1 | // setting environment variables globally |
1.3. Agent
In declarative pipelines the agent directive is used for specifying which agent/slave the job/task is to be executed on. This directive only allows you to specify where the task can be executed (On which agent, slave, label or docker image)
1 | agent { kubernetes { label 'my-defined-label' inheritFrom 'my-defined-build' containerTemplate { name 'build-name' image 'my-docker-image:latest' workingDir 'my/working/dir' } } } |
1.4. Parameters
The parameters directive provides a list of parameters which a user should provide when triggering the Pipeline. The values for these user-specified parameters are made available to Pipeline steps via the ‘params' object.
1 | String parameter: |
1.5. Gerrit Triggers
This plugin integrates Jenkins to Gerrit code review for triggering builds when a “patch set” is created.
Trigger configuration in pipeline job:
In the “Build Triggers” section of your Job configuration page; tick “Gerrit event”
Specify what type of event(s) to trigger on:
- Draft Published: Sent when a change moves from draft state to new. (only available in version 2.5 or higher of Gerrit).
- Patchset Created: Sent when a new patchset arrives on a change. Before version 2.6.0, this was the only event you could trigger on.
- Change Merged: Sent when a change is merged on the Gerrit server.
- Comment Added: Sent when a comment is added to a change. Which category and value to trigger on can be configured. The available categories can be configured in the server settings for the plugin.
- Ref Updated: Sent when a ref is updated on the Gerrit server, i.e. someone pushes past code review.
Trigger configuration in Jenkinsfile:
Jenkinsfile will overwrite the configuration done in pipeline job.
1 | triggers { |
2. Jenkinsfile simple
2.1. @Library
1 | @Library(['scm@<pipeline-utils branch or tag>']) _ |
Example:
1 | @Library(['scm@container-stable-2.x', 'commonLibs@0.0.3']) _ |
2.2. extraOnboardingImages
List containing the extra images that need to onboard to Open Edge server.
列出包含需要附加到Open Edge服务器的其他图像的列表。
Example:
1 | Map containers = [ |
2.3. tersyParams
platform: Optional arguments
String containing platform type of NESC node. 包含NESC节点的平台类型的字符串
Possible values:
- any
- TPI3-like
- NCIR-like
Default value: NCIR-like
When test is executed in real HW tersyParams will define which kind of environment is selected
在实际硬件中执行测试时,tersyParams将定义选择哪种环境
Map containing information that is used for test environment reservation from Tersy.
映射包含用于从Tersy保留测试环境的信息。
Duration and pool or tenant attributes in tersyParams are mandatory if RUN_ON_HW is set to true.
如果将RUN_ON_HW设置为true,则tersyParams中的duration和pool或tenant属性是必需的。
Other attributes are optional.
Tersy parameters:
For details on supported parameters see documentation of tersycli create command.
有关支持的参数的详细信息,请参见tersycli create命令的文档。
Only long format of the named arguments are supported, use for example ‘duration’ instead of ‘d’.
Note that ‘reservation_name‘ is not needed as it is automatically set to Jenkins job name.
Default value of queue_timeout parameter is 1 hour if priority is given.
Default value of env_type is CAAS.
Additionally a specific tersycli version can be requested with tersycliVersion, 2.3.5 is used by default.
仅支持长格式的命名参数,例如使用“duration”代替“ d”。
1 | Map tersyParamsMvp = [ |
2.4. builds
1 | List builds = [ |
2.5. agent
agent: Optional arguments
Jenkins agent where the test is executed. 执行测试的Jenkins代理。
Some possible values are:
single-node-container-k8s-dpdkfor 8 core cloud-development environment with dpdk supportsingle-node-pod-k8s-dpdkfor 16 core cloud-development environment with dpdk supportmulti-node-pod-k8s-dpdkfor 1+1 node, 13 core each, container-infra-system environment with dpdk support- TODO: IT kubernetes cluster agent
Default value for the parameter is single-node-container-k8s-dpdk
cleanCommand: Optional arguments
Optional shell command to run before and after test to clean up resources.
在测试之前和之后运行的可选shell命令,以清理资源。
要注意的是,pipeline检测v2和v3的Helm命令,并将它们分别暴露为环境变量$ {HELM2}和$ {HELM3}。因此最好在shell脚本中使用这些变量,以避免在’helm’命令更改为指向helm3而不是旧的helm2时更改脚本。
helmName: Mandatory arguments
Name of the helm chart to be tested and published. Helm chart will be cloned to a directory with this name regardless what the repository name is. All scripts are executed in this directory.
要测试和发布的helm chart的名称, 无论存储库名称是什么,Helm chart都会被克隆到具有该名称的目录中。所有脚本都在此目录中执行。
To allow for more than one pipeline to be executed for same helm chart, pipeline appends automatically Jenkins build id and branch name to the helm name defined in the env_vars file.
为了允许对同一helm chart执行多个管道,管道会自动将Jenkins构建ID和分支名称附加到env_vars文件中定义的helm名称。
deployCommand: Optional arguments
Optional shell command to deploy the helm chart. Default value is ci/deploy.sh
可选的shell命令,用于部署helm chart。默认值为ci / deploy.sh, 应该是这里的command
testCommand: Optional arguments
Optional shell command to deploy the helm chart. Default value is ci/test.sh
可选的shell命令,用于部署helm chart。默认值为ci / test.sh
logsCommand: Optional arguments
Optional shell command to run at the end of the pipeline to collect logs. Default value is gather-artifacts.sh Will be executed always even if deploy or test fails. To ensure that all logs are collected make sure that the command handles error cases correctly.
可选的shell命令在pipeline末端运行以收集日志。失败后的log收集,为确保收集所有日志,请确保该命令正确处理错误情况。
If using bash script you can use +e flag to ignore errors:
如果使用bash脚本,则可以使用+ e标志忽略错误: #!/bin/bash +e
1 | List tests = [ |
2.6. jobParams
1 | List jobParams = [ |
2.7. Notification Recipients
Specifies if email or office365 (yammer/teams) notification should be sent. By default no notifications are sent.
For each of the notification type define a Map which has webhookUrl and/or emailAddresses defined.
An example where all the notifications are sent:
notifyMainBranchFailure: Optional arguments
Set to receive notifications when pipeline failed on the branch defined by mainBranch. Do not define to not receive these notifications.
设置为在mainBranch定义的分支上的pipeline 发生故障时接收通知。别定义为不接收这些通知。
notifyTagSuccess: Optional arguments
Set to receive notifications of successful publishing of helm chart. Do not define to not receive these notifications.
notifyTagFailure: Optional arguments
Set to receive information when pipeline failed when helm chart was tried to be published. Do not define to not receive these notifications.
webhookUrl: Optional arguments
Provide the webhook from yammer or teams to get notifications sent there. In yammer you can add Jenkins app from the “Add or Remove Apps” link. In teams this is done via the “Connectors” link for the channel.
emailAddresses: Optional arguments
Comma separated list of email addresses to sent notifications to.
1 | Map defaultNotificationRecipients = [ |
1 | Map notifications = [ |
2.8. containerPipeline
calling the pipeline from container Jenkinsfile:
gitlabConnection: Mandatory arguments
Tells Jenkins which gitlab connection to use when reporting. Valid values are:
- ‘IT gitlab’ for gitlabe1.ext.net.nokia.com
- ‘Gitlabe2’ for gitlabe2.ext.net.nokia.com
tagPublishRegexp: Optional arguments
By default all tags trigger image publishing if publishTags is set to true. With this argument it is possible to filter which tags are published. The format is regular expression. Some examples:
默认情况下,如果publishTags设置为true,则所有标签都会触发图像发布。使用此参数,可以过滤发布哪些标签。格式为正则表达式。
^\\d+\\.\\d+\\.\\d+$would match only semVer formatted tags e.g. 1.2.0 –将仅匹配semVer格式的标签,例如1.2.0^release-.*would match only tags which start with release- prefix –将仅匹配以release-前缀开头的标签^\\d+\\.\\d+\\.\\d+([-_]hb)?$when publishing for Heart Beat build –在发布Heart Beat版本时
1 | containerPipeline containers: containers, |
2.9. Set environment variables
Some environment variables name and value are printed to ./env_vars file. Scripts like config.deployCommand, config.testCommand, config.cleanCommand can source env_vars file and use the listed env variables.
一些环境变量的名称和值会打印到./env_vars文件中。诸如config.deployCommand,config.testCommand,config.cleanCommand之类的脚本可以获取env_vars文件并使用列出的env变量。
Example env_vars:
1 | 17:59:24 + cat env_vars |
RUN_ON_OPEN_EDGE env variable is deprecated. Scripts should be changed to use RUN_ON_HW instead of RUN_ON_OPEN_EDGE.
不赞成使用RUN_ON_OPEN_EDGE环境变量。应将脚本更改为使用RUN_ON_HW而不是RUN_ON_OPEN_EDGE。
BUILD_ID is the Jenkins job build number. This and BRANCH_NAME are also appended to the HELM_NAME variable so that it does not conflict with other pipelines running tests for the same helm chart. The name is limited to 40 characters. This allows container developers to add possible pre- and postfixes, but the name should not exceed the maximum allowed for helm release name (53 characters).
BUILD_ID变量和BRANCH_NAME也附加到HELM_NAME变量中,以使其与运行针对同一helm chart的测试中的其他pipelines不冲突。
2.10. 参考链接
https://www.w3cschool.cn/jenkins/jenkins-jg9528pb.html
https://confluence.int.net.nokia.com/display/MPP/Jenkinsfile+Template