ProjectLuo
ProjectLuo

Reputation: 113

Using if statements in actions

I have this main.yml workflow right here:

name: Testing

on: 
  push:
    branches:
    - main
jobs:
  upgrade-kubectl:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install kubectl version
        uses: ./.github/actions/promote-image
        with:
          kubectl: 'latest'

and my action.yml metadata file:

name: "Helm, Kubectl or Devspace installation setup"
description: "Install a specific version of Helm, Kubectl or Devspace. Acceptable values are latest or version strings like 1.15.0"
inputs:
  kubectl:
    description: "Version of Kubectl"
    required: false
  helm:
    description: "Version of Helm"
    required: false
  devspace:
    description: "Version of Devspace"
    required: false
runs:
  using: "composite"
  steps:
    - name: Setting up kubectl
      uses: azure/setup-kubectl@v1
      with:
        version: ${{ inputs.kubectl }}
    - name: Setting up Helm
      uses: azure/setup-helm@v1
      with:
        version: ${{ inputs.helm }}
    - name: Setting up Devspace
      uses: loft-sh/setup-devspace@main
      with:
        version: ${{ inputs.devspace }}

Currently I am just supplying the kubectl version in my workflow, but when the action is triggered it is running all 3 steps instead.

How do I make it so that if I supply one version it only runs the one step, supply two version it runs two steps respectively, etc.

Any help would be appreciated !

Upvotes: 2

Views: 680

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 22970

It seems that now conditions are supported on composite actions.

Therefore, you could add if conditions at each step level according to the input used.

In that case, your action.yml workflow file would look like this:

name: "Helm, Kubectl or Devspace installation setup"
description: "Install a specific version of Helm, Kubectl or Devspace. Acceptable values are latest or version strings like 1.15.0"
inputs:
  kubectl:
    description: "Version of Kubectl"
    required: false
  helm:
    description: "Version of Helm"
    required: false
  devspace:
    description: "Version of Devspace"
    required: false
runs:
  using: "composite"
  steps:
    - name: Setting up kubectl
      if: ${{ inputs.kubectl != '' }}
      uses: azure/setup-kubectl@v1
      with:
        version: ${{ inputs.kubectl }}
    - name: Setting up Helm
      if: ${{ inputs.helm != '' }}
      uses: azure/setup-helm@v1
      with:
        version: ${{ inputs.helm }}
    - name: Setting up Devspace
      if: ${{ inputs.devspace != '' }}
      uses: loft-sh/setup-devspace@main
      with:
        version: ${{ inputs.devspace }}

Note that the syntax:

  • if: ${{ inputs.kubectl != '' }} works
  • if: ${{ inputs.kubectl }} != '' doesn't

Upvotes: 1

Related Questions