0%

在GitLab CI中使用drone-dingtalk-message插件

drone-dingtalk-message 是本人写的一个服务于 Drone CI 的插件,当时也没想要兼容其他CI工具,所以命名就直接带上了Drone。最近随着根据自定义模板和使用环境变量等功能的实现,我突发奇想,是否可以直接用在其他CI系统比如GitLab CI中呢?

从原理出发

用过Drone CI的同学应该会知道,Drone CI的插件其实一般是一个Docker镜像,既然是Docker,那么按理是可以用在其他地方的。那么参数是怎么传递到容器的呢?答案是环境变量。

实践出真知

多说无益,让我们试一试便知道究竟能不能行。
我们先跑个help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$ docker run lddsb/drone-dingtalk-message -h

NAME:
Drone DingTalk Message Plugin - Sending message to DingTalk group by robot using WebHook

USAGE:
drone-dingtalk [global options] command [command options] [arguments...]

VERSION:
0.2.1130

AUTHOR:
Dee Luo <[email protected]>

COMMANDS:
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--config.debug debug mode [$PLUGIN_DEBUG]
--config.tips.title value customize the tips title [$PLUGIN_TIPS_TITLE]
--config.token value, --access_token value, --token value DingTalk webhook access token [$PLUGIN_ACCESS_TOKEN, $PLUGIN_TOKEN]
--config.secret value, --secret value DingTalk WebHook secret for generate sign [$PLUGIN_SECRET]
--config.message.type value, --message_type value DingTalk message type, like text, markdown, action card, link and feed card... [$PLUGIN_MSG_TYPE, $PLUGIN_TYPE, $PLUGIN_MESSAGE_TYPE]
--config.message.at.all value at all in a message(only text and markdown type message can at) [$PLUGIN_MSG_AT_ALL]
--config.message.at.mobiles value at someone in a DingTalk group need this guy bind's mobile [$PLUGIN_MSG_AT_MOBILES]
--commit.author.username value providers the author username for the current commit [$DRONE_COMMIT_AUTHOR]
--commit.author.avatar value providers the author avatar url for the current commit [$DRONE_COMMIT_AUTHOR_AVATAR]
--commit.author.email value providers the author email for the current commit [$DRONE_COMMIT_AUTHOR_EMAIL]
--commit.author.name value providers the author name for the current commit [$DRONE_COMMIT_AUTHOR]
--commit.branch value providers the branch for the current build (default: "master") [$DRONE_COMMIT_BRANCH]
--commit.link value providers the http link to the current commit in the remote source code management system(e.g.GitHub) [$DRONE_COMMIT_LINK]
--commit.message value providers the commit message for the current build [$DRONE_COMMIT_MESSAGE]
--commit.sha value providers the commit sha for the current build [$DRONE_COMMIT_SHA]
--commit.ref value provider the commit ref for the current build [$DRONE_COMMIT_REF]
--repo.full.name value providers the full name of the repository [$DRONE_REPO]
--repo.name value provider the name of the repository [$DRONE_REPO_NAME]
--repo.group value provider the group of the repository [$DRONE_REPO_NAMESPACE]
--repo.remote.url value provider the remote url of the repository [$DRONE_REMOTE_URL]
--repo.owner value provider the owner of the repository [$DRONE_REPO_OWNER]
--build.status value build status (default: "success") [$DRONE_BUILD_STATUS]
--build.link value build link [$DRONE_BUILD_LINK]
--build.event value build event [$DRONE_BUILD_EVENT]
--build.started value build started [$DRONE_BUILD_STARTED]
--build.finished value build finished [$DRONE_BUILD_FINISHED]
--tpl.build.status.success value tpl.build status for replace success [$TPL_BUILD_STATUS_SUCCESS, $ PLUGIN_TPL_BUILD_STATUS_SUCCESS]
--tpl.build.status.failure value tpl.build status for replace failure [$TPL_BUILD_STATUS_FAILURE, $ PLUGIN_TPL_BUILD_STATUS_FAILURE]
--custom.pic.url.success value custom success picture url [$SUCCESS_PICTURE_URL, $PLUGIN_SUCCESS_PIC]
--custom.pic.url.failure value custom failure picture url [$FAILURE_PICTURE_URL, $PLUGIN_FAILURE_PIC]
--custom.color.success value custom success color for title in markdown [$SUCCESS_COLOR, $PLUGIN_SUCCESS_COLOR]
--custom.color.failure value custom failure color for title in markdown [$FAILURE_COLOR, $PLUGIN_FAILURE_COLOR]
--custom.tpl value custom tpl [$PLUGIN_TPL, $PLUGIN_CUSTOM_TPL]
--tpl.repo.full.name value tpl custom repo full name [$PLUGIN_TPL_REPO_FULL_NAME, $TPL_REPO_FULL_NAME]
--tpl.repo.short.name value tpl custom repo short name [$PLUGIN_TPL_REPO_SHORT_NAME, $TPL_REPO_SHORT_NAME]
--tpl.commit.branch.name value tpl custom commit branch name [$PLUGIN_TPL_COMMIT_BRANCH_NAME, $TPL_COMMIT_BRANCH_NAME]
--help, -h show help
--version, -v print the version

