Skip to content

Commit

Permalink
feat(generate-matrix): add generate-matrix helper (#351)
Browse files Browse the repository at this point in the history
* feat(generate-matrix): add generate-matrix helper

* make batches optional
  • Loading branch information
danadajian authored Mar 15, 2023
1 parent b94eb72 commit 74438fc
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 1 deletion.
43 changes: 43 additions & 0 deletions .github/workflows/generate-matrix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Generate Matrix

on:
pull_request:
branches: [ main ]
paths:
- 'src/helpers/generate-matrix.ts'

jobs:
scheduler:
name: Determine packages to build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- uses: ./
id: matrix
with:
helper: generate-matrix
paths: |
test/path/1
test/path/2
test/path/3
outputs:
matrix: ${{ steps.matrix.outputs.output }}

build:
runs-on: ubuntu-latest
needs: scheduler
strategy:
matrix: ${{ fromJson(needs.scheduler.outputs.matrix) }}
steps:
- run: echo "Run each job using ${{ matrix.path }}"

build-status:
runs-on: ubuntu-latest
if: always()
needs: build
steps:
- name: Check build status
run: exit ${{ (needs.build.result == 'failure' || needs.build.result == 'cancelled') && 1 || 0 }}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ The following parameters can be used for additional control over when it is safe
### [filter-paths](.github/workflows/filter-paths.yml)
* Returns `true` if specified file paths have changed for a PR, and `false` otherwise

### [generate-matrix](.github/workflows/generate-matrix.yml)
* Returns a job matrix JSON for dynamically running workflows

### [generate-path-matrix](.github/workflows/generate-path-matrix.yml)
* Returns a job matrix JSON for dynamically running workflows only for changed file paths
* Can be used to parallelize similar jobs, which can be useful in a monorepo environment. More information on matrix strategies can be found [here](https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix)
Expand Down
73 changes: 73 additions & 0 deletions dist/839.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/839.index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20083,6 +20083,14 @@ var map = {
228,
794
],
"./generate-matrix": [
1839,
839
],
"./generate-matrix.ts": [
1839,
839
],
"./generate-path-matrix": [
2533,
832,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions src/helpers/generate-matrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { HelperInputs } from '../types/generated';
import { chunk } from 'lodash';

export class GenerateMatrix extends HelperInputs {
paths = '';
batches?: string;
}

export const generateMatrix = ({ paths, batches = '1' }: GenerateMatrix) => {
const matrixValues = paths.split(/[\n,]/);
return {
include: chunk(matrixValues, Math.ceil(matrixValues.length / Number(batches))).map(chunk => ({ path: chunk.join(',') }))
};
};
39 changes: 39 additions & 0 deletions test/helpers/generate-matrix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { generateMatrix } from '../../src/helpers/generate-matrix';

jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
getOctokit: jest.fn()
}));

describe('generateMatrix', () => {
it('should generate matrix json with appropriate batching', () => {
const result = generateMatrix({ paths: 'path/1,path/2,path/3,path/4,path/5', batches: '3' });
expect(result).toEqual({
include: [
{
path: 'path/1,path/2'
},
{
path: 'path/3,path/4'
},
{
path: 'path/5'
}
]
});
});
});

0 comments on commit 74438fc

Please sign in to comment.