Bạn cũng đã thấy việc tạo các job trên jenkins thì khá là phức tạo và nếu bạn không có 1 công cụ (tool) thì bạn cần chỉnh sửa rất nhiều và công việc thì lập đi lập lại.
Bằng các sử dụng jenkins builder thì công việc sẽ as Code hơn và auto hơn!
Reference Links:
https://citizix.com/how-to-create-jenkins-jobs-using-jenkins-job-builder/
https://buildmedia.readthedocs.org/media/pdf/jenkins-job-builder/latest/jenkins-job-builder.pdf
https://medium.com/testableapple/jenkins-pipeline-job-builder-fast-growing-2c84591cfb60
1) Install Jenkins Job buidler
sudo apt-get update
sudo apt-get install jenkins-job-builder
Tạo 1 file là jenkins_jobs.ini
[job_builder] allow_duplicates=False update=all [jenkins] user=admin password=110bd6d23d89139451c8c54c811c21766f url=http://34.221.63.133:8080/ query_plugins_info=False
2.Creating a job with Jenkins job builder
2.1) single job
chúng ta sẽ có singlejob.yaml
với nội dung như sau:
singlejob.yaml — Creates the job as per the configurations in the yaml
- job: scm: - Simple-Test-Job builders: - shell: | TIME=`date` echo "It is ${TIME}" parameters: - string: name: BRANCH default: '{branch|master}' name: simple-test-job project-type: freestyle defaults: global description: 'Do not edit this job through the web!' disabled: false display-name: 'Fancy Test Job name' concurrent: true quiet-period: 5 block-downstream: false block-upstream: false retry-count: 3 build-discarder: daysToKeep: 3 numToKeep: 20 artifactDaysToKeep: -1 artifactNumToKeep: -1 - scm: name: Simple-Test-Job scm: - git: url: https://github.com/etowett/apisim.git branches: - main
Bây giờ bạn thực hiện tạo job thông qua command và template của jenkins job builder.
root@work-space-u20:~/mylab-helm-chart/jenkins-job-builder# jenkins-jobs --conf jenkins_jobs.ini update singlejob.yaml INFO:jenkins_jobs.cli.subcommand.update:Updating jobs in ['singlejob.yaml'] ([]) INFO:root:Caching type parameters of parameters = jenkins_jobs.modules.parameters:Parameters INFO:root:Caching type scm of scm = jenkins_jobs.modules.scm:SCM INFO:root:Caching type builders of builders = jenkins_jobs.modules.builders:Builders INFO:jenkins_jobs.builder:Number of jobs generated: 1 INFO:jenkins_jobs.builder:Creating jenkins job simple-test-job INFO:jenkins_jobs.cli.subcommand.update:Number of jobs updated: 1 INFO:jenkins_jobs.builder:Number of views generated: 0 INFO:jenkins_jobs.cli.subcommand.update:Number of views updated: 0
Đây là hỉnh anh trên jenkins
2.2) multiple jobs
Creates multiple jobs all having the same configuration as per the job template. All the jobs will be created at one go.
multiplejobtemplate.yaml
- project: name: Test-Job-Template-Temp jobs: - 'Test-Job-Template-{num}': num: 1 - 'Test-Job-Template-{num}': num: 2 - 'Test-Job-Template-{num}': num: 3 - job-template: disabled_var: command: df type: freestyle scm: - Test-Job-Template builders: - shell: | TIME=`date` echo "{num} It is $TIME" echo "Job-Name Test-Job-Template-{num}" parameters: - string: name: BRANCH default: '{branch|master}' name: 'Test-Job-Template-{num}' id: 'Test-Job-Template-{num}' project-type: freestyle defaults: global description: 'Do not edit this job through the web!' disabled: false display-name: 'Fancy Test Job name {num}' concurrent: true quiet-period: 5 block-downstream: false block-upstream: false retry-count: 3 build-discarder: daysToKeep: 3 numToKeep: 20 artifactDaysToKeep: -1 artifactNumToKeep: -1 - scm: name: Test-Job-Template scm: - git: url: https://github.com/etowett/apisim.git branches: - main
# jenkins-jobs --conf jenkins_jobs.ini update multiplejobtemplate.yaml INFO:jenkins_jobs.cli.subcommand.update:Updating jobs in ['multiplejobtemplate.yaml\u200a'] ([]) INFO:root:Caching type parameters of parameters = jenkins_jobs.modules.parameters:Parameters INFO:root:Caching type scm of scm = jenkins_jobs.modules.scm:SCM INFO:root:Caching type builders of builders = jenkins_jobs.modules.builders:Builders INFO:jenkins_jobs.builder:Number of jobs generated: 3 INFO:jenkins_jobs.builder:Creating jenkins job Test-Job-Template-1 INFO:jenkins_jobs.builder:Creating jenkins job Test-Job-Template-2 INFO:jenkins_jobs.builder:Creating jenkins job Test-Job-Template-3 INFO:jenkins_jobs.cli.subcommand.update:Number of jobs updated: 3 INFO:jenkins_jobs.builder:Number of views generated: 0 INFO:jenkins_jobs.cli.subcommand.update:Number of views updated: 0
Oke giờ bạn sẽ tự phát triên
3) How to create Jenkins Job Builder from any pipeline or any XML
Mình cũng đã đặt 1 câu hỏi là làm thể nào tạo ra 1 template jenkins job builder 1 cách dễ dàng?
Có thể convert từ XML jobs to YAML của job builder không?
https://github.com/ktdreyer/jenkins-job-wrecker
bạn có thể thực tham khảo link trên!