美文网首页
Jenkins实战中的一些技巧

Jenkins实战中的一些技巧

作者: 欧子有话说_ | 来源:发表于2022-09-03 09:38 被阅读0次

在隔离期间,看了下Jenkins,虽然没有用到Pipline等功能,但通过基础的配置,也能将CI/CD做的不错(理解General、源码管理、触发器构建、构建环境、构建、构建后的操作等步骤),其实其最大的特点就是插件全、支持灵活的shell操作,下面列举用到的一些功能。

1:Parameterized Trigger

https://plugins.jenkins.io/parameterized-trigger/

这个插件很有用,但你完成一个构建后,可以触发下一个构建并传递新的参数。比如说Go构建完成后,可以触发发送邮件。

image.png

2:Envi ronment Injector

https://plugins.jenkins.io/envinject/

这个插件的作用就是能够创造环境变量,在多个构建过程中能复用变量。具体做法就是变量保存在一个文件中。

<pre class="prettyprint hljs bash" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Desc=curl "https://gitlab.com?branch=${GIT_BRANCH}"
echo "Desc=${BranchDesc2}" >/var/lib/jenkins/env.properties</pre>

image.png

3:Upload to pgyer with apiV2

https://plugins.jenkins.io/upload-pgyer/

这个插件主要是Android打包后,将apk文件上传到蒲公英中下载。

image.png

在该例子中,shell修改了上一个步骤说到的注入环境变量,然后payer插件能读取,如果不用注入环境变量,shell修改变量值是不会生效的。

4:构 建,选择执行shell

shell非常强大,可以完成很多工作,比如:

<pre class="prettyprint hljs bash" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">git clone git@ip:项目/仓库.git articlepath cdarticlepath
git checkout master

cd ${WORKSPACE}
./cmd/build.sh</pre>

在这个例子中,可以看到一个仓库的构建过程还依赖另外一个仓库,可以选择clone下来进行编译,不过好像git submodule就是解决这个问题的

5:Email Extension

https://plugins.jenkins.io/email-ext/

这个插件能够让你配置更多的邮件参数,可以自定义参数等等。

image.png

不过需要注意的是,Jenkins中自带的邮件设置也要做更改。

6:Publish Over SSH

在构建过程中,可以利用SSH做一些事情,比如执行命令或者发送文件(其实用shell更方便)

  • Send files or execute commands over SSH before the build starts
  • Send files or execute commands over SSH after the build runs

对于ssh和git来说,可以采用私钥免登录。

7:全局属性和环境变量

image.png

可以在设置中配置一些全局的变量,比如软件的编译位置等等。

8:Manager Credentials

这个对安全性很重要,任何形式的密钥(邮箱,gitlab密码)都可以以票据的方式保存,非常方便和安全。

9:Parameterized Build

https://wiki.jenkins.io/display/JENKINS/Parameterized+Build

参数化构建,可以作为全局变量传递,但这个值shell是不能更改的(除非使用注入环境变量),Parameterized Trigger传递参数的时候,在下一个构建任务中也要新建参数用于接收上一个构建的变量。

10:GitLab

https://plugins.jenkins.io/gitlab-plugin/

GitLab插件很有用,通过配置,在提交代码的时候Gialab会调用jenkins webhook事件给Jenkins(token验证),然后开启构建过程,比如:

image.png image.png

相关文章

网友评论

      本文标题:Jenkins实战中的一些技巧

      本文链接:https://www.haomeiwen.com/subject/valunrtx.html