How to run a reusable workflow conditionnaly? - yaml

I have the following workflow in my GitHub actions:
name: Tests e2e iOS App
on:
workflow_dispatch:
inputs:
skip:
type: boolean
required: true
default: false
jobs:
build-simu-ios-zip:
name: Build iOS simulator zip
uses: ./.github/workflows/reusable-e2e-buildsimuioszip.yml
secrets: inherit
with:
environment: ${{ inputs.environment }}
I would like to run the job build-simu-ios-zip conditionnaly, I add the following:
jobs:
build-simu-ios-zip:
name: Build iOS simulator zip
+ if: ${{ inputs.skip == 'false' }}
uses: ./.github/workflows/reusable-e2e-buildsimuioszip.yml
secrets: inherit
with:
environment: ${{ inputs.environment }}
But the job automatically get skipped.
I also tried to pass an input to the reusable workflow and make it conditionnaly it from there, but it also skip.
How can I make a conditionnal reusable workflow in GitHub action?

I made some tests here and using if: ${{ inputs.skip == 'false' }} with single quotes ' doesn't work as you are comparing a boolean type with a string.
However, I found 2 options that worked:
if: ${{ inputs.skip == false }} (no quote)
if: ${{ ! inputs.skip }} (as it's a boolean input, but with !)
Note: I used this workflow for tests.

Related

Parametrise uses parameter in GitHub Actions

How I can pass parameter inputs.custom to this actions code:
jobs:
test-custom:
name: Test Custom
uses: ./.github/workflows/work4-${{ inputs.custom }}.yml
Fully working example:
First workflow work1-build.yml:
name: Start workflow fail
on: [push]
jobs:
build-fail:
name: Build with other workflow
uses: ./.github/workflows/work3-build-fail.yml
with:
custom: custom-name1
Second workflow work3-build-fail.yml:
name: Build fail with input test
on:
workflow_call:
inputs:
custom:
description: Some custom string
required: true
type: string
jobs:
test-custom:
name: Test Custom
uses: ./.github/workflows/work4-${{ inputs.custom }}.yml
Third workflow work4-custom-name1.yml
name: Custom 1
on:
workflow_call
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "IN CUSTOM 1"
Example above make GitHub respond with an error:
Invalid workflow file
error parsing called workflow
".github/workflows/work1-build.yml"
-> "./.github/workflows/work3-build-fail.yml" (source branch with sha:720087c8794e76f52277f9b1229b44ea65ab89d5)
--> "./.github/workflows/work4-${{ inputs.custom }}.yml"
: failed to fetch workflow: workflow was not found.
I can successfully add ${{ inputs.custom }} to:
test-print:
runs-on: ubuntu-latest
name: Print input
steps:
- name: Step print input
run: echo ${{ inputs.custom }}
Docs doesn't contain any examples with uses parametrisation:
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses
I found documentation that proves this isn't possible. There is no supporting info stating that the uses key has access to any contexts.
See: https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
I believe this is an architectural limitation of GitHub Actions, it appears they want to resolve all workflows/actions at the start of all jobs and thus dynamic resolution isn't possible.

Triggering yaml file when a certain label is applied to the pull request

I am working on a project where I need to create a tweet from an account whenever a certain issue or pull request has a label issue/tweet
I am able to make tweet when label is applied to issue but unable to do so when the same lable is applied to a pr
the .yml file I am working on
name: Send a Tweet
on:
issues:
-label: issue/tweet
pull_request:
types: [labeled]
jobs:
tweet:
if: ${{github.event.label.name == 'issue/tweet'}}
runs-on: ubuntu-latest
steps:
- uses: ethomson/send-tweet-action#v1
with:
status: ${{github.event.issue.html_url}} "#opensource"
consumer-key: ${{ secrets.TWITTER_CONSUMER_API_KEY }}
consumer-secret: ${{ secrets.TWITTER_CONSUMER_API_SECRET }}
access-token: ${{ secrets.TWITTER_ACCESS_TOKEN }}
access-token-secret: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
expected to make a tweet when pr is labeled with `issue/tweet'
Let's dissect the important parts...
Workflow sections
on events
You want issues and pull_request to trigger the workflow and I think both of them should have their types set to labeled.
if job condition
The condition needs to be applied to the correct object within github.event. For each of these events, the content is different. It's defined in Webhook events and payloads.
Both issue and pull_request should have github.event.label so I'm not sure why it wouldn't work. It might be a good idea to dump the entire context at the beginning of your job in order to debug it.
However, both events also have github.event.TYPE.labels, an array of label objects. Therefore, it might be a better option to use that and apply contains expression on it:
contains(github.event.TYPE.labels.*.name, 'issue/tweet')
Result
name: Send a Tweet
on:
issues:
types: [labeled]
pull_request:
types: [labeled]
jobs:
tweet:
if: >-
(
contains(github.event.pull_request.labels.*.name, 'issue/tweet') ||
contains(github.event.issue.labels.*.name, 'issue/tweet')
)
runs-on: ubuntu-latest
steps:
- uses: crazy-max/ghaction-dump-context#v1
- uses: ethomson/send-tweet-action#v1
with:
status: ${{github.event.issue.html_url}} "#opensource"
consumer-key: ${{ secrets.TWITTER_CONSUMER_API_KEY }}
consumer-secret: ${{ secrets.TWITTER_CONSUMER_API_SECRET }}
access-token: ${{ secrets.TWITTER_ACCESS_TOKEN }}
access-token-secret: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}

Set Github action env variable based on target branch

I am trying to set different env variables according to the target branch of the pull request.
For example, I am trying to have separate env variable for main and develop, but the ci is keeping such step and not running the actions
- name: Make envfile for develop
uses: SpicyPizza/create-envfile#v1.3
if: ${{ github.ref == 'refs/heads/develop'}}
with:
- name: Make envfile for main
uses: SpicyPizza/create-envfile#v1.3
if: ${{ github.ref == 'refs/heads/main'}}
with:

Only re-run the failed job in a GitHub Actions matrix

How can I re-run only a failed job in a GitHub Actions matrix?
I only want to run the failed jobs and not all of them. My CI matrix takes a lot of time to run, and I don't want to waste energy re-running the whole matrix because one of them failed.
So this button is not my solution:
Re-run a specific job has not been implemented yet, but it is on the roadmap.
However, reading this ISSUE about the subject, there is a workaround from this post, which stores the last run status in cache and then skips jobs based on that status.
How they used it:
- name: Set default run status
run: echo "::set-output name=last_run_status::default" > last_run_status
- name: Restore last run status
id: last_run
uses: actions/cache#v2
with:
path: |
last_run_status
key: ${{ github.run_id }}-${{ matrix.os }}-${{ matrix.node-version }}-${{ matrix.webpack }}-${{ steps.date.outputs.date }}
restore-keys: |
${{ github.run_id }}-${{ matrix.os }}-${{ matrix.node-version }}-${{ matrix.webpack }}-
- name: Set last run status
id: last_run_status
run: cat last_run_status
- name: Checkout ref
uses: actions/checkout#v2
with:
ref: ${{ github.event.workflow_dispatch.ref }}
- name: Use Node.js ${{ matrix.node-version }}
if: steps.last_run_status.outputs.last_run_status != 'success'
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
Here is the original workflow file.
GitHub finally made it possible to do this. There is now a "Re-run failed jobs" button:

Fallback in expression syntax

I have a github workflow that runs on two different events: pull_request and manually. It looks like this:
on:
pull_request:
branches: [master]
workflow_dispatch: {}
env:
API_URL: https://example.com/
What I want is to be able to set an alternate API_URL when I trigger the workflow manually. But I want the workflow to run the same when it runs automatically.
So, first, I add “inputs” to the workflow_dispatch:
on:
pull_request:
branches: [master]
workflow_dispatch:
inputs:
API_URL:
description:
required: true
default: https://example.com
env:
API_URL: ${{ github.event.API_URL }}
From what I understand, this way the API_URL env variable will only be set when the workflow is run manually. But when it’s run automatically, there’s no github.event.API_URL value.
It seems to me that a conditional fallback here is needed.
Will something like this work?
env:
API_URL: ${{ github.event.API_URL || 'https://example.com/' }}
But from the docs the || operator will evaluate to 1 or 0.
Is there a good way to solve this? Thanks!
This worked for me:
env:
BRANCH: ${{ github.base_ref || 'devel' }}
Unfortunately (at the time of writing this), it's not possible that way. But you can achieve the same effect by setting the environment variable (API_URL) to the default value dynamically only when the job is not run manually (eg. the job is triggered by the PR event). You can do that by creating a conditional job step that will run only if API_URL has not been set yet:
on:
pull_request:
branches:
- master
workflow_dispatch:
inputs:
API_URL:
required: true
default: https://example.com
jobs:
build:
env:
API_URL: ${{ github.event.inputs.API_URL }}
runs-on: ubuntu-latest
steps:
- name: Set API_URL environment variable
if: env.API_URL == null
run: echo "API_URL=https://example.com" >> $GITHUB_ENV
- name: Test API_URL environment variable
run: echo "API_URL=$API_URL"
This way we will have API_URL environment variable set regardless of the way the workflow was triggered (either manually or automatically) with the ability to override it with a custom value when run manually.

Resources