Skip to content

Commit

Permalink
enh(ci): skip workflow when no change on PR (#1927) (#1940)
Browse files Browse the repository at this point in the history
  • Loading branch information
kduret authored Dec 19, 2024
1 parent 58ffb92 commit 55ddcd3
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 14 deletions.
26 changes: 22 additions & 4 deletions .github/workflows/centreon-collect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ jobs:

unit-test:
needs: [get-environment]
if: ${{ github.event.inputs.unit_tests == 'true' && ! contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) }}

if: |
github.event.inputs.unit_tests == 'true' &&
needs.get-environment.outputs.skip_workflow == 'false' &&
needs.get-environment.outputs.stability != 'stable'
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -240,7 +242,9 @@ jobs:

package:
needs: [get-environment]
if: ${{ ! contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) }}
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
needs.get-environment.outputs.stability != 'stable'
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -292,6 +296,8 @@ jobs:
robot-test:
needs: [get-environment, package]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
needs.get-environment.outputs.stability != 'stable' &&
(github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.is_nightly == 'true')) &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
Expand Down Expand Up @@ -364,7 +370,7 @@ jobs:
needs: [get-environment, package]
if: |
github.event_name != 'workflow_dispatch' &&
contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) &&
needs.get-environment.outputs.stability == 'stable' &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
Expand All @@ -387,6 +393,7 @@ jobs:

deliver-rpm:
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
Expand Down Expand Up @@ -421,6 +428,7 @@ jobs:

