Using Slack to start Jenkins builds

后端 未结 2 1752
情歌与酒
情歌与酒 2021-02-09 17:54

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 buil

2条回答
  •  难免孤独
    2021-02-09 18:26

    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 Plugin
    3. Build Authorization Token Root Plugin

    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).

    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 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' 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 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.

    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!

提交回复
热议问题