Mark a conditional Github Action as required - github

I have a Github action in my repository that runs only when a specific file has changed. I have marked this action as required in repository settings so that whenever it fails the automatic merge gets blocked. Merge block is working fine but if a PR doesn't include a change to that specific file, Github still blocks the PR waiting for the job to complete which won't even run. Ideally, Github should ignore this action if it is not applicable.
I have put up a public repository that showcases this behavior. There are two PRs open: one that is working fine and the other one is blocked.
Any suggested workaround for this?

A workaround could be to update the workflow to run independently of the path, but check the path in the job execution with something like the path filter action, then return the message you want on the PR if the label isn't set and set an error to block the merge, or return that everything is ok if the file hasn't been updated.
Your workflow would in that case looks like below:
name: All Checked Verifier
on:
pull_request:
types: [labeled, unlabeled, opened, edited, synchronize]
jobs:
enforce-label:
runs-on: ubuntu-latest
steps:
- uses: dorny/paths-filter#v2
id: changes
with:
filters: |
public_api_views:
- '**/code.py'
- if: steps.changes.outputs.public_api_views == 'true'
uses: yogevbd/enforce-label-action#2.1.0
with:
REQUIRED_LABELS_ALL: "checked-everything"
REQUIRED_LABELS_ALL_DESCRIPTION: "Make sure we have checked everything and once done, add label 'checked-everything' to this PR."

It's weird because the https://github.com/sferhan/hello-github-actions/pull/6 not excuting the workflow
and i forked the repo https://github.com/bxb100/hello-github-actions, it's work fine... so i guess the action have problem.

Related

Using `GITHUB_REF` when triggering a reusable workflow

I'm trying to call multiple workflows from a single one and I want to use the commit sha, or the branch name or something to get the workflow file with the recent changes on the branch the workflows are triggered on.
For example, I am on a branch <feature_branch> and I want to trigger the workflow's on that branch. I want the content of the later called workflows also to be the one from that branch. For that reason, I tried the following:
// My repository structure's essential part
repo_folder
> .github
| > workflows
| | > main-ci.yml
| | > other-workflow.yml
# main-ci.yml
name: Main CI workflow
on: ...
jobs:
uses: <my_repo>/.github/workflows/other-workflow.yml#$GITHUB_REF
...
# other-workflow.yml
...
The issue is that when GitHub parses the main-ci workflow it doesn't seem to resolve the $GITHUB_REF environment variable before trying to call the workflow, and reports a problem
error parsing called workflow "<my_repo>/.github/workflows/other_workflow.yml#$GITHUB_REF": failed to fetch workflow: reference to workflow should be either a valid branch, tag, or commit
I tried with context variables too (like ${{ github.sha }}) but with that syntax, it asks for removing the spaces from the version field.
Just figured this out after heading out to the workflow syntax section of the GitHub actions' documentation. It says:
If you use the second syntax option (without {owner}/{repo} and #{ref}) the called workflow is from the same commit as the caller workflow.
And the example shows
jobs:
call-workflow-1-in-local-repo:
uses: octo-org/this-repo/.github/workflows/workflow-1.yml#172239021f7ba04fe7327647b213799853a9eb89
call-workflow-2-in-local-repo:
uses: ./.github/workflows/workflow-2.yml
call-workflow-in-another-repo:
uses: octo-org/another-repo/.github/workflows/workflow.yml#v1
So, all I needed to do is to change
uses: <my_repo>/.github/workflows/other-workflow.yml#$GITHUB_REF
to
uses: ./.github/workflows/other-workflow.yml

Update/Edit of workflow file in GitHub action

I have configured a manual workflow and it runs OK, but once I update/edit it and commit it to the same branch, the changes do not affect it. I mean the action still runs but uses the old version of the workflow file. is there any step I need to do?
Steps I followed for editing the workflow file:
https://docs.github.com/en/actions/learn-github-actions/finding-and-customizing-actions#browsing-marketplace-actions-in-the-workflow-editor
Here is workflow file details, just in case
The original:
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "development" branch
release:
types: [created]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world Nikzad!, 3
Note: Let's say I just replace the last line with the bellow line, but my output still says Hello, world Nikzad!, 3 where it should say Hello, world Nikzad!, 4.
run: echo Hello, world Nikzad!, 4
I found my problem. actually, when we are creating a new release, we are providing a tag name for that release, and when we create that tag name and push it to the repo, the version of the workflow at that point is what matters, so when I want to edit or update my workflow, I do the followings:
Edit the workflow (Make any change you want, eg change the title or add echo)
Commit and push workflow changes
Create a new tag and push
From the GitHub panel, create a new release based on the new tag
Now I see the action is running based on the new workflow
Note: Maybe it is more about learning of the process, but I did not find it straight forward anywhere, I hope it helps someone.

How to exclude certain branches for a path pattern within Github Actions?

How to apply branches-ignore and paths-ignore on GitHub Actions? For example, if I have the following source tree:
|-src\
|----reports\
|-------<foo>DailyReports.ts
I want to only deploy that to production (master branch) but NOT staging (staging branch).
Any advice and insight is appreciated.
You won't be able to achieve what you want with ON trigger configuration alone. Therefore the implementation below wouldn't work as it would trigger for push to the specified path OR for push to the master branch, but not only for both:
on:
push:
path:
- 'src/reports/*DailyReports.ts'
branches:
- master
In that case, you could do it by using only one trigger at the workflow level, and check the other condition with an IF expression.
For example by doing this:
on:
push:
path:
- 'src/reports/*DailyReports.ts'
jobs:
job1:
runs-on: ...
if: github.ref == 'refs/heads/master' # run this job only for the master branch
steps:
...
Or by checking the branch at the workflow level, and then use something like this path-filter action to check if the file or directory has been updated or not before performing some operation.
I personally suggest the first option (cf example), as it's less verbose.
Note: If the src/reports/*DailyReports.ts path isn't working as expected, you can use the filter pattern cheat sheet to find the right expression.

Running Github Actions with label name - only run for latest label added

We trigger GH workflows on PRs when labels get added.
For example if you add a label dev to the existing PR, it runs the dev workflow, but if you add another label - test, it will run that second job AS well as any job matching existing label on the PR that has a trigger associated with it (so second time it will run dev and test jobs).
This is how we check for a match:
on:
pull_request:
types:
- labeled
jobs:
dev:
if: ${{ contains(github.event.pull_request.labels.*.name, 'dev') }}
Is there any way that we can consider ONLY the label created as part of that event and ignore existing ones so that they don't end up running all existing matches every time?
Turns out you can now use
jobs:
dev:
if: ${{ github.event.label.name == 'dev' }}
Still same result, no #Ben Dubuisson?
It doesn't only take in count the last added label, but all the ones the PR contains too..

Github workflow executing unexpextedly

Ok, so I'm trying to get a workflow to execute on a specific branch, with a specific tag.
So I wrote this:
on:
push:
branches:
- 'branchname'
tags:
- 'tagname'
with the expectation that it would run whenever something on the branchname branch was taged with tagname. Instead it seems like it is being run on master whenever something is pushed, regardless of tags. Can someone explain why?