COPYRIGHT:
© 2018-2020 Dee Luo

从输出结果不难看出,我们只要设置好了几个关键的变量,比如PLUGIN_TOKENPLUGIN_SECRETPLUGIN_TPLPLUGIN_TYPE这几个,就可以实现钉钉群机器人消息推送了。

GitLab

设置Variables

我们可以在GitLab中通过设置仓库的CI/CDVariables实现环境变量的赋值,当然你也完全可以在.gitlab-ci.yml中直接设置环境变量,在这不多做介绍。下面给出这几个关键变量的含义:

变量名 含义
PLUGIN_TOKEN 群机器人的Token(必填)
PLUGIN_SECRET 群机器人的Secret(如果你设置了的话)
PLUGIN_TPL 自定义模版,可以是本地文件(路径)或远程文件(URL)
PLUGIN_TYPE 消息类型,推荐使用markdown

.gitlab-ci.yml

1
2
3
4
5
6
7
8
stages:
- build

build:
stage: build
image: lddsb/drone-dingtalk-message:1.2
script:
- drone-dingtalk

自定义模板

当然了,不要忘了自定义一下消息模板,因为默认的模版我没有对GitLab CI做兼容处理,原谅我能力有限。下面是一个自定义模版的例子(GitLab CI专用)

1
2
3
4
5
6
7
8
9
### **[CI_PROJECT_TITLE]**的**[CI_COMMIT_BRANCH]**更新**[TPL_BUILD_STATUS]**

更新内容: [CI_COMMIT_MESSAGE]

更新详情: [[CI_COMMIT_SHA]]([CI_PROJECT_URL]/commit/[CI_COMMIT_SHA])

更新作者: [[GITLAB_USER_NAME]([GITLAB_USER_EMAIL])](mailto:[GITLAB_USER_EMAIL])

[点击查看打包详情 [TPL_STATUS_EMOTICON]]([CI_PIPELINE_URL])

PS:你可以在仓库中设置仓库的别名,这样可以通过 CI_PROJECT_TITLE 变量获取一个更友好的仓库名字噢!还有更多好用的小技巧等待你去发掘与分享!

配置GitLab Runner

按照仓库中CI/CD里面的Runner安装教程,安装一个可用于该仓库的Runner即可。

撒花🎉

现在,你只需要把.gitlab-ci.yml文件推送到仓库,即可实现钉钉群机器人消息推送。是不是超简单!

成果展示

GitLab 内置变量参考

