0

My current github workflow as quite repeating code to allow a conditionnement in what and where releases should be pushed depending on the event type :

name: Build LaTeX document & latexdiff to previous tagged version.
on:
  push:
    branches: [master]
  pull_request:
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Build stuff
        ...
        ...
      - name: Upload results (master latest)
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: "latest"
          prerelease: true
          draft: true
          title: "Build"
          files: |
            result
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
      - name: Upload results (pull request)
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: github.ref_name
          prerelease: true
          draft: true
          title: "Build"
          files: |
            result
        if: ${{ github.event_name == 'pull_request' }}
      - name: Upload results (tag)
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: github.ref_name
          prerelease: false
          draft: false
          title: "Build"
          files: |
            result
        if: ${{ github.ref_type == 'tag' }}

Is there a way to conditontionally set values so that i dont have to repeat 3 times the same thing ? Basically i want to deal with the three different cases :

  • Some commit lands on master, I releas it with the latest tag.
  • Some tag lands on master, I release it with its propper tag.
  • PRs are released under their names.

Not that it changes anything, but it really looks ugly to me.

lrnv
  • 1,038
  • 8
  • 19
  • 1
    Check the [answers](https://stackoverflow.com/questions/65384420/how-do-i-make-a-github-action-matrix-element-conditional) in this, similar to your use case. – Sibtain Feb 15 '23 at 09:43

1 Answers1

1

You could set the params as an output of a step and then use them in the upload step:

name: Build LaTeX document & latexdiff to previous tagged version.

on:
  push:
    branches: [master]
  pull_request:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Build stuff
        ...
        
      - name: Set params
        id: params
        run: |
          if [[ "${{ github.event_name }}" -eq "push"  && "${{ github.ref }}" -eq "refs/heads/master" ]]; then
            echo "tag=latest" >> $GITHUB_OUTPUT
            echo "prerelease=true" >> $GITHUB_OUTPUT
            echo "draft=true" >> $GITHUB_OUTPUT
          elif [[ "${{ github.event_name }}" -eq "pull_request" ]]; then
            echo "tag=${{ github.ref_name }}" >> $GITHUB_OUTPUT
            echo "prerelease=true" >> $GITHUB_OUTPUT
            echo "draft=true" >> $GITHUB_OUTPUT
          elif [[ "${{ github.ref_type }}" -eq "tag" ]]; then
            echo "tag=${{ github.ref_name }}" >> $GITHUB_OUTPUT
            echo "prerelease=false" >> $GITHUB_OUTPUT
            echo "draft=false" >> $GITHUB_OUTPUT
          fi

      - name: Upload results
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: ${{ steps.params.outputs.tag }}
          prerelease: ${{ steps.params.outputs.prerelease }}
          draft: ${{ steps.params.outputs.draft }}
          title: "Build"
          files: |
            result

Is it better then having 3 types of the step? I don't know. I have my doubts.

Notes:

  • There is no default. In case it's possible that there is an additional GitHub event that doesn't match these cases, then either set it in the additional else Bash condition or introduce an if condition to the upload step to make sure that it runs only in one of the three scenarios.
  • I haven't tested the code and there might be some bug. However, I believe you understand the idea of setting the params.
tmt
  • 7,611
  • 4
  • 32
  • 46
  • Looks like this is what i need indeed. Thanks a lot for taking the time, really helps. – lrnv Feb 15 '23 at 12:35