Skip to content

Commit f73d3ab

Browse files
authored
Merge pull request #4459 from bcgov/NDT-1181-Skip-CI-CD-on-release
Ndt 1181 skip ci cd on release
2 parents c54ea4d + efeed13 commit f73d3ab

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed

.github/workflows/build.yaml

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,75 @@ jobs:
5252
build_only_on_main: false
5353
permissions:
5454
contents: read
55+
actions: read
5556
packages: write
5657
name: ${{ matrix.name }}
5758
steps:
59+
- name: Release commit skip check
60+
id: release-check
61+
uses: actions/github-script@v8
62+
with:
63+
script: |
64+
const { owner, repo } = context.repo;
65+
const isReleaseCommit = (message) => (message || '').includes('chore: release');
66+
let message = '';
67+
68+
if (context.payload.pull_request?.head?.sha) {
69+
const { data } = await github.rest.repos.getCommit({
70+
owner,
71+
repo,
72+
ref: context.payload.pull_request.head.sha
73+
});
74+
message = data?.commit?.message || '';
75+
} else if (context.payload.head_commit?.message) {
76+
message = context.payload.head_commit.message;
77+
} else {
78+
const { data } = await github.rest.repos.getCommit({ owner, repo, ref: context.sha });
79+
message = data?.commit?.message || '';
80+
}
81+
82+
const skipRelease = isReleaseCommit(message);
83+
core.info(`Release skip check message: ${message}`);
84+
core.setOutput('skip_release', skipRelease.toString());
85+
86+
- name: Debug last successful build run for PR
87+
if: ${{ github.event_name == 'pull_request' && matrix.name == 'app' }}
88+
uses: actions/github-script@v8
89+
with:
90+
script: |
91+
const { owner, repo } = context.repo;
92+
const pr = context.payload.pull_request;
93+
if (!pr) {
94+
core.info('No PR context; skipping last successful run lookup.');
95+
return;
96+
}
97+
98+
const workflowId = 'build.yaml';
99+
const runs = await github.rest.actions.listWorkflowRuns({
100+
owner,
101+
repo,
102+
workflow_id: workflowId,
103+
event: 'pull_request',
104+
per_page: 50
105+
});
106+
107+
const matchingRuns = runs.data.workflow_runs.filter((run) =>
108+
run.conclusion === 'success' &&
109+
run.pull_requests?.some((p) => p.number === pr.number)
110+
);
111+
112+
const last = matchingRuns[0];
113+
if (!last) {
114+
core.info(`No successful ${workflowId} runs found for PR #${pr.number}.`);
115+
return;
116+
}
117+
118+
core.info(`Last successful ${workflowId} run for PR #${pr.number}:`);
119+
core.info(`- run id: ${last.id}`);
120+
core.info(`- head sha: ${last.head_sha}`);
121+
core.info(`- updated at: ${last.updated_at}`);
122+
core.info(`- html url: ${last.html_url}`);
123+
58124
- name: Checkout
59125
uses: actions/checkout@v6
60126
- name: Set up QEMU
@@ -77,7 +143,7 @@ jobs:
77143
password: ${{ secrets.GITHUB_TOKEN }}
78144

79145
- name: Build and push Docker image
80-
if: ${{ matrix.build_only_on_main != true || github.ref == 'refs/heads/main' || matrix.name == 'app' }}
146+
if: ${{ (matrix.build_only_on_main != true || github.ref == 'refs/heads/main' || matrix.name == 'app') && !(steps.release-check.outputs.skip_release == 'true' && matrix.name == 'app' && github.ref != 'refs/heads/main') }}
81147
uses: docker/build-push-action@v6
82148
with:
83149
context: ${{ matrix.context }}

.github/workflows/main.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ jobs:
5656
RENOVATE_PRIVATE_KEY: ${{ secrets.RENOVATE_PRIVATE_KEY }}
5757

