我正在尝试从管道内部自动创建Jenkins管道。
我有一个创建Bitbucket存储库并向其中提交一些代码(包括Jenkinsfile)的管道。
我需要向该管道添加另一步,然后为其创建管道构建,这将运行Jenkinsfile中的步骤。
我认为Jobs DSL应该能够处理此问题,但是我发现的文档非常稀少,而且我仍然不确定是否可以或如何做。
任何帮助将不胜感激。我想生成的Pipeline构建只需要有一个到存储库的链接,并被告知要在那里运行Jenkinsfile?
答案 0 :(得分:1)
是的,您的用例需要Job DSL。
编辑
pipeline {
agent {
label 'slave'
}
stages{
stage('stage'){
steps {
// some other steps
jobDsl scriptText: '''pipelineJob(\'new-job\') {
def repo = \'https://xxxxx@bitbucket.org/xxxx/dummyrepo.git\'
triggers {
scm(\'H/5 * * * *\')
}
definition {
cpsScm {
scm {
git {
remote {
url(repo)
credentials('bitbucket-jenkins-access')
}
branches(\'master\')
scriptPath(\'Jenkinsfile\')
extensions { }
}
}
}
}
}'''
}
}
}
}
文档-https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob-scm-git
答案 1 :(得分:1)
通过使用此python库jenins-job-builder,您可以轻松地从其他管道或任何其他远程位置创建所需的管道或自由样式的作业。
示例:
steps-1
python3 -m venv .venv
source .venv/bin/activate
pip install --user jenkins-job-builder
步骤2
完成上述操作后,创建2个文件,一个文件名为config.ini,另一个文件为job.yml。请注意-关于文件名没有严格的规则。取决于您。
包含的config.ini文件可以看起来像
[job_builder]
allow_duplicates = False
keep_descriptions = False
ignore_cache = True
recursive = False
update = all
[jenkins]
password = jenkins-password
query_plugins_info = False
url = http://jenkins-url.net
user = jenkins-username
如果您正在创建管道作业,那么job.yml文件可能看起来像
- job:
name: pipeline01
display-name: 'pipeline01'
description: 'Do not edit this job through the web!'
project-type: pipeline
dsl: |
node(){
stage('hello') {
sh 'echo "Hellow World!"'
}
}
步骤3
上述所有内容之后。调用以下命令
jenkins-jobs --conf config.ini update job.yml
注意-jenkins-jobs命令仅在遵循步骤1时可用