Skip to content

Commit abce599

Browse files
committed
Update workflows to not execute jobs at all if there were no detected changes for the corresponding package
1 parent d717438 commit abce599

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

.github/actions/src/info/__tests__/index.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { Dirent } from 'fs';
22
import fs from 'fs/promises';
33
import pathlib from 'path';
4+
import * as core from '@actions/core';
45
import { describe, expect, test, vi } from 'vitest';
56
import * as git from '../../commons.js';
6-
import { getAllPackages, getRawPackages } from '../index.js';
7+
import { getAllPackages, getRawPackages, main } from '../index.js';
78

89
const mockedCheckChanges = vi.spyOn(git, 'checkForChanges');
910

@@ -212,3 +213,34 @@ describe(getAllPackages, () => {
212213
expect(results['@sourceacademy/modules-lib'].changes).toEqual(true);
213214
});
214215
});
216+
217+
describe(main, () => {
218+
vi.spyOn(git, 'getGitRoot').mockResolvedValue('root');
219+
const mockedSetOutput = vi.spyOn(core, 'setOutput');
220+
221+
vi.spyOn(core.summary, 'addHeading').mockImplementation(() => core.summary);
222+
vi.spyOn(core.summary, 'addTable').mockImplementation(() => core.summary);
223+
vi.spyOn(core.summary, 'write').mockImplementation(() => Promise.resolve(core.summary));
224+
225+
test('Does not write packages with no changes to the output', async () => {
226+
mockedCheckChanges.mockImplementation(path => {
227+
return Promise.resolve(path === 'root/src/tabs/tab0');
228+
});
229+
230+
await main();
231+
const { mock: { calls } } = mockedSetOutput;
232+
233+
expect(mockedSetOutput).toHaveBeenCalledTimes(6);
234+
235+
expect(calls[0]).toEqual(['bundles', []]);
236+
expect(calls[1]).toEqual(['tabs', [expect.objectContaining({ changes: true })]]);
237+
expect(calls[2]).toEqual(['libs', []]);
238+
239+
// These next two are undefined because the mock implementations
240+
// don't return any info about them
241+
expect(calls[3]).toEqual(['devserver', undefined]);
242+
expect(calls[4]).toEqual(['docserver', undefined]);
243+
244+
expect(calls[5]).toEqual(['workflows', false]);
245+
});
246+
});

.github/actions/src/info/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ function setOutputs(
208208
devserver: PackageRecord,
209209
docserver: PackageRecord
210210
) {
211-
core.setOutput('bundles', bundles);
212-
core.setOutput('tabs', tabs);
213-
core.setOutput('libs', libs);
211+
core.setOutput('bundles', bundles.filter(x => x.changes));
212+
core.setOutput('tabs', tabs.filter(x => x.changes));
213+
core.setOutput('libs', libs.filter(x => x.changes));
214214
core.setOutput('devserver', devserver);
215215
core.setOutput('docserver', docserver);
216216
}
217217

