Jenkins Job DSL创建种子作业,配置为Cod插件



我的任务是启动Jenkins并在安装期间自动创建作业DSL种子作业。这意味着我必须使用配置作为代码插件,但是这个插件使用作业DSL来创建作业,这意味着我必须使用作业DSL作业来创建作业DSL作业,以前有人尝试过吗?这有可能吗?

可能的选项是将种子作业导入为XML,但这个功能在最新的Jenkins helm chart中已被弃用。

是的,完全有可能。

but this plugin use Job DSL to create jobs-是的,但它假设,工作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-codejob-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")
}
}
}

现在,种子作业将自动从存储库导入所有作业。

作业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 Viewer。

相关内容

  • 没有找到相关文章