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


肯德基:好吃!!!!

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

這是我們來試試把jenkins裝進container裡面
未來還可以裝進去不同的cloud vender上面, 例如AWS, Azure, GCP...等等

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

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

巧克力!!! https://chocolatey.org/
輸入 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

看到這個就是完美囉!

那我們來檢查一下是不是能正常登入
http://localhost:8080/

密碼如果找不到的話可以在container的這邊找到:
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

設定 Jenkins Server

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

必須裝的plugin

Blue Ocean

好用的圖像化界面,不用一行一行的看輸出

Configure-as-code-plugin

遵循IAC的精神, 想要Infra as code就靠他了
https://github.com/jenkinsci/configuration-as-code-plugin

安裝完後會長成下面的圖示

就可以利用寫好的程式碼進行測試囉

Job-dsl-plugin

遵循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去找出需要的參數

https://github.com/jenkinsci/job-dsl-plugin/wiki
https://jenkinsci.github.io/job-dsl-plugin/

手動先建立第一個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.

來檢查一下是不是成功了呢

順便也檢查一下JobOutput

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有不同的寫法

我們把Seedconfigure改成下面這樣:

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

pipelineJob('github-demo') {
    definition {
        cpsScm {
            scm {
                git {
                    remote {
                        github('jenkinsci/pipeline-examples')
                    }
                }
            }
            scriptPath('declarative-examples/simple-examples/environmentInStage.groovy')
        }
    }
}

會看到多一條新的Job

Reference

DSL
IAC


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







Related Posts

[DAY4] 初學Command Line

[DAY4] 初學Command Line

TypeScript - 配置說明

TypeScript - 配置說明

a tag 遺失 block 屬性

a tag 遺失 block 屬性


Comments