How to read/get job arguments sent using slack slash commands to trigger a gitlab job?

333 Views Asked by At

I am trying to trigger a release job on master branch using slack slash commands, the job is triggered but i can't seem to get or read the passed argument. Per the docs of gitlab we can pass arguments to run a job but how to read the argument is not specified. Here's the slack slash command:

/gitlab projectX run slack:chatops hello

Here's the job in the gitlab-ci.yml

slack:chatops:
stage: chatops
rules:
  - if: $CI_PIPELINE_SOURCE == "chat"
script:
  - echo "Hello World, job argument: "$1

Anyone already tried using job arguments?

SOLUTION

I found the solution after carefully re-reading the ChatOps docs for Gitlab where there's a CHAT_INPUT variable that contains all arguments as a string and the previous job becomes:

slack:chatops:
stage: chatops
rules:
  - if: $CI_PIPELINE_SOURCE == "chat"
script:
  - echo "Hello World, job argument: $CHAT_INPUT"
1

There are 1 best solutions below

1
On BEST ANSWER

A different page in the docs mention some variables that become available. https://docs.gitlab.com/ee/ci/chatops/index.html

ChatOps passes the following CI/CD variables to the job:

  • CHAT_INPUT contains any additional arguments.
  • CHAT_CHANNEL is set to the name of channel the action was triggered in.
  • CHAT_USER_ID is set to the chat service’s user ID of the user who triggered the slash command.