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 }}