Sam
Sam

Reputation: 1834

Possible to trigger a build via github pr comment with some arguments

I would like to able to trigger a build via a Github comment. I've got that working however its a bit limiting. In my case, I have 3 or 4 named deployment targets and would like to trigger a pipeline that deployed to one of those deployments. There's nothing contextually indicative of what my preferred environment would be so I want to just be a variable.

Something like: /AzurePipelines run Deploy env=test foo=bar

Is this possible? If not does anyone have a clever workaround?

Currently, I have moved relevant portions of the pipeline to a template and have a pipeline per env leveraging that template. This creates an unpleasant amount of noise though.

Upvotes: 0

Views: 426

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30313

You can use Predefined variable $(Build.SourceVersionMessage) in your pipeline.

This variable is agent-scoped, and can be used as an environment variable in a script and as a parameter in a build task

So that You can add a powershell task at the top of your pipeline to get the your PR commit message. and then extract (using regular expression or json) the properties and values from the commit message. Then you can set the extracted properties to Env Variables for your pipeline.

Below is a simple example, When I create a pr, I set the commit message to a json string {"env":"test" "foo":"bar"}. And I extract the env and foo properties and their values in the scripts and set them to pipeline variables.

variables:
  env: ""
  foo: ""
steps:
- task: PowerShell@2
  displayName: 'Get Variables'
  inputs:
    targetType: inline
    script: |
      $data= '$(Build.SourceVersionMessage)'
      write-host $data
      $variables= $data | ConvertFrom-Json
      $variables.PSObject.Properties | foreach {Write-host "##vso[task.setvariable variable=$_.Name]$_.Value"}

Then you can use the variables in the following tasks according to your commit message.

Hope above helps!

Upvotes: 1

Related Questions