[英]Creating a Jenkins Pipeline build from a pipeline
我正在嘗試從管道內自動創建 Jenkins 管道構建。
我有一個管道,它創建一個 Bitbucket 存儲庫並向它提交一些代碼,包括一個 Jenkinsfile。
我需要向此管道添加另一個步驟,然后為其創建管道構建,這將運行 Jenkinsfile 中的步驟。
我認為 Jobs DSL 應該能夠處理這個問題,但是我為它找到的文檔非常稀少,我仍然不完全確定它是否可能或如何做到這一點。
任何幫助,將不勝感激。 我想生成的流水線構建只需要一個指向存儲庫的鏈接,並被告知在那里運行 Jenkinsfile?
是的,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
通過使用這個 python 庫jenins-job-builder,您可以輕松地從另一個管道或任何其他遠程位置創建預期的管道或自由式作業。
例子:
步驟 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 后才可用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.