Search code examples
yamlcontinuous-integrationgithub-actions

Combining conditional artifact download blocks in GitHub Actions


I have two separate GitHub Actions blocks that download artifacts from a workflow based on the version specified by the user. I want to merge these two blocks into a single one. The only difference between the two blocks is that the run_number input is only included if the inputs.my_artifact_version is not set to "latest".

Here's the original code:

- name: Download The Latest Artifact Version
  if: inputs.my_artifact_version == 'latest'
  uses: dawidd6/action-download-artifact@v2
  with:
    workflow: my_backup_workflow.yml
    workflow_conclusion: success
    branch: main
    name: ${{ env.MY_BACKUP_FOLDER }}
    path: output
    if_no_artifact_found: fail

- name: Download The Artifact Version [${{ inputs.my_artifact_version }}]
  if: inputs.my_artifact_version != 'latest'
  uses: dawidd6/action-download-artifact@v2
  with:
    workflow: my_backup_workflow.yml
    workflow_conclusion: success
    branch: main
    run_number: ${{ inputs.my_artifact_version }}
    name: ${{ env.MY_BACKUP_FOLDER }}
    path: output
    if_no_artifact_found: fail

I want to merge these two blocks into a single one that will automatically detect whether to include the run_number input or not.

Can anyone help me with this?

Thanks!


Solution

  • To combine both steps, you may use something like this in one step without an if:

    run_number: ${{ (inputs.my_artifact_version != 'latest') && inputs.my_artifact_version || '' }}