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

Jenkins Job DSL create Seed Job with Configuration as a Cod Plugin

我的任务是启动 Jenkins 并在安装期间自动创建 Job DSL Seed 作业。 这意味着我必须使用 Configuration as a Code plugin,但是这个插件使用 Job DSL 来创建作业,这意味着我必须使用 Job DSL 作业创建 Job DSL 作业,以前有人试过吗?几乎可以吗?

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

是的,完全有可能。

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-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")
              }
          }
      }

现在种子作业会自动从您的存储库中导入所有作业。

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 Viewer