Exclude GitHub Action/Workflow job from invoking on new label

Viewed 111

GitHub Action Workflow with a job conditioned by:

    if: |
      (needs.changed.outputs.stack_workspace) &&
      (
        (github.event.action == 'edited' && contains(github.event.pull_request.body, '/teardown')) ||
        (
          contains('unlabeled closed', github.event.action) && 
            (
              github.event.label.name == 'auto:deploy' || 
              contains(github.event.pull_request.labels.*.name, 'auto:deploy')
            )
        )
      )

The job is executed when the PR is labeled with 'auto:deploy' - which is unfortunate because its a destroy job that runs at the same time as the deploy job :D

How do I change the conditional to preserve truthiness when 'auto:deploy' is removed but not when it is added?

1 Answers

The GitHub Expression, contains() accepts arrays or strings as search lists. But, when search is a string, its just chars, not words and yes, 'labeled' is in 'unlabeled'.

There really isn't a way to declare the array on the fly, in the expression so you have to separate each test into its own comparison (creating almost a dozen conditions in my case, or deconstruct the conditions via scripting in a previous step and out but the value for the job conditional.

      - name: Determine Job Targets
        id: targets
        env:
          IS_DEPLOY_LABEL: ${{ github.event.label.name == 'auto:deploy' }}
          HAS_DEPLOY_LABEL: ${{ contains(github.event.pull_request.labels.*.name, 'auto:deploy') }}
          ACTION_NAME: ${{ github.event.action }}
          PR_BODY: ${{ github.event.pull_request.body }}
        run: |
          is_deploy_label() {
              [[ $IS_DEPLOY_LABEL == 'true' ]] && return 0
              return 1
          }
          has_deploy_label() {
              [[ $HAS_DEPLOY_LABEL == 'true' ]] && return 0
              return 1
          }
          slash_cmd() {
              [[ $ACTION_NAME == 'edited' && $PR_BODY == *"/$1"* ]] && return 0
              return 1
          }
          review_action() {
              [[ $ACTION_NAME =~ ^(labeled|opened|synchronize)$ ]] && ( is_deploy_label || has_deploy_label ) && return 0
              return 1
          }
          teardown_action() {
              [[ $ACTION_NAME == "unlabeled" ]] && is_deploy_label && return 0
              [[ $ACTION_NAME == "closed" ]] && return 0
              return 1
          }
          declare -a dispatch_jobs
          slash_cmd 'teardown' || teardown_action && dispatch_jobs+=('teardown')
          slash_cmd 'review' || review_action && dispatch_jobs+=('review')
          echo "${dispatch_jobs[@]}"
          echo "::set-output name=job_names::${dispatch_jobs[@]}"

and

    if: contains(needs.changed.outputs.targets, 'review')
Related