Skip to content

Open issues on workflow failure, close on success #44

Open issues on workflow failure, close on success

Open issues on workflow failure, close on success #44

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@v4
with:
show-progress: false
- 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 "basename=${base}" >> $GITHUB_OUTPUT
echo "template=.github/workflows/${base}-issue-template.md" >> $GITHUB_OUTPUT
path=".github/workflows/${base}-issue-template.md"
if [ -f "$path" ]; then
echo "filename=$path" >> $GITHUB_OUTPUT
else
cd .github/workflows
wget https://raw.githubusercontent.com/urcomputeringpal/workflow-failure-issues/v0/.github/workflows/workflow-failure-issues-issue-template.md
echo "filename=.github/workflows/workflow-failure-issues-issue-template.md" >> $GITHUB_OUTPUT
fi
- name: Create issue
uses: urcomputeringpal/[email protected]
with:
filename: ${{ steps.template.outputs.filename }}
# 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.outputs.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")
}
});