snippetMinor
How do I prevent a given GitHub Workflow from being launched twice from two almost simultaneous events?
Viewed 0 times
preventtwiceworkflowlaunchedeventsalmostgithubbeingsimultaneoustwo
Problem
I have a repo, in which there is one folder at the root with a Makefile to build a C library. In the same repo, there is another folder at the root with a folder with another Makefile for building a node C addon via Node-API, and a small JS/TS API using said addon. The NAPI addon is dependent on the C library.
I have a workflow file for the C which runs when there is a commit on the C folder, and runs with any push or PR.
I have another workflow file that runs when there is a commit+push/PR to the JS/TS folder, OR when the C workflow has completed successfully.
My goal is (of course) to only have the minimal amount of workflows run, depending on "how high in the stack the commits were". If commits touch only the C, I want both the C and NAPI addon to be checked. If commits touch only the NAPI addon, I want only the NAPI addon to be checked. If commits touch both the C and NAPI addon, I want both the C and NAPI to be checked; but for NAPI to only be checked once.
For now, when the commits touch both the C and NAPI addon, the CI for the NAPI addon is launched twice: once for the commits affecting the NAPI folder, once for the successful completion of the C workflow.
How do I fix this ?
NB: I've tried using
I have a workflow file for the C which runs when there is a commit on the C folder, and runs with any push or PR.
I have another workflow file that runs when there is a commit+push/PR to the JS/TS folder, OR when the C workflow has completed successfully.
My goal is (of course) to only have the minimal amount of workflows run, depending on "how high in the stack the commits were". If commits touch only the C, I want both the C and NAPI addon to be checked. If commits touch only the NAPI addon, I want only the NAPI addon to be checked. If commits touch both the C and NAPI addon, I want both the C and NAPI to be checked; but for NAPI to only be checked once.
For now, when the commits touch both the C and NAPI addon, the CI for the NAPI addon is launched twice: once for the commits affecting the NAPI folder, once for the successful completion of the C workflow.
How do I fix this ?
NB: I've tried using
fkirc/skip-duplicate-actions@master, but it didn't do the job.Solution
We have similar situations whereby you want the action to do different things based on the files changed within the commit. You could generate a list of files modified within the PR/commit (within your action) using something like (simplified for ease of reading):
you could then examine the list of files that are part of the commit/PR and run your logic accordingly in steps in a simple script
Logic will then dictate what runs based on what is contained in the commit also this would turn this into a single workflow from what I can see.
Hope that helps.
# export a list of changed files
export FILESCHANGED=$( curl --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' --header 'content-type: application/json' --silent https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }} | jq '.files[].filename')
# writes changed files into a text file whilst removing quotes and substituting a space for a newline (each file will appear on a separate line).
echo ${FILESCHANGED} | tr -d [\"] | sed 's/ /\n/g' >> files_changed.txt
# display the list of changed files for visibility in the workflow
echo "Files changed in this commit"
cat files_changed.txt
you could then examine the list of files that are part of the commit/PR and run your logic accordingly in steps in a simple script
if C makefile is contained in the list
run C makefile
else
do not run C makefile.
if C makefile is contained and/or if NAPI makefile is contained
run NAPI makefile
else if neither exist in list
do not run NAPI makefile.Logic will then dictate what runs based on what is contained in the commit also this would turn this into a single workflow from what I can see.
Hope that helps.
Code Snippets
if C makefile is contained in the list
run C makefile
else
do not run C makefile.
if C makefile is contained and/or if NAPI makefile is contained
run NAPI makefile
else if neither exist in list
do not run NAPI makefile.Context
StackExchange DevOps Q#13916, answer score: 1
Revisions (0)
No revisions yet.