Reputation: 2921
With a workflow file like this:
on: [push, pull_request]
runs are triggered for each single commit in a pull request.
Is it possible to only trigger once, for the pull request (including all commits) as a whole?
I just got literally hundreds of runs for a bigger pull request...
Upvotes: 9
Views: 4705
Reputation: 51
jobs:
my_job:
runs-on: ubuntu-latest
steps:
- name: Check if workflow has already run
id: check_workflow_status
uses: actions/github-script@v4
with:
script: |
const response = await github.repos.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
event: 'push',
branch: 'main',
per_page: 1,
status: 'completed'
});
const completedWorkflows = response.data.workflow_runs.filter(run => run.conclusion === 'success');
const hasCompletedWorkflow = completedWorkflows.length > 0;
console.log(`Has completed workflow? ${hasCompletedWorkflow}`);
if (hasCompletedWorkflow) {
core.setFailed('Workflow has already been run.');
} else {
console.log('Workflow can continue.');
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Upvotes: 2
Reputation: 43206
From the Webhook events page:
By default, all activity types trigger a workflow to run. You can limit your workflow runs to specific activity types using the
types
keyword. For more information, see "Workflow syntax for GitHub Actions."
The pull_request
event has many activities associated with it that trigger any actions that listen for that event. Activities such as synchronize
or edited
might contribute to the reasons why your action is being called whenever the pull request is modified.
You can limit the activity types using the types
list. For example:
on:
pull_request:
types: [opened]
In the above case, the action will only run when a pull request is opened. You can add more to this list as you see fit.
Upvotes: 13