Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fallback in expression syntax

I have a github workflow that runs on two different events: pull_request and manually. It looks like this:

on:
  pull_request:
    branches: [master]
  workflow_dispatch: {}

env:
  API_URL: https://example.com/

What I want is to be able to set an alternate API_URL when I trigger the workflow manually. But I want the workflow to run the same when it runs automatically. So, first, I add “inputs” to the workflow_dispatch:

on:
  pull_request:
    branches: [master]
  workflow_dispatch:
    inputs:
      API_URL:
        description:
        required: true
        default: https://example.com

env:
  API_URL: ${{ github.event.API_URL }}

From what I understand, this way the API_URL env variable will only be set when the workflow is run manually. But when it’s run automatically, there’s no github.event.API_URL value.

It seems to me that a conditional fallback here is needed. Will something like this work?

env:
  API_URL: ${{ github.event.API_URL || 'https://example.com/' }}

But from the docs the || operator will evaluate to 1 or 0.

Is there a good way to solve this? Thanks!

like image 575
timetowonder Avatar asked Jul 17 '26 02:07

timetowonder


2 Answers

This worked for me:

env:
  BRANCH: ${{ github.base_ref || 'devel' }}
like image 132
shanemcd Avatar answered Jul 18 '26 17:07

shanemcd


Unfortunately (at the time of writing this), it's not possible that way. But you can achieve the same effect by setting the environment variable (API_URL) to the default value dynamically only when the job is not run manually (eg. the job is triggered by the PR event). You can do that by creating a conditional job step that will run only if API_URL has not been set yet:

on:
  pull_request:
    branches:
      - master
  workflow_dispatch:
    inputs:
      API_URL:
        required: true
        default: https://example.com
jobs:
  build:
    env:
      API_URL: ${{ github.event.inputs.API_URL }}
    runs-on: ubuntu-latest
    steps:
      - name: Set API_URL environment variable
        if: env.API_URL == null
        run: echo "API_URL=https://example.com" >> $GITHUB_ENV
      - name: Test API_URL environment variable
        run: echo "API_URL=$API_URL"

This way we will have API_URL environment variable set regardless of the way the workflow was triggered (either manually or automatically) with the ability to override it with a custom value when run manually.

like image 22
Marcin Kłopotek Avatar answered Jul 18 '26 17:07

Marcin Kłopotek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!