Skip to content

Open issues on workflow failure, close on success #21

Open issues on workflow failure, close on success

Open issues on workflow failure, close on success #21

name: Open issues on workflow failure, close on success
on:
workflow_call:
workflow_run:
workflows:
- Flaky
types:
- completed
jobs:
create-or-update:
name: Create or update issue
if: |
github.event.workflow_run.head_branch == github.event.repository.default_branch &&
github.event.workflow_run.conclusion == 'failure'
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
steps:
- uses: actions/checkout@v3
- name: Find template
id: template
env:
WORKFLOW_RUN_PATH: ${{ github.event.workflow_run.path }}
run: |
base=$(echo ${WORKFLOW_RUN_PATH} | rev | cut -f 1 -d '/' | rev | cut -f 1 -d .)
echo "workflow-template=.github/workflows/${base}-issue-template.md" >> $GITHUB_OUTPUT
path=".github/workflows/${base}-issue-template.md"
if [ -f "$path" ]; then
echo "template=$path" >> $GITHUB_OUTPUT
else
echo "template=.github/workflows/workflow-failure-issues-issue-template.md" >> $GITHUB_OUTPUT
fi
- name: Create issue
uses: urcomputeringpal/[email protected]
with:
filename: ${{ steps.template.outputs.template }}
# Update an existing open issue instead of creating a new one each time.
update_existing: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WORKFLOW_TEMPLATE: ${{ steps.template.output.workflow-template }}
close:
name: Close issue
if: |
github.event.workflow_run.head_branch == github.event.repository.default_branch &&
github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
steps:
- name: Close issue
uses: actions/github-script@v6
env:
WORKFLOW_NAME: ${{ github.event.workflow.name }}
with:
script: |
github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
labels: [process.env.WORKFLOW_NAME, 'actions', 'failure']
}).then((issues) => {
if (issues.data.length != 0) {
github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issues.data[0].number,
state: 'closed'
});
} else {
console.log("No issue found")
}
});