github-actions

GitHub Actions: How can I specify ref to checkout conditionally?


I made an optional input to specify branch:

on:
  workflow_dispatch:
    inputs:
      DEPLOYMENT_BRANCH:
        description: branch that needs to be deployed
        type: string
        required: false

... then want to use as a checkout option:

jobs:
  my_job:
    steps:
    - name: Checkout repo
      uses: actions/checkout@v3
      with:
        ref: ${​​{​​ inputs.DEPLOYMENT_BRANCH }​​}​​

Now, I want to checkout only if the DEPLOYMENT_BRANCH is passed and ignore otherwise. So I came up with this:

- name: Checkout repo
  if: inputs.DEPLOYMENT_BRANCH != ''
  uses: actions/checkout@v3
  with:
    ref: ${{ inputs.DEPLOYMENT_BRANCH }}

Can someone please validate if this is right way to identify undefined DEPLOYMENT_BRANCH​​​​​​​ or should I use some other variable?


Solution

  • You can directly pass the input to actions/checkout@v3 with ${​​{​​inputs.DEPLOYMENT_BRANCH}​​}​​(without any conditionals).

     - name: Checkout repo
       uses: actions/checkout@v3
       with:
         ref: ${​​{​​ inputs.DEPLOYMENT_BRANCH }​​}​​ 
    

    This is because from the source code the action will fallback to the default if the ref input evaluate to false.