2323 is_targeting_feature_branch :
2424 description : " if it is a PR, check if targeting a feature branch"
2525 value : ${{ jobs.get-environment.outputs.is_targeting_feature_branch }}
26+ skip_workflow :
27+ description : " if the current workflow should be skipped"
28+ value : ${{ jobs.get-environment.outputs.skip_workflow }}
2629
2730jobs :
2831 get-environment :
@@ -34,10 +37,128 @@ jobs:
3437 target_stability : ${{ steps.get_stability.outputs.target_stability }}
3538 release_type : ${{ steps.get_release_type.outputs.release_type }}
3639 is_targeting_feature_branch : ${{ steps.get_stability.outputs.is_targeting_feature_branch }}
40+ skip_workflow : ${{ steps.skip_workflow.outputs.result }}
3741
3842 steps :
43+ - name : Check if PR has skip label
44+ id : has_skip_label
45+ uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
46+ with :
47+ script : |
48+ let hasSkipLabel = false;
49+ if (${{ contains(fromJSON('["pull_request", "pull_request_target"]') , github.event_name) }} === true) {
50+ try {
51+ const labels = await github.rest.issues.listLabelsOnIssue({
52+ owner: context.repo.owner,
53+ repo: context.repo.repo,
54+ issue_number: context.issue.number
55+ });
56+ labels.data.forEach(({ name }) => {
57+ if (name === '${{ format('skip-workflow-{0}', github.workflow) }}') {
58+ hasSkipLabel = true;
59+ }
60+ });
61+ } catch (e) {
62+ core.warning(`failed to list labels: ${e}`);
63+ }
64+ }
65+ return hasSkipLabel;
66+
3967 - name : Checkout sources (current branch)
4068 uses : actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
69+ with :
70+ fetch-depth : ${{ steps.has_skip_label.outputs.result == 'true' && 100 || 1 }}
71+
72+ - if : ${{ steps.has_skip_label.outputs.result == 'true' }}
73+ name : Get workflow triggered paths
74+ id : get_workflow_triggered_paths
75+ uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
76+ with :
77+ script : |
78+ const fs = require('fs');
79+
80+ let paths = [];
81+
82+ const workflowFilePath = '${{ github.workflow_ref }}'.replace('${{ github.repository }}/', '').split('@').shift();
83+
84+ if (fs.existsSync(workflowFilePath)) {
85+ const workflowFileContent = fs.readFileSync(workflowFilePath, 'utf8');
86+ const workflowFileContentLines = workflowFileContent.split('\n');
87+
88+ let hasReadOn = false;
89+ let hasReadPullRequest = false;
90+ let hasReadPaths = false;
91+ for (const line of workflowFileContentLines) {
92+ if (line.match(/^on:\s*$/)) {
93+ hasReadOn = true;
94+ continue;
95+ }
96+ if (line.match(/^\s{2}pull_request(_target)?:\s*$/)) {
97+ hasReadPullRequest = true;
98+ continue;
99+ }
100+ if (line.match(/^\s{4}paths:\s*$/)) {
101+ hasReadPaths = true;
102+ continue;
103+ }
104+
105+ if (hasReadOn && hasReadPullRequest && hasReadPaths) {
106+ const matches = line.match(/^\s{6}-\s['"](.+)['"]\s*$/);
107+ if (matches) {
108+ paths.push(matches[1].trim());
109+ } else {
110+ break;
111+ }
112+ }
113+ }
114+ }
115+
116+ if (paths.length === 0) {
117+ paths = ['**'];
118+ }
119+
120+ console.log(paths);
121+
122+ return paths;
123+
124+ - if : ${{ steps.has_skip_label.outputs.result == 'true' }}
125+ name : Get push changes
126+ id : get_push_changes
127+ uses : tj-actions/changed-files@bab30c2299617f6615ec02a68b9a40d10bd21366 # v45.0.5
128+ with :
129+ since_last_remote_commit : true
130+ json : true
131+ escape_json : false
132+ files : ${{ join(fromJSON(steps.get_workflow_triggered_paths.outputs.result), ';') }}
133+ files_separator : ' ;'
134+
135+ - name : Check if current workflow should be skipped
136+ id : skip_workflow
137+ uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
138+ with :
139+ script : |
140+ if (${{ steps.has_skip_label.outputs.result }} === false) {
141+ return false;
142+ }
143+
144+ const label = '${{ format('skip-workflow-{0}', github.workflow) }}';
145+ if ('${{ steps.get_push_changes.outputs.any_changed }}' === 'true') {
146+ try {
147+ await github.rest.issues.removeLabel({
148+ name: label,
149+ owner: context.repo.owner,
150+ repo: context.repo.repo,
151+ issue_number: context.issue.number
152+ });
153+ core.notice(`label ${label} removed because changes were detected on last push.`);
154+ } catch (e) {
155+ core.warning(`failed to remove label ${label}: ${e}`);
156+ }
157+
158+ return false;
159+ }
160+
161+ return true;
41162
42163 - if : ${{ github.event_name == 'pull_request' }}
43164 name : Get nested pull request path
@@ -155,6 +276,7 @@ jobs:
155276 ['release_type', '${{ steps.get_release_type.outputs.release_type || '<em>not defined because this is not a release</em>' }}'],
156277 ['is_targeting_feature_branch', '${{ steps.get_stability.outputs.is_targeting_feature_branch }}'],
157278 ['target_stability', '${{ steps.get_stability.outputs.target_stability || '<em>not defined because current run is not triggered by pull request event</em>' }}'],
279+ ['skip_workflow', '${{ steps.skip_workflow.outputs.result }}']
158280 ];
159281 core.summary
160282 .addHeading(`${context.workflow} environment outputs`)
0 commit comments