【问题标题】:Jenkins Job DSL create Seed Job with Configuration as a Cod PluginJenkins Job DSL 使用配置作为 Cod 插件创建种子作业
【发布时间】:2021-10-20 03:50:48
【问题描述】:

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

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

【问题讨论】:

    标签: jenkins jenkins-job-dsl jenkins-confuration-as-a-code


    【解决方案1】:

    是的,完全有可能。

    but this plugin use Job DSL to create jobs - 是的,但它假定已经安装了 Job DSL 插件。

    让我复制我使用的 Jenkins 部署的 sn-p:

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

    【讨论】:

    • 这似乎是我需要的,你也可以分享 groovy 文件的例子吗?
    • @AndriyKopachevskyy 添加了一个示例。
    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多