我的任務是啟動 Jenkins 并在安裝程序中自動創建 Job DSL Seed 作業。這意味著我必須使用 Configuration 作為代碼插件,但是這個插件使用 Job DSL 來創建作業,這意味著我必須使用 Job DSL 作業創建 Job DSL 作業,以前有人嘗試過嗎?這幾乎是可能的嗎?
可能的選項是將種子作業匯入為 XML,但此功能在最新的 Jenkins helm chart 中已被棄用。
uj5u.com熱心網友回復:
是的,這是完全可能的。
but this plugin use Job DSL to create jobs - 是的,但它假設已經安裝了 Job DSL 插件。
讓我復制我使用的 Jenkins 部署片段:
- name: Get tag of the latest version of Plugin installation manager tool
ansible.builtin.raw: |
git ls-remote https://github.com/jenkinsci/plugin-installation-manager-tool.git \
| grep -E "refs/tags/[0-9] \.[0-9] \.[0-9] $" | tail -n1 | cut -d / -f 3
register: plugin_manager_version
# https://github.com/jenkinsci/plugin-installation-manager-tool#getting-started
- name: Download Plugin installation manager tool
ansible.builtin.get_url:
url:
"https://github.com/jenkinsci/plugin-installation-manager-tool/releases/\
download/{{ plugin_manager_version.stdout | trim }}/jenkins-plugin-manager-{{ plugin_manager_version.stdout | trim }}.jar"
dest: /tmp/jenkins/jenkins-plugin-manager.jar
- name: Copy a list of plugins to install
ansible.builtin.copy:
src: plugins.txt
dest: /tmp/jenkins/plugins.txt
mode: 0444
owner: jenkins
group: jenkins
- name: Install the latest version of plugins
ansible.builtin.command: |
java -jar /tmp/jenkins/jenkins-plugin-manager.jar --war /usr/lib/jenkins/jenkins.war -f /tmp/jenkins/plugins.txt -d /var/lib/jenkins/plugins
- name: Set ownership of plugins
ansible.builtin.file:
path: /var/lib/jenkins/plugins
owner: jenkins
group: jenkins
mode: 0644
recurse: true
- name: Set ownership of plugins folder
ansible.builtin.file:
path: /var/lib/jenkins/plugins
owner: jenkins
group: jenkins
mode: 0755
recurse: false
- name: Copy Jenkins configuration files
ansible.builtin.template:
src: "{{ item }}.yaml.j2"
dest: "/var/lib/jenkins/casc_configs/{{ item }}.yaml"
mode: 0644
owner: jenkins
group: jenkins
loop: "{{ ['credentials', 'general', 'seed-job', 'users'] | flatten(1) }}"
- name: Restart Jenkins
ansible.builtin.systemd:
name: jenkins
state: restarted
enabled: true
在plugins.txt有,除其他外,插件configuration-as-code和job-dsl。
而且seed-job.yaml是這樣的:
jobs:
- script: >
freeStyleJob('Seed Job') {
description('Synchronizes Jenkins jobs with ones in my-repo/jobs folder.')
displayName('Seed Job')
scm {
git {
remote {
name('Jenkins jobs')
url('https://github.com/my-repo.git')
credentials('GITHUB_CREDENTIALS')
}
branch('master')
}
}
triggers {
pollSCM {
scmpoll_spec('* * * * *')
}
}
steps {
dsl {
external "jobs/**/*.groovy"
removeAction("DELETE")
removeViewAction("DELETE")
}
}
}
現在種子作業將自動從您的存盤庫匯入所有作業。
一個示例 Job DSL 如下所示:
freeStyleJob('System Cleanup') {
displayName('System Cleanup')
description('Remove unused Docker stuff and golang cache once a month.')
label('continuous-integration')
triggers {
scm('H 0 1 * *')
}
steps {
shell('docker system prune --all --volumes --force')
shell('go clean -cache -modcache -testcache')
}
}
但是當然您可以為其他型別的作業(例如 multibranchPipeline)撰寫作業 DSL。請參閱Jenkins 實體中的API 查看器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/326752.html
標籤:詹金斯 jenkins-job-dsl 詹金斯配置即代码
上一篇:在Powershell中拆分字串