deliver-deb:
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
Expand Down Expand Up @@ -454,6 +462,7 @@ jobs:
promote:
needs: [get-environment, deliver-rpm, deliver-deb]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
(contains(fromJson('["stable", "testing"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch') &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
Expand All @@ -478,3 +487,12 @@ jobs:
github_ref_name: ${{ github.ref_name }}
release_type: ${{ needs.get-environment.outputs.release_type }}
is_cloud: ${{ needs.get-environment.outputs.is_cloud }}

set-skip-label:
needs: [get-environment, deliver-rpm, deliver-deb, promote]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
uses: ./.github/workflows/set-pull-request-skip-label.yml
14 changes: 13 additions & 1 deletion .github/workflows/docker-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ jobs:
with:
version_file: CMakeLists.txt

create-and-push-docker:
dockerize:
needs: [get-environment]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
needs.get-environment.outputs.stability != 'stable'
strategy:
fail-fast: false
Expand Down Expand Up @@ -122,3 +125,12 @@ jobs:
pull: true
push: true
tags: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/${{ matrix.image }}:${{ matrix.tag }}

set-skip-label:
needs: [get-environment, dockerize]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
uses: ./.github/workflows/set-pull-request-skip-label.yml
12 changes: 12 additions & 0 deletions .github/workflows/docker-gorgone-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:

dockerize:
needs: [get-environment]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
needs.get-environment.outputs.stability != 'stable'
runs-on: ubuntu-24.04

strategy:
Expand Down Expand Up @@ -51,3 +54,12 @@ jobs:
pull: true
push: true
tags: ${{ vars.DOCKER_INTERNAL_REGISTRY_URL }}/gorgone-testing-${{ matrix.distrib }}:${{ needs.get-environment.outputs.gorgone_docker_version }}

set-skip-label:
needs: [get-environment, dockerize]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
uses: ./.github/workflows/set-pull-request-skip-label.yml
122 changes: 122 additions & 0 deletions .github/workflows/get-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ on:
gorgone_docker_version:
description: "md5 of gorgone dockerfile"
value: ${{ jobs.get-environment.outputs.gorgone_docker_version }}
skip_workflow:
description: "if the current workflow should be skipped"
value: ${{ jobs.get-environment.outputs.skip_workflow }}

jobs:
get-environment:
Expand All @@ -59,10 +62,37 @@ jobs:
img_version: ${{ steps.get_docker_images_version.outputs.img_version }}
test_img_version: ${{ steps.get_docker_images_version.outputs.test_img_version }}
gorgone_docker_version: ${{ steps.get_docker_images_version.outputs.gorgone_docker_version }}
skip_workflow: ${{ steps.skip_workflow.outputs.result }}

steps:
- name: Check if PR has skip label
id: has_skip_label
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
let hasSkipLabel = false;
if (${{ contains(fromJSON('["pull_request", "pull_request_target"]') , github.event_name) }} === true) {
try {
const labels = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
labels.data.forEach(({ name }) => {
if (name === '${{ format('skip-workflow-{0}', github.workflow) }}') {
hasSkipLabel = true;
}
});
} catch (e) {
core.warning(`failed to list labels: ${e}`);
}
}
return hasSkipLabel;
- name: Checkout sources (current branch)
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
with:
fetch-depth: ${{ steps.has_skip_label.outputs.result == 'true' && 100 || 1 }}

# get latest major version to detect cloud / on-prem versions
- name: Checkout sources (develop branch)
Expand All @@ -72,6 +102,97 @@ jobs:
path: centreon-develop
sparse-checkout: .version

- if: ${{ steps.has_skip_label.outputs.result == 'true' }}
name: Get workflow triggered paths
id: get_workflow_triggered_paths
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const fs = require('fs');
let paths = [];
const workflowFilePath = '${{ github.workflow_ref }}'.replace('${{ github.repository }}/', '').split('@').shift();
if (fs.existsSync(workflowFilePath)) {
const workflowFileContent = fs.readFileSync(workflowFilePath, 'utf8');
const workflowFileContentLines = workflowFileContent.split('\n');
let hasReadOn = false;
let hasReadPullRequest = false;
let hasReadPaths = false;
for (const line of workflowFileContentLines) {
if (line.match(/^on:\s*$/)) {
hasReadOn = true;
continue;
}
if (line.match(/^\s{2}pull_request(_target)?:\s*$/)) {
hasReadPullRequest = true;
continue;
}
if (line.match(/^\s{4}paths:\s*$/)) {
hasReadPaths = true;
continue;
}
if (hasReadOn && hasReadPullRequest && hasReadPaths) {
const matches = line.match(/^\s{6}-\s['"](.+)['"]\s*$/);
if (matches) {
paths.push(matches[1].trim());
} else {
break;
}
}
}
}
if (paths.length === 0) {
paths = ['**'];
}
console.log(paths);
return paths;
- if: ${{ steps.has_skip_label.outputs.result == 'true' }}
name: Get push changes
id: get_push_changes
uses: tj-actions/changed-files@bab30c2299617f6615ec02a68b9a40d10bd21366 # v45.0.5
with:
since_last_remote_commit: true
json: true
escape_json: false
files: ${{ join(fromJSON(steps.get_workflow_triggered_paths.outputs.result), ';') }}
files_separator: ';'

- name: Check if current workflow should be skipped
id: skip_workflow
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
if (${{ steps.has_skip_label.outputs.result }} === false) {
return false;
}
const label = '${{ format('skip-workflow-{0}', github.workflow) }}';
if ('${{ steps.get_push_changes.outputs.any_changed }}' === 'true') {
try {
await github.rest.issues.removeLabel({
name: label,
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
core.notice(`label ${label} removed because changes were detected on last push.`);
} catch (e) {
core.warning(`failed to remove label ${label}: ${e}`);
}
return false;
}
return true;
- name: Store latest major version
id: latest_major_version
run: |
Expand Down Expand Up @@ -261,6 +382,7 @@ jobs:
['img_version', '${{ steps.get_docker_images_version.outputs.img_version }}'],
['test_img_version', '${{ steps.get_docker_images_version.outputs.test_img_version }}'],
['gorgone_docker_version', '${{ steps.get_docker_images_version.outputs.gorgone_docker_version }}'],
['skip_workflow', '${{ steps.skip_workflow.outputs.result }}'],
];
outputTable.push(['target_stability', '${{ steps.get_stability.outputs.target_stability || '<em>not defined because current run is not triggered by pull request event</em>' }}']);
Expand Down
38 changes: 34 additions & 4 deletions .github/workflows/gorgone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ jobs:

package:
needs: [get-environment]
if: ${{ needs.get-environment.outputs.stability != 'stable' }}
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
needs.get-environment.outputs.stability != 'stable'
strategy:
fail-fast: false
Expand Down Expand Up @@ -133,6 +135,9 @@ jobs:

test-gorgone:
needs: [get-environment, package]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
needs.get-environment.outputs.stability != 'stable'
strategy:
fail-fast: false
Expand Down Expand Up @@ -245,7 +250,12 @@ jobs:
deliver-sources:
runs-on: [self-hosted, common]
needs: [get-environment, package]
if: ${{ contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch' }}
if: |
github.event_name != 'workflow_dispatch' &&
needs.get-environment.outputs.stability == 'stable' &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
steps:
- name: Checkout sources
Expand All @@ -264,7 +274,12 @@ jobs:
deliver-rpm:
runs-on: [self-hosted, common]
needs: [get-environment, package]
if: ${{ contains(fromJson('["testing", "unstable"]'), needs.get-environment.outputs.stability) }}
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
strategy:
matrix:
Expand All @@ -289,7 +304,12 @@ jobs:
deliver-deb:
runs-on: [self-hosted, common]
needs: [get-environment, package]
if: ${{ contains(fromJson('["testing", "unstable"]'), needs.get-environment.outputs.stability) }}
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
contains(fromJson('["unstable", "testing"]'), needs.get-environment.outputs.stability) &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
strategy:
matrix:
Expand All @@ -314,6 +334,7 @@ jobs:
promote:
needs: [get-environment, deliver-rpm, deliver-deb]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
(contains(fromJson('["stable", "testing"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch') &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
Expand All @@ -338,3 +359,12 @@ jobs:
github_ref_name: ${{ github.ref_name }}
release_type: ${{ needs.get-environment.outputs.release_type }}
is_cloud: ${{ needs.get-environment.outputs.is_cloud }}

set-skip-label:
needs: [get-environment, deliver-rpm, deliver-deb, promote]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
uses: ./.github/workflows/set-pull-request-skip-label.yml
Loading

0 comments on commit 55ddcd3

Please sign in to comment.