5858
setup-s3-backup:
59+
if: github.event_name != 'pull_request' || github.event.pull_request.user.login != 'dependabot[bot]'
5960
uses: ./.github/workflows/s3-backup.yaml
6061
secrets:
6162
AWS_ARN_DEV: ${{ secrets.AWS_ARN_DEV }}
@@ -225,7 +226,7 @@ jobs:
225226
TEST_PG_PASSWORD: ${{ secrets.TEST_PG_PASSWORD }}
226227

227228
deploy-feature:
228-
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
229+
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false && github.event.pull_request.user.login != 'dependabot[bot]'
229230
needs: [build]
230231
uses: ./.github/workflows/deploy_feature.yaml
231232
secrets:

.github/workflows/test-code.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,88 @@ jobs:
1717
jest:
1818
runs-on: ubuntu-latest
1919
steps:
20+
- name: Release commit skip check
21+
id: release-check
22+
uses: actions/github-script@v8
23+
with:
24+
script: |
25+
const { owner, repo } = context.repo;
26+
const isReleaseCommit = (message) => (message || '').includes('chore: release');
27+
let message = '';
28+
29+
if (context.payload.pull_request?.head?.sha) {
30+
const { data } = await github.rest.repos.getCommit({
31+
owner,
32+
repo,
33+
ref: context.payload.pull_request.head.sha
34+
});
35+
message = data?.commit?.message || '';
36+
} else if (context.payload.head_commit?.message) {
37+
message = context.payload.head_commit.message;
38+
} else {
39+
const { data } = await github.rest.repos.getCommit({ owner, repo, ref: context.sha });
40+
message = data?.commit?.message || '';
41+
}
42+
43+
const skipRelease = isReleaseCommit(message);
44+
core.info(`Release skip check message: ${message}`);
45+
core.setOutput('skip_release', skipRelease.toString());
46+
47+
- name: Debug last successful test code run for PR
48+
if: ${{ github.event_name == 'pull_request' }}
49+
uses: actions/github-script@v8
50+
with:
51+
script: |
52+
const { owner, repo } = context.repo;
53+
const pr = context.payload.pull_request;
54+
if (!pr) {
55+
core.info('No PR context; skipping last successful run lookup.');
56+
return;
57+
}
58+
59+
const workflowId = 'test-code.yaml';
60+
const runs = await github.rest.actions.listWorkflowRuns({
61+
owner,
62+
repo,
63+
workflow_id: workflowId,
64+
event: 'pull_request',
65+
per_page: 50
66+
});
67+
68+
const matchingRuns = runs.data.workflow_runs.filter((run) =>
69+
run.conclusion === 'success' &&
70+
run.pull_requests?.some((p) => p.number === pr.number)
71+
);
72+
73+
const last = matchingRuns[0];
74+
if (!last) {
75+
core.info(`No successful ${workflowId} runs found for PR #${pr.number}.`);
76+
return;
77+
}
78+
79+
core.info(`Last successful ${workflowId} run for PR #${pr.number}:`);
80+
core.info(`- run id: ${last.id}`);
81+
core.info(`- head sha: ${last.head_sha}`);
82+
core.info(`- updated at: ${last.updated_at}`);
83+
core.info(`- html url: ${last.html_url}`);
84+
2085
- uses: actions/checkout@v6
2186
with:
2287
# Disabling shallow clone is recommended for improving relevancy of reporting
2388
fetch-depth: 0
2489
- name: dev env setup
90+
if: ${{ !(steps.release-check.outputs.skip_release == 'true' && github.ref != 'refs/heads/main') }}
2591
uses: ./.github/actions/dev-env-setup
2692
- name: run jest tests with coverage
93+
if: ${{ !(steps.release-check.outputs.skip_release == 'true' && github.ref != 'refs/heads/main') }}
2794
uses: Wandalen/wretry.action@master
2895
with:
2996
current_path: ./app
3097
command: yarn test --coverage --silent
3198
attempt_limit: 2
3299
attempt_delay: 2000
33100
- name: SonarCloud Scan
101+
if: ${{ !(steps.release-check.outputs.skip_release == 'true' && github.ref != 'refs/heads/main') }}
34102
uses: SonarSource/sonarqube-scan-action@v7.0.0
35103
env:
36104
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)