Skip to content

Commit

Permalink
enh(ci): skip workflow when no change on PR (#1927) (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
kduret authored Dec 19, 2024
1 parent 843c515 commit b08c2a3
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 15 deletions.
33 changes: 28 additions & 5 deletions .github/workflows/centreon-collect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ jobs:

unit-test:
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'
runs-on: [self-hosted, collect]

strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -109,7 +110,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'
uses: ./.github/workflows/package-collect.yml
with:
major_version: ${{ needs.get-environment.outputs.major_version }}
Expand Down Expand Up @@ -143,7 +146,12 @@ jobs:
token_download_centreon_com: ${{ secrets.TOKEN_DOWNLOAD_CENTREON_COM }}

deliver-rpm:
if: ${{ contains(fromJson('["testing", "stable"]'), needs.get-environment.outputs.stability) }}
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
contains(fromJson('["testing"]'), needs.get-environment.outputs.stability) &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
needs: [get-environment, package]
runs-on: [self-hosted, common]
strategy:
Expand Down Expand Up @@ -186,7 +194,12 @@ jobs:
ticket_labels: '["Nightly", "Pipeline", "nightly-${{ github.ref_name }}", "${{ github.job }}"]'

deliver-deb:
if: ${{ contains(fromJson('["testing", "stable"]'), needs.get-environment.outputs.stability) }}
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
contains(fromJson('["testing"]'), needs.get-environment.outputs.stability) &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
needs: [get-environment, package]
runs-on: [self-hosted, common]
strategy:
Expand Down Expand Up @@ -235,6 +248,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 @@ -259,3 +273,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
122 changes: 122 additions & 0 deletions .github/workflows/get-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -66,10 +69,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 @@ -79,6 +109,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 @@ -284,6 +405,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
35 changes: 31 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 @@ -137,7 +139,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 @@ -156,7 +163,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 @@ -181,7 +193,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 @@ -206,6 +223,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 @@ -230,3 +248,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
30 changes: 28 additions & 2 deletions .github/workflows/libzmq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:

package-rpm:
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 @@ -74,6 +77,9 @@ jobs:

package-deb:
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 @@ -146,8 +152,13 @@ jobs:
key: ${{ github.run_id }}-${{ github.sha }}-deb-libzmq-${{ matrix.distrib }}-${{ matrix.arch }}

deliver-rpm:
if: ${{ contains(fromJson('["testing", "unstable"]'), needs.get-environment.outputs.stability) }}
needs: [get-environment, package-rpm]
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')
runs-on: [self-hosted, common]
strategy:
matrix:
Expand Down Expand Up @@ -176,8 +187,13 @@ jobs:
is_cloud: ${{ needs.get-environment.outputs.is_cloud }}

deliver-deb:
if: ${{ contains(fromJson('["testing", "unstable"]'), needs.get-environment.outputs.stability) }}
needs: [get-environment, package-deb]
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')
runs-on: [self-hosted, common]
strategy:
matrix:
Expand Down Expand Up @@ -212,6 +228,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 @@ -236,3 +253,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 b08c2a3

Please sign in to comment.