Home > Software engineering >  Conditionally append a string in a GitHub actions workflow
Conditionally append a string in a GitHub actions workflow

Time:10-18

Is it possible to use GitHub Actions expressions to concatenate a string conditionally?

E.g. like I'm trying to do in below example.

      - name: Release
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist  ${{ if (startsWith(github.ref, 'refs/tags/')) { '--snapshot' }}

CodePudding user response:

You could use the following which would return -additional-arg if the condition is satisfied and empty string otherwise:

${{ (startsWith(github.ref, 'refs/tags/') && '-additional-arg') || '' }}

Example:

name: Test arguments

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Release
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist ${{ (startsWith(github.ref, 'refs/tags/') && '--snapshot') || '' }}
  • Related