Skip to content

on workflow run

on workflow run #3269

name: on workflow run
on:
workflow_run:
workflows: ["on pull request"]
types:
- completed
env:
RUN_NUMBER: ${{ github.run_number }}
jobs:
# Stop previous runs
stop-previous-run:
runs-on: ubuntu-22.04
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.12.1
with:
access_token: ${{ secrets.GITHUB_TOKEN }}
check-if-allure-artifacts-exist:
runs-on: ubuntu-22.04
needs: stop-previous-run
outputs:
artifacts-exist: ${{ steps.check-artifacts-exist.outputs.artifacts-exist }}
steps:
- name: Check if allure artifacts exist
id: check-artifacts-exist
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }}
});
const matchArtifact = artifacts.data.artifacts.find(artifact => artifact.name === 'allure-results');
if (!matchArtifact) {
core.setOutput('artifacts-exist', 'false');
} else {
core.setOutput('artifacts-exist', 'true');
}
generate-allure-report:
runs-on: ubuntu-22.04
needs: check-if-allure-artifacts-exist
if: ${{ needs.check-if-allure-artifacts-exist.outputs.artifacts-exist != 'false' }}
steps:
- name: Dowload artifacts
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }}
});
const matchArtifact = artifacts.data.artifacts.find(artifact => artifact.name === 'allure-results');
if (!matchArtifact) {
throw new Error('Artifact not found');
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip'
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/allure-results.zip', Buffer.from(download.data));
- run: unzip allure-results.zip
- name: Get Allure history
uses: actions/checkout@v3
if: always()
continue-on-error: true
with:
ref: gh-pages
path: gh-pages
- name: Allure Report action from marketplace
uses: simple-elf/allure-report-action@master
if: always()
with:
allure_results: ./
allure_history: allure-history
keep_reports: 30
- name: Deploy report to Github Pages
if: always()
uses: peaceiris/actions-gh-pages@v2
env:
PERSONAL_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: allure-history
# Comment to PR, Add labels to PR, Remove unnecessary labels
actions-with-pr:
runs-on: ubuntu-22.04
needs: generate-allure-report
if: always() && github.event.workflow_run.event == 'pull_request'
steps:
- name: 'Download artifact'
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }}
});
const matchArtifact = artifacts.data.artifacts.find(artifact => artifact.name === 'env_for_comment');
if (!matchArtifact) {
throw new Error('Artifact not found');
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip'
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/env_for_comment.zip', Buffer.from(download.data));
- run: |
unzip env_for_comment.zip
echo ${{ needs.generate-allure-report.result }}
- name: 'Comment to PR -- if report generated'
if: needs.generate-allure-report.result == 'success'
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const runNumber = process.env.RUN_NUMBER;
const conclusion = process.env.WORKFLOW_CONCLUSION;
const runId = Number(fs.readFileSync('./runId'));
const repoFullName = fs.readFileSync('./repoFullName');
const ownerName = fs.readFileSync('./ownerName');
const githubPagesLink = 'https://' + ownerName.toString().toLowerCase().trim() + '.github.io/' + String(repoFullName).replace(`${ownerName.toString().trim()}/`, '') + '/';
const githubRunLink = 'https://github.com/' + repoFullName + '/actions/runs/' + runId;
await github.rest.issues.createComment({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Workflow status is ${conclusion === 'success'? conclusion + ' \u2705' : conclusion + ' \u274C'} \n Checkout tests results here: <a href="${githubPagesLink}${runNumber}">Cypress e2e Test Report</a> \n Link to GitHub workflow: <a href="${githubRunLink}">Github Workflow Link</a> \n❕Note: Report deployment may take up to 10 minutes.`
})
- name: 'Comment to PR -- if report wasnt generated'
if: needs.generate-allure-report.result == 'skipped'
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const runNumber = process.env.RUN_NUMBER;
const conclusion = process.env.WORKFLOW_CONCLUSION;
const runId = Number(fs.readFileSync('./runId'));
const repoFullName = fs.readFileSync('./repoFullName');
const ownerName = fs.readFileSync('./ownerName');
const githubPagesLink = 'https://' + ownerName.toString().toLowerCase().trim() + '.github.io/' + String(repoFullName).replace(`${ownerName.toString().trim()}/`, '') + '/';
const githubRunLink = 'https://github.com/' + repoFullName + '/actions/runs/' + runId;
await github.rest.issues.createComment({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Workflow status is ${conclusion === 'success'? conclusion + ' \u2705' : conclusion === 'cancelled'? conclusion + ' \u2757' : conclusion + ' \u274C'} \n Unfortunately test report wasn't generated, it can be if no workspaces changed. Please check the workflow run below. \n Link to GitHub workflow: <a href="${githubRunLink}">Github Workflow Link</a>`
})
- name: Remove old lables
if: always()
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
GENERAT_ALLURE_REPORT_STATUS: ${{ needs.generate-allure-report.result }}
with:
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const conclusion = process.env.WORKFLOW_CONCLUSION;
const generateAllureReportStatus = process.env.GENERAT_ALLURE_REPORT_STATUS;
const labels = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number
});
if (labels) {
for (const label of labels.data) {
if ((label.name == `e2e-${conclusion}` && generateAllureReportStatus == 'success') || label.name == `workflow-${conclusion}`) {
console.log(`Label ${label.name} already exists`);
continue;
}
if (label.name.startsWith("workflow-") || label.name.startsWith("e2e-")) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
name: label.name
});
}
}
} else {
console.log('No labels found');
}
- name: 'Add e2e label to PR -- if report generated'
if: needs.generate-allure-report.result == 'success'
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const conclusion = process.env.WORKFLOW_CONCLUSION;
await github.rest.issues.addLabels({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [`e2e-${conclusion}`]
})
- name: 'Add workflow label to PR'
if: always()
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const conclusion = process.env.WORKFLOW_CONCLUSION;
await github.rest.issues.addLabels({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [`workflow-${conclusion}`]
})