仅供参考,你也完全可以在使用drone-dingtalk-message的时候打开debug,也就是PLUGIN_DEBUG环境变量,得到跟下面类似的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
HOSTNAME=runner
CI_JOB_IMAGE=lddsb/drone-dingtalk-message
CI_RUNNER_SHORT_TOKEN=TuYGsz99
CI_BUILDS_DIR=/builds
CI_PROJECT_DIR=/builds/your-group/project-name
CI_CONCURRENT_ID=0
CI_CONCURRENT_PROJECT_ID=0
CI_SERVER=yes
CI_SERVER_TLS_CA_FILE=
CI_PIPELINE_ID=9922
CI_PIPELINE_URL=https://git.example.com/your-group/project-name/pipelines/9922
CI_JOB_ID=11601
CI_JOB_URL=https://git.example.com/your-group/project-name/-/jobs/11601
CI_JOB_TOKEN=[MASKED]
CI_BUILD_ID=11601
CI_BUILD_TOKEN=[MASKED]
CI_REGISTRY_USER=gitlab-ci-token
CI_REGISTRY_PASSWORD=[MASKED]
CI_REPOSITORY_URL=https://gitlab-ci-token:[MASKED]@git.example.com/your-group/project-name.git
CI_JOB_JWT=[MASKED]
CI_JOB_NAME=build
CI_JOB_STAGE=build
CI_NODE_TOTAL=1
CI_BUILD_NAME=build
CI_BUILD_STAGE=build
CI=true
GITLAB_CI=true
CI_SERVER_URL=https://git.example.com
CI_SERVER_HOST=git.example.com
CI_SERVER_PORT=443
CI_SERVER_PROTOCOL=https
CI_SERVER_NAME=GitLab
CI_SERVER_VERSION=12.10.6-ee
CI_SERVER_VERSION_MAJOR=12
CI_SERVER_VERSION_MINOR=10
CI_SERVER_VERSION_PATCH=6
CI_SERVER_REVISION=933c215f042
GITLAB_FEATURES=
CI_PROJECT_ID=728
CI_PROJECT_NAME=project-name
CI_PROJECT_TITLE=project-name
CI_PROJECT_PATH=your-group/project-name
CI_PROJECT_PATH_SLUG=your-group-project-name
CI_PROJECT_NAMESPACE=your-group
CI_PROJECT_URL=https://git.example.com/your-group/project-name
CI_PROJECT_VISIBILITY=internal
CI_PROJECT_REPOSITORY_LANGUAGES=
CI_DEFAULT_BRANCH=master
CI_PAGES_DOMAIN=example.com
CI_PAGES_URL=http://your-group.example.com/project-name
CI_REGISTRY=registry.git.example.com
CI_REGISTRY_IMAGE=registry.git.example.com/your-group/project-name
CI_API_V4_URL=https://git.example.com/api/v4
CI_PIPELINE_IID=5
CI_PIPELINE_SOURCE=web
CI_CONFIG_PATH=.gitlab-ci.yml
CI_COMMIT_SHA=aa2cfadd069353a2b996563225731e3b082cf9ae
CI_COMMIT_SHORT_SHA=aa2cfadd
CI_COMMIT_BEFORE_SHA=0000000000000000000000000000000000000000
CI_COMMIT_REF_NAME=master
CI_COMMIT_REF_SLUG=master
CI_COMMIT_BRANCH=master
CI_COMMIT_MESSAGE=test: test drone dingtalk
CI_COMMIT_TITLE=test: test drone dingtalk
CI_COMMIT_DESCRIPTION=
CI_COMMIT_REF_PROTECTED=true
CI_BUILD_REF=aa2cfadd069353a2b996563225731e3b082cf9ae
CI_BUILD_BEFORE_SHA=0000000000000000000000000000000000000000
CI_BUILD_REF_NAME=master
CI_BUILD_REF_SLUG=master
CI_RUNNER_ID=14
CI_RUNNER_DESCRIPTION=docker-runner
CI_RUNNER_TAGS=linux, docker
GITLAB_USER_ID=4
[email protected]
GITLAB_USER_LOGIN=username
GITLAB_USER_NAME=username
PLUGIN_DEBUG=true
PLUGIN_TOKEN=your-token
CI_DISPOSABLE_ENVIRONMENT=true
CI_RUNNER_VERSION=13.4.1
CI_RUNNER_REVISION=e95f89a0
CI_RUNNER_EXECUTABLE_ARCH=linux/amd64
HOME=/root

然后你就可以根据自己的需要挑选合适的环境变量用到自定义模板中去啦!Enjoy it!