Search code examples
github-actions

Github Actions- expression function to set environment variable


I had written below Github Actions where I am unable to set URL in the workflow graph.

# This is a basic workflow to help you get started with running Fortify scans using ScanCentral into Fortify SSC

name: Namelink Scan

# Controls when the workflow will run
on:
  workflow_dispatch:
    inputs:
      choice:
        type: choice
        description: Please select the project to scan
        options:
        - Factorysite
        - HouseSite
env:
    Factorysite: "https://company.com"
    HouseSite: "https://house.com"

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "Scan"
  Scan:
    # The type of runner that the job will run on
    runs-on: windows-latest
    environment:
      name: Test
      url: ${{ format('env.{0}', inputs.choice) }}
    steps:
      - uses: actions/checkout@v3

In the environment section, I mentioned the environments as below

environment:
      name: Test
      url: ${{ format('env.{0}', inputs.choice) }}

Expected Output to show URL in workflow graph.

Expected Output with URL

Actual Output with warning in workflow Graph.

Actual Output without URL link

Receiving below warning which I cant to fix.

Annotations
1 warning
Scan
Environment URL 'env.Factorysite' is not a valid http(s) URL, so it will not be shown as a link in the workflow graph.

Solution

  • In the given code example

    environment:
      name: Test
      url: ${{ format('env.{0}', inputs.choice) }}
    

    The url property value is evaluated to either env.Factorysite or env.HouseSite string depending on provided choice. Take note it's a string (not a value of an evaluated string). So you will not get the appropriate url value at runtime but the name of the environment property holding the URL value. In other words, the code written is not equivalent to (what you expect):

    environment:
      name: Test
      url: ${{ env.Factorysite }}
    

    In this example, the value of the url property will be evaluated at workflow runtime to the value of the environment variable (https://company.com in your case). So far so good. Now we want to join the approaches to get the value of the proper URL at runtime based on provided choice. Since env context is an object we can refer to its properties using the array access operator ([]) to get the given property value:

    environment:
      name: Test
      url: ${{ env[inputs.choice] }}
    

    This way you can populate the expected value of the environment.url and get it properly displayed in the workflow visualization graph.