Home > Software design >  Github action expressions
Github action expressions

Time:11-15

Working on Github actions for the first time. In my .yml file I have the following

on:
  workflow_dispatch:
    branches:    
      - main
    inputs:
      environment:
        type: choice
        description: 'Select environment to deploy in'
        required: true
        options:
          - dev
          - non-prod
          - prod
          - staging

based on the option I need to do the following

for staging

  - name: build
    run: CI=false yarn build-staging

for non-prod

  - name: build
    run: CI=false yarn build

Could you please provide me with some pointers on how this can be achieved?

CodePudding user response:

The simplest way to go about it would be to use an if condition on the jobs within your workflow, for example:

on:
  workflow_dispatch:
    branches:    
      - main
    inputs:
      environment:
        type: choice
        description: 'Select environment to deploy in'
        required: true
        options:
          - dev
          - non-prod
          - prod
          - staging
jobs:
  staging:
    runs-on: ubuntu-latest
    if: inputs.environment == 'staging'
    steps:
    - name: build
      run: CI=false yarn build-staging
  prod:
    runs-on: ubuntu-latest
    if: inputs.environment == 'prod'
    steps:
    - name: build
      run: CI=false yarn build
  • Related