Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(ci): skip workflow when no change on PR (#1927) #1943

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions .github/workflows/centreon-collect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,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 @@ -84,7 +85,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 @@ -117,7 +120,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 @@ -147,7 +155,12 @@ jobs:
is_cloud: ${{ needs.get-environment.outputs.is_cloud }}

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 @@ -175,8 +188,13 @@ jobs:
is_cloud: ${{ needs.get-environment.outputs.is_cloud }}

promote:
needs: [get-environment]
if: ${{ contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch' }}
needs: [get-environment, deliver-rpm, deliver-deb]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
(contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch') &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
runs-on: [self-hosted, common]
strategy:
matrix:
Expand All @@ -197,3 +215,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 @@ -92,3 +95,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 @@ -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 @@ -257,6 +378,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
43 changes: 37 additions & 6 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 @@ -95,7 +97,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 @@ -114,7 +121,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 @@ -139,7 +151,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 @@ -162,8 +179,13 @@ jobs:
is_cloud: ${{ needs.get-environment.outputs.is_cloud }}

promote:
needs: [get-environment]
if: ${{ contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch' }}
needs: [get-environment, deliver-rpm, deliver-deb]
if: |
needs.get-environment.outputs.skip_workflow == 'false' &&
(contains(fromJson('["stable"]'), needs.get-environment.outputs.stability) && github.event_name != 'workflow_dispatch') &&
! cancelled() &&
! contains(needs.*.result, 'failure') &&
! contains(needs.*.result, 'cancelled')
runs-on: [self-hosted, common]
strategy:
matrix:
Expand All @@ -184,3 +206,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