Skip to content
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
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
13 changes: 13 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://unpkg.com/@changesets/config@1.7.0/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": [],
"___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
"updateInternalDependents": "always"
}
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @Khan/actions

A monorepo for shared github actions.

Currently only composite actions are supported, although supporting nodejs actions probably wouldn't be too hard. If your script is simple, you can do a composite action that uses `actions/github-script`, see the `filter-files` action for an example.

## How does the monorepo work?

Github doesn't support putting actions in subdirectories, so we need to do some fancy work here. Inspired by [gitpkg](https://github.com/ramasilveyra/gitpkg), we "publish" versions of our actions to 'bare tags' in this repo. So the tag `filter-files-v0.0.1` would only contain the files for the `filter-files` action, and thus github is perfectly happy for us to reference it as `uses: @Khan/actions#filter-files-v0.0.1`.

Actions that depend on other actions within this repo (with e.g. `uses: filter-files`) will have the references automatically converted to the appropriated pinned reference (e.g. `Khan/actions#filter-files-v0.0.1`) as part of the publish process.

## How does changeset play in?

Changeset helps us track what needs to be published, and automatically produces a changelog so we know what changed in a given release.
27 changes: 27 additions & 0 deletions actions/check-for-changeset/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 'Check for a changeset'
description: 'Fail if no .changeset/*.md file is included in the PR.'
runs:
using: "composite"
steps:
- name: "Get All Changed Files"
id: changed
uses: jaredly/get-changed-files@v1.0.1
with:
format: json
- uses: actions/github-script@v6
with:
script: |
const inputFiles = JSON.parse(`${{ steps.changed.outputs.added_modified }}`);
core.debug("Changed files: " + inputFiles);

const hasChangeset = inputFiles.some(name => {
return /^\.changeset\/.*\.md/.test(name);
});
if (!hasChangeset) {
core.setFailed(
"This PR does not have a changeset. You can add one by " +
"running `yarn changeset` and following the prompts.\n" +
"If this PR doesn't need a changeset, run `yarn changeset " +
"--empty` and commit results."
);
}
4 changes: 4 additions & 0 deletions actions/check-for-changeset/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "check-for-changeset",
"version": "0.0.0"
}
37 changes: 37 additions & 0 deletions actions/filter-files/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: 'Filter files please'
description: 'Filter the list of changed files'
inputs:
changed-files:
description: 'jsonified list of changed files from setup'
required: true
files:
description: 'comma-separated list of files to check for'
required: false
extensions:
description: 'comma-separated list of extensions to check for'
required: false
outputs:
filtered:
description: 'The jsonified list of files that match'
value: ${{ steps.result.outputs.result }}
runs:
using: "composite"
steps:
- uses: actions/github-script@v6
id: result
with:
script: |
const extensionsRaw = "${{ inputs.extensions }}";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should have a policy (in a future change) of breaking out inline scripts to be imported from another file? That way we can lint and test them, for example: https://github.com/actions/github-script#run-a-separate-file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on interesting, I didn't know that was an option! Sounds like a good policy.

@k4b7 k4b7 May 19, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeresig has that always been possible or is this a new feature? Either way, I'm glad it's possible.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always been possible, I believe!

const exactFilesRaw = "${{ inputs.files }}";
const inputFiles = JSON.parse(`${{ inputs.changed-files }}`);
const extensions = extensionsRaw.trim() ? extensionsRaw.split(',') : [];
const exactFiles = exactFilesRaw.trim() ? exactFilesRaw.split(',') : [];

const result = inputFiles.filter(name => {
return extensions.some(ext => name.endsWith(ext)) || (
exactFiles.includes(name)
)
})
console.log(`Filtered Files: ${JSON.stringify(result)}`)
return result;
result-encoding: json
4 changes: 4 additions & 0 deletions actions/filter-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "filter-files",
"version": "0.0.0"
}
28 changes: 28 additions & 0 deletions actions/full-or-limited/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Full or Limited

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for abstracting this into an action.

description: Do a full run if certain files have changed, or a limited run of some others have changed
inputs:
full-trigger:
description: A jsonified Array of string files that would trigger a full run
required: true
limited-trigger:
description: A jsonified Array of string files that should be passed to a limited run
required: true
full:
description: The command to run if a full run is triggered
limited:
description: The command to run, with {} replaced with the list of files to run on.
runs:
using: "composite"
steps:
- name: Full run
if: inputs.full-trigger != '[]'
run: ${{ inputs.full }}
shell: bash

- name: Limited run
if: inputs.full-trigger == '[]' && inputs.limited-trigger != '[]'
# Will be replaced with a pinned version by our build system
uses: json-args
with:
list: ${{ inputs.limited-trigger }}
run: ${{ inputs.limited }}
7 changes: 7 additions & 0 deletions actions/full-or-limited/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "full-or-limited",
"version": "0.0.0",
"dependencies": {
"json-args": "*"
}
}
32 changes: 32 additions & 0 deletions actions/json-args/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: 'Pass a jsonified-list of files as shell arguments yes'
description: 'Because file names with spaces are the worst'
inputs:
list:
description: A jsonified Array of string file names
required: true
run:
description: "a command to run, where the literal '{}' will be replaced with the files as individual arguments. If no '{}' is provided, the files will be appended."
required: true
runs:
using: "composite"
steps:
- uses: actions/github-script@v6
with:
script: |
const listRaw = `${{ inputs.list }}`;
const files = JSON.parse(listRaw);
const {execSync} = require('child_process');
if (files.some(name => name.match(/['"]/))) {
throw new Error(`Not going to mess with file names that have quotes in them.`)
}
const filesList = files.map(name => `"${name}"`).join(' ')
let cmd = `${{ inputs.run }}`;
if (cmd.includes('{}')) {
cmd = cmd.replace('{}', filesList)
} else {
cmd += ' ' + filesList;
}
console.log(`Running: ${cmd}`);
execSync(cmd, {
stdio: 'inherit',
})
4 changes: 4 additions & 0 deletions actions/json-args/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "json-args",
"version": "0.0.3"
}
29 changes: 29 additions & 0 deletions actions/shared-node-cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: 'Shared Node Cache'
description: 'Install & cache our npm dependencies'
inputs:
node-version:
description: 'Node version to use'
required: false
default: '16.x'
runs:
using: "composite"
steps:

- name: Use Node.js ${{ inputs.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ inputs.node-version }}

- name: Cache node_modules
uses: actions/cache@v2
id: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-${{ inputs.node-version }}-node_modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{ inputs.node-version }}-node_modules-

- name: Install Dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
shell: bash
run: yarn install --frozen-lockfile
4 changes: 4 additions & 0 deletions actions/shared-node-cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "shared-node-cache",
"version": "0.0.2"
}
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"repository": "git@github.com:Khan/actions.git",
"private": true,
"workspaces": [
"actions/*"
],
"devDependencies": {
"@changesets/cli": "^2.22.0"
}
}
Loading