3

I just started using Pulumi and I checked a lot of docs, a lot of repos and just can't find how can I get the PR (Pull Request) number to be used.

I know that Pulumi generates an Environment variable/output/config named ci.pr.number.

I would like to use that number to create an Azure Resource Group like rg-{appname}-{environment}-{pr number}. For example, rg-myapp-dev-022.

Last attempt was this one:

var config = new Pulumi.Config();
var prNumber = config.Require("ci.pr.number");

Thanks in advance!

PS: I am using GitHub Actions to run Pulumi.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Jonathas Costa
  • 1,006
  • 9
  • 27

1 Answers1

2

If you want to use the PR number in your Pulumi program at runtime, you'll need to read it from the GitHub Actions runtime environment somehow; Pulumi doesn't write it to your stack configuration. GitHub Actions exposes a Context API that makes this fairly straightforward, assuming you have access to make a small addition to the workflow itself. Here's an example of a PR workflow containing a step that pulls the PR number into an environment variable (PR_NUMBER) and then uses it in a couple of scripts, first Bash, then Node.js:

name: My Workflow
on:
  pull_request:
    branches:
      - master
jobs:
  build:
    name: Do Things
    runs-on: ubuntu-18.04
    steps:
      - name: Install Node
        uses: actions/setup-node@v1
        with:
          node-version: '14.x'

      - name: Check out branch
        uses: actions/checkout@v2

      - name: Read the PR number
        env:
          PR_NUMBER: ${{ github.event.number }}
        run: |
          echo "From Bash: ${PR_NUMBER}"
          node -e "console.log('From Node:', process.env.PR_NUMBER);"

GHA Output

I don't have a way to verify the relevant C# handy, but it looks like you should be able to do this with System.Environment.GetEnvironmentVariable("PR_NUMBER").

https://learn.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariable

Christian Nunciato
  • 10,276
  • 2
  • 35
  • 45