Jenkins 发展到2.x, 出现了pipeline,实现了真正意义上的 “pipeline as code”,即 完全的代码驱动构建。使用pipeline进行构建的过程跟用dockerfile进行镜像构建非常相似。pipeline是写在Jenkinsfile文件里面的。
以下是一段经典的jenkins pipeline “hello world” 示例:
pipeline { agent { docker 'centos:latest' } stages { stage('build') { steps { sh 'echo helloworld!' } } } }
其中,整个构建是在一个 最新版本的centos docker 镜像中构建,然后就能在输出中看到 “ hello world” 了。输出截图如下:
更多Jenkins知识,请参考官方网站: Jenkins教程 。从图中可以看到整个构建过程是在blueocean中构建的,blueocean很有趣,具体参考教程。
Generic webhook trigger 是一个 jenkins的插件,支持pipeline,用来接受时间触发源发送来的触发请求。安装插件后,直接在pipeline编写触发接受规则即可。使用该插件的主要步骤如下:
通过Jenkins 插件管理器安装该插件:
pipeline { agent { docker { image 'node:10.16' args '--name stockhey -p 8087:3000' } } triggers { GenericTrigger( genericVariables: [ [key: 'ref',value: '$.ref'] ], token: 'secret', causeString: 'Triggered on $ref', printContributedVariables: true, printPostContent: true ) } stages { stage('Build') { steps { sh 'npm install' } } stage('Deliver') { steps { sh 'npm start' } } } }
其中:
整个项目在node:10.16 镜像中构建
触发器发送的触发请求应包含 “token=secret” 参数。示例请求url (http://xxxxx.example.com/generic-webhook-trigger/invoke?token=secret)
项目构建过程中运行了两个命令: npm install 和 npm start
下面介绍 事件触发源: github webhook
其中:
Triggered on refs/heads/master Connecting to https://api.github.com using admin/****** (GitHub Access Token) Obtained Jenkinsfile from 53a7ae2ac478bf215c4exxxxxxxxxxxxxxxxxxx Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline GenericWebhookEnvironmentContributor Received: {"ref":"refs/heads/master","before":"xxxxxxxxxxxxxxxxxxx","after":"yyyyyyyyyyyyyyyyyyyyyy","repository": ... ... ... ... ... Contributing variables: ref = refs/heads/master [Pipeline] node Running on Jenkins in /var/jenkins_home/workspace/stockhey_project_nodejs_master [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Checkout SCM) [Pipeline] checkout using credential github > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository ... ... ... ... > git rev-parse --verify HEAD # timeout=10 Resetting working tree > git reset --hard # timeout=10 > git clean -fdx # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] isUnix [Pipeline] sh + docker inspect -f . node:10.16 . [Pipeline] withDockerContainer Jenkins seems to be running inside container 0c2429dab55184c7c58c4816a70da9ac3509d798aa290ccb1997064991f52ac2 # $ docker run -t -d -u 0:0 --name stockhey -p 8087:3000 -w /var/jenkins_home/workspace/stockhey_project_nodejs_master --volumes-from ********* node:10.16 cat $ docker top 76f86a532288e35df1de44f9050706ebe72963669cca59ef45d9c524dc5a118f -eo pid,comm [Pipeline] { [Pipeline] stage [Pipeline] { (Build) [Pipeline] sh + npm install audited 539 packages in 3.058s found 3 low severity vulnerabilities run `npm audit fix` to fix them, or `npm audit` for details [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Deliver) [Pipeline] sh + npm start > stock_project_nodejs@0.1.0 start /var/jenkins_home/workspace/stockhey_project_nodejs_master > node bin/www mongodb connection success { Error: connect ECONNREFUSED 127.0.0.1:27017 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14) name: 'MongoNetworkError', [Symbol(mongoErrorContextSymbol)]: {} } <-- GET / insert success 27.18.188.137-2020-03-30-AM --> GET / 200 309ms 4.57kb <-- GET /stylesheets/ech_all.css <-- GET /stylesheets/style.css --> GET /stylesheets/ech_all.css 200 4ms 38.91kb --> GET /stylesheets/style.css 200 3ms 613b <-- GET /javascripts/myechart.js --> GET /javascripts/myechart.js 200 1ms 8.09kb <-- GET /images/arrow-words.png --> GET /images/arrow-words.png 200 0ms 2.75kb <-- GET /favicon.ico --> GET /favicon.ico 200 1ms 4.19kb Sending interrupt signal to process ... ...
其中,可以清楚的看到 构建从被触发,到执行的全部过程。
可能是发现了github 与 持续构建 之间天然的 紧密联系,而不再甘心只做jenkins的伴娘。github的新功能Github Action 已经正式推出一段时间了。相信会对Jenkins统治的世界造成一定的影响。值得持续关注。
本文作者:王海生
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!