Skip to content

Commit

Permalink
feat: add workflow modify checker (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-matsuzawa authored Apr 6, 2021
1 parent a57e499 commit 8f67835
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/workflow_modify_checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: workflow modify checker

# This file is is check the workflow modification on the pull request.
# If this workflow is set to work, it will forcibly cancel all workflows when any workflow changes are made.
# To get this workflow working, configure the following settings:
# 1. Push this file to the default branch.
# 2. Add issue label 'update CI'.
# 3. Set github secrets 'CANCEL_WORKFLOW_TOKEN'. (Token can cancel the repository workflow.)
#
# If you want to modify any of the workflow files, set the Pull Request label to 'update CI'.
# Then re-run the canceled workflow.

on:
pull_request_target:
types: [opened, reopened, synchronize, labeled]
paths:
'.github/workflows/**'

jobs:
check-actions:
name: check actions
runs-on: ubuntu-20.04

steps:
- name: check secret
id: check_secret
run: |
if [[ -n '${{secrets.CANCEL_WORKFLOW_TOKEN}}' ]]; then
echo "::set-output name=exist_secret::true"
else
echo "::set-output name=exist_secret::false"
fi
- if: contains(github.event.pull_request.labels.*.name, 'update CI') != true && steps.check_secret.outputs.exist_secret == 'true'
name: wait 20 sec
run: sleep 20
- if: contains(github.event.pull_request.labels.*.name, 'update CI') != true && steps.check_secret.outputs.exist_secret == 'true'
name: cancel workflows
id: cancel_workflow
uses: actions/github-script@v3
with:
github-token: ${{ secrets.CANCEL_WORKFLOW_TOKEN }}
script: |
const creator = context.payload.sender.login
const opts1 = github.actions.listWorkflowRunsForRepo.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
event: 'pull_request',
status: 'queued'
})
const queuedWorkflows = await github.paginate(opts1)
const opts2 = github.actions.listWorkflowRunsForRepo.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
event: 'pull_request',
status: 'in_progress'
})
const inProgressWorkflows = await github.paginate(opts2)
const workflows = queuedWorkflows.concat(inProgressWorkflows)
//console.log(workflows);
for (const workflow of workflows) {
if (workflow.run_number == context.runNumber) continue
await github.actions.cancelWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: workflow.id
})
console.log(`cancel: ${workflow.name} (${workflow.id}),`,
`path:${workflow.head_repository.full_name}/tree/${workflow.head_branch}`);
}
throw new Error('Change workflow file. please set label "update CI".')
- if: success()
run: echo "enabled editing workflow."

0 comments on commit 8f67835

Please sign in to comment.