使用Slack启动Jenkins构建 [英] Using Slack to start Jenkins builds

查看:138
本文介绍了使用Slack启动Jenkins构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将擦除Mac Mini并从头开始重新安装新的XCode和Jenkins.我们最近才开始使用Slack,我们希望使用它来接收Jenkins构建通知并启动Jenkins构建.

we're about to wipe our Mac Mini and reinstall new XCode and Jenkins from scratch. We have just recently started using Slack and we would like to use it to receive Jenkins build notifications and to start Jenkins builds.

所以Slack通知没有问题.一切正常.

So Slack notifications, no problem. All working.

当我尝试使用Slack开始构建时,我的问题就出现了.

My problem comes when I am trying to start builds using Slack.

本教程( http://sonnguyen.ws/how-to-trigger-a-jenkins-build-from-slack/)介绍了一种使用Slack斜杠命令的方法.但是,我们的Mac Mini在我们的内部网络上.到目前为止,我们一直在使用Skype通过此sevabot与Jenkins进行自动构建.

This tutorial (http://sonnguyen.ws/how-to-trigger-a-jenkins-build-from-slack/) explains a way to use Slack's slash commands. However our Mac Mini is on our internal network. Up until this point we have being using Skype to automatically build with Jenkins using this sevabot.

使用Slack时,流行的选择似乎是使用斜杠命令,但是....我仍然有一些疑问,因为这是我第一次设置此东西:

With Slack, the popular choice seems to be to use the slash commands but.... I still have some questions since it's my first time setting this stuff up:

  1. 我们应该使用斜杠命令方法并采用某种领导者/跟随者方法,如下所述:只需为这些内容以及一些成功的示例设置寻找有关易于实现,可伸缩性,可维护性等方面的指导.

    Just looking for some guidance on ease of implementation, scalability, maintainability, etc for this stuff and some sample setups that people have had success with.

    谢谢.

    推荐答案

    所以我终于设法使用Slack的斜杠命令,字符串解析和以下答案来做到这一点:

    So I finally managed to do it using Slack's slash commands, strings parsing and this answer: How to set environment variables in Jenkins?

    我使用过的詹金斯插件:

    Jenkins plugins I used:

    1. pre-scm-build
    2. EnvInject插件
    3. 构建授权令牌根插件

    我将Slack斜杠命令配置为调用 https://my-jenkins-server//buildByToken/buildWithParameters?job = JOB_NAME&token = MY_SECRET_TOKEN .因此,当我键入'/build_my_app branch_name project_param2'时,我在斜杠命令之后键入的文本将作为URL参数(在本例中为'branch_name project_param2')发送过来.

    I configured my Slack slash command to call https://my-jenkins-server//buildByToken/buildWithParameters?job=JOB_NAME&token=MY_SECRET_TOKEN. So when I type '/build_my_app branch_name project_param2', the text I type after my slash command gets sent over as on URL parameter ('branch_name project_param2' in this case).

    一旦到达另一侧,我需要将此文本"参数拆分为Jenkins可用于构建过程的多个环境变量.为此,我添加了两个pre-scm构建步骤.

    Once it gets to the other side I need to split this 'text' parameter up into multiple environment variables that Jenkins can use for the build process. To achieve this I add two pre-scm build steps.

    Pre-scm构建步骤1是一个执行外壳"命令,由以下脚本组成:

    Pre-scm build step 1 is an 'Execute shell' command which consists of the following script:

    build_parameters=($text)
    
    echo BRANCH=${build_parameters[0]} > props
    echo PARAM2=${build_parameters[1]} >> props
    

    'props'是我用来存储解析的字符串的文件,因为在此步骤中尝试使用'export'设置环境变量会失败,因为在实际开始构建时env变量不会持续存在.这个脚本将$ text参数存储在一个字符串数组中(由空格分隔),我只引用该数组的每个索引来获取所需的变量.需要一个新的工作参数吗?只需在此处添加另一行即可.

    'props' is a file I use to store the parsed strings, as trying to set the environment variables in this step using 'export' fails as the env variables do not persist when the build actually starts. This script stores the $text parameter in a string array (split by space characters), and they I just reference each index of the array to get my desired variable. Need a new job parameter? Just add another line here.

    Pre-scm构建步骤2是一个注入环境变量"作业,在属性文件路径"中,我仅指定"$ WORKSPACE/props".然后读取包含我解析的env变量的文件,并将它们注入作业环境中.

    Pre-scm build step 2 is an 'Inject Environment Variables' job and in 'Properties File Path' I simply specify '$WORKSPACE/props'. This then reads the file with my parsed env variables and injects them into the job environment.

    是的,无需创建任何Web应用程序即可手动解析文本"参数,然后使用在URL中单独设置的所有参数调用另一个URL.不必使用每种可能的参数组合来完成一百万个不同的Jenkins工作.

    So yeah, no having to create any webapps to manually parse the 'text' parameter and then call a different URL with all the parameters set separately in the URL. No having to make a million different Jenkins jobs with each possible combination of parameter.

    希望这可以节省一些时间!

    Hope this saves someone some time!

    这篇关于使用Slack启动Jenkins构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