218-
async function main() {
218+
export async function main() {
219219
const gitRoot = await getGitRoot();
220220
const { packages, bundles, tabs, libs } = await getAllPackages(gitRoot);
221221

.github/workflows/pull-request.yml

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Check out source code
1818
uses: actions/checkout@v5
1919

20-
- name: Initialize Repo
20+
- name: Initialize Github Actions
2121
uses: ./.github/actions/src/init
2222
with:
2323
package-name: '@sourceacademy/modules-github-actions'
@@ -63,11 +63,9 @@ jobs:
6363

6464
steps:
6565
- name: Check out source code
66-
if: matrix.lib.changes
6766
uses: actions/checkout@v5
6867

69-
- name: Initialize Repo
70-
if: matrix.lib.changes
68+
- name: Initialize ${{ matrix.lib.name }}
7169
uses: ./.github/actions/src/init
7270
with:
7371
package-name: ${{ matrix.lib.name }}
@@ -76,15 +74,15 @@ jobs:
7674

7775
# Only thing we really need to do on Windows is run tests (without coverage)
7876
- name: Run Tests (Windows)
79-
if: matrix.lib.changes && matrix.os == 'windows-latest'
77+
if: matrix.os == 'windows-latest'
8078
run: yarn workspaces foreach -A --include ${{ matrix.lib.name }} run test
8179

8280
- name: Run Tests (Ubuntu)
83-
if: matrix.lib.changes && matrix.os == 'ubuntu-latest'
81+
if: matrix.os == 'ubuntu-latest'
8482
run: yarn workspaces foreach -A --include ${{ matrix.lib.name }} run test --coverage
8583

8684
- name: Run Auxillary Tasks
87-
if: matrix.lib.changes && matrix.os == 'ubuntu-latest'
85+
if: matrix.os == 'ubuntu-latest'
8886
run: yarn workspaces foreach -A --include ${{ matrix.lib.name }} run tsc
8987

9088
tabs:
@@ -97,24 +95,20 @@ jobs:
9795
name: ${{ matrix.tabInfo.tabName }} Tab
9896
steps:
9997
- name: Check out source code
100-
if: matrix.tabInfo.changes
10198
uses: actions/checkout@v5
10299

103-
- name: Initialize Repo
104-
if: matrix.tabInfo.changes
100+
- name: Initialize ${{ matrix.tabInfo.tabName }}
105101
uses: ./.github/actions/src/init
106102
with:
107103
package-name: ${{ matrix.tabInfo.name }}
108104
playwright: ${{ matrix.tabInfo.needsPlaywright && matrix.tabInfo.changes }}
109105

110106
- name: Build Tab
111-
if: matrix.tabInfo.changes
112107
run: |
113108
cd ${{ matrix.tabInfo.directory }}
114109
yarn build
115110
116111
- name: Upload Tab Artifact
117-
if: matrix.tabInfo.changes
118112
uses: actions/upload-artifact@v4
119113
with:
120114
name: ${{ matrix.tabInfo.tabName }}-tab
@@ -124,13 +118,11 @@ jobs:
124118
# https://github.com/vitest-dev/vitest/issues/5477
125119
# Known Vitest issue for coverage running in browser mode
126120
# Momentarily disable coverage checking on Github Actions for tabs
127-
if: matrix.tabInfo.changes
128121
run: |
129122
cd ${{ matrix.tabInfo.directory }}
130123
yarn test
131124
132125
- name: Run Auxillary Tasks
133-
if: matrix.tabInfo.changes
134126
run: |
135127
cd ${{ matrix.tabInfo.directory }}
136128
yarn tsc
@@ -145,31 +137,26 @@ jobs:
145137
name: ${{ matrix.bundleInfo.bundleName }} Bundle
146138
steps:
147139
- name: Check out source code
148-
if: matrix.bundleInfo.changes
149140
uses: actions/checkout@v5
150141

151-
- name: Initialize Repo
152-
if: matrix.bundleInfo.changes
142+
- name: Initialize ${{ matrix.bundleInfo.name }}
153143
uses: ./.github/actions/src/init
154144
with:
155145
package-name: ${{ matrix.bundleInfo.name }}
156146
playwright: ${{ matrix.bundleInfo.needsPlaywright }}
157147

158148
- name: Build Bundle
159-
if: matrix.bundleInfo.changes
160149
run: |
161150
cd ${{ matrix.bundleInfo.directory }}
162151
yarn build
163152
yarn tsc
164153
165154
- name: Build Bundle Docs
166-
if: matrix.bundleInfo.changes
167155
run: |
168156
cd ${{ matrix.bundleInfo.directory }}
169157
yarn buildtools build docs
170158
171159
- name: Run Tests
172-
if: matrix.bundleInfo.changes
173160
run: |
174161
cd ${{ matrix.bundleInfo.directory }}
175162
yarn test --coverage

0 commit comments

Comments
 (0)