How to build CICD with Jenkins as code based on container

Posted by jerryeml on 2021-06-14


最近因為一些因素 開始回到了當初Jenkins老爺爺的懷抱XD
因此想來紀錄一下 玩到的東西

更新日期 - 2021/06/13

Jenkins 老爺爺是誰? 跟肯德基爺爺比呢?

Jenkins:是一款主要由川口耕介開發、用 Java 編寫的開源的 CI/CD 工具,它執行在 Servlet 容器(如:Apache Tomcat)中,支援 SCM (Source Code Management,原始碼管理)工具,如 Git


既然要回來玩 總要加點東西

未來還可以裝進去不同的cloud vender上面, 例如AWS, Azure, GCP...等等

開始環境設定囉 (官方教學)

我們會需要基本的一些設定 可以先安裝一個萬用的installer

輸入 choco install docker-cli -y
輸入 choco install docker-desktop -y

從 Docker Hub 下載Jenkins image 然後把它run起來吧!!!

docker pull jenkins:2.60.3
docker pull jenkins/jenkins:2.289.1-lts-centos

docker run -p 8080:8080 -p 50000:50000 jenkins:2.60.3
docker run -p 8080:8080 -p 50000:50000 jenkins/jenkins:2.289.1-lts-centos



This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

設定 Jenkins Server

當我們進入Jenkins後 最基本的就是安裝好用的plugin拉


Blue Ocean



遵循IAC的精神, 想要Infra as code就靠他了




遵循IAC的精神, 幫助自動建立各種Jenkins裡面的Jobs

A Job DSL script consists of API methods provided by the Job DSL plugin; you can use these API methods to configure different aspects of a job, such as its type (freestyle versus pipeline jobs), build triggers, build parameters, post-build actions, and so on. You can find all supported methods on the API reference site.

可以根據不同你想要建置的Job type去找出需要的參數

手動先建立第一個seed 來幫你自動Create job吧!!!

The seed job is a normal Jenkins job that runs the Job DSL script; in turn, the script contains instructions that create additional jobs.

簡單來說!!! 當你今天有一堆Job需要被產生的時候
The seed job 可以幫助你透過Script產生更多的Job

我們先來建立一個Seed job吧!

The Job DSL script that you’ll define will create a single freestyle job that prints a 'Hello World!' message in the job’s console output.

For the Hello World freestyle job, you need the job API method (freeStyleJob is an alias of job and would also work). Let’s navigate to the documentation for the job method.

Click the ellipsis icon (…) in job(String name) { … } to show the methods and blocks that are available within the job block.

Let’s go over some of the most commonly used methods and blocks within the job block:

  • parameters: setting parameters for users to input when they create a new build of the job.
  • properties: static values that are to be used within the job.
  • scm: configuration for how to retrieve the source code from a source-control management provider like GitHub.
  • steps: definitions for each step of the build.
  • triggers: apart from manually creating a build, specifies in what situations the job should be run (for example, periodically like a cron job, or after some events like a push to a GitHub repository).

You can further expand child blocks to see what methods and blocks are available within. Click on the ellipsis icon (…) in steps { … } to uncover the shell(String command) method, which you can use to run a shell script.

Putting the pieces together, you can write a Job DSL script like the following to create a freestyle job that, when run, will print 'Hello World!' in the output console.

job('demo') {
    steps {
        shell('echo Hello World!')

思考並組好想要CreateJOB內容後, 我們就可以真的來把Seed Job做出來囉

To create the seed job, go to your_jenkins_url, log in (if necessary), click the New Item link on the left of the dashboard. On the screen that follows, type in seed, select Freestyle project, and click OK.

In the screen that follows, scroll down to the Build section and click on the Add build step dropdown. Next select Process Job DSLs.

Then, click on the radio button next to Use the provided DSL script, and paste the Job DSL script you wrote into the DSL Script text area.

Then, navigate to your_jenkins_url and confirm that the seed job is there.



You’ve run the demo job and confirmed that the echo step specified in the Job DSL script was executed. In the next and final step, you will be modifying and re-applying the Job DSL script to include an additional pipeline job.

定義自己的 Piplein Job 的內容

根據不同的Job type有不同的寫法


job('demo') {
    steps {
        shell('echo Hello World!')

pipelineJob('github-demo') {
    definition {
        cpsScm {
            scm {
                git {
                    remote {




#jenkins #CICD #container #IAC #Jenkins Job DSL Plugin

Related Posts

沒有高低之分 只有先後順序

沒有高低之分 只有先後順序

用 Python 自學資料科學與機器學習入門實戰:Pandas 基礎入門

用 Python 自學資料科學與機器學習入門實戰:Pandas 基礎入門

Python基礎入門 | 當前日期和時間 (Current date and time)

Python基礎入門 | 當前日期和時間 (Current date and time)