Skip to content

Commit cf9eb5e

Browse files
authored
refactor: move GitHub Actions to .yml file templates (#12)
1 parent 4b0a9d4 commit cf9eb5e

File tree

5 files changed

+87
-72
lines changed

5 files changed

+87
-72
lines changed

lib/create-github-action.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import writePrettyFile from "./write-pretty-file.js";
2+
import getTemplateFileContent from "./get-template-file-content.js";
3+
4+
export default async function createGitHubAction(actionFileName, options = {}) {
5+
let fileContent = getTemplateFileContent(`github-actions/${actionFileName}`);
6+
7+
if (options.replace) {
8+
fileContent = fileContent.replace(
9+
options.replace.searchValue,
10+
options.replace.replaceValue
11+
);
12+
}
13+
14+
await writePrettyFile(`.github/workflows/${actionFileName}`, fileContent);
15+
}

lib/create-release-action.js

+7-30
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
1-
import writePrettyFile from "./write-pretty-file.js";
1+
import createGitHubAction from "./create-github-action.js";
22

33
export default async function createReleaseAction({ owner }) {
44
const npmTokenSecretName =
55
owner === "octoherd" ? "NPM_AUTOMATION_TOKEN" : "NPM_TOKEN";
6-
await writePrettyFile(
7-
".github/workflows/release.yml",
8-
`name: Release
9-
on:
10-
push:
11-
branches:
12-
- main
136

14-
jobs:
15-
release:
16-
name: release
17-
runs-on: ubuntu-latest
18-
steps:
19-
- uses: actions/checkout@v2
20-
- uses: actions/setup-node@v2
21-
with:
22-
node-version: 12
23-
- uses: actions/cache@v1
24-
with:
25-
path: ~/.npm
26-
key: \${{ runner.os }}-node-\${{ hashFiles('**/package-lock.json') }}
27-
restore-keys: |
28-
\${{ runner.os }}-node-
29-
- run: npm ci
30-
- run: npx semantic-release
31-
env:
32-
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
33-
NPM_TOKEN: \${{ secrets.${npmTokenSecretName} }}
34-
`
35-
);
7+
await createGitHubAction("release.yml", {
8+
replace: {
9+
searchValue: "__npmTokenSecretName__",
10+
replaceValue: npmTokenSecretName,
11+
},
12+
});
3613
}

lib/create-test-action.js

+2-42
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,5 @@
1-
import writePrettyFile from "./write-pretty-file.js";
1+
import createGitHubAction from "./create-github-action.js";
22

33
export default async function createTestAction() {
4-
await writePrettyFile(
5-
".github/workflows/test.yml",
6-
`name: Test
7-
on:
8-
push:
9-
branches:
10-
- main
11-
pull_request:
12-
types: [opened, synchronize]
13-
14-
jobs:
15-
test_matrix:
16-
runs-on: ubuntu-latest
17-
strategy:
18-
matrix:
19-
node_version: ["12", "14"]
20-
21-
steps:
22-
- uses: actions/checkout@v2
23-
- name: Use Node.js \${{ matrix.node_version }}
24-
uses: actions/setup-node@v2
25-
with:
26-
node-version: \${{ matrix.node_version }}
27-
- uses: actions/cache@v2
28-
with:
29-
path: ~/.npm
30-
key: \${{ runner.os }}-node-\${{ hashFiles('**/package-lock.json') }}
31-
restore-keys: |
32-
\${{ runner.os }}-node-
33-
- run: npm ci
34-
- run: npm test
35-
36-
# The "test" step can be required in branch protection and does not
37-
# change each time the test matrix changes.
38-
test:
39-
runs-on: ubuntu-latest
40-
needs: test_matrix
41-
steps:
42-
- run: echo ok
43-
`
44-
);
4+
await createGitHubAction("test.yml");
455
}

templates/github-actions/release.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Release
2+
on:
3+
push:
4+
branches:
5+
- main
6+
7+
jobs:
8+
release:
9+
name: release
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: actions/setup-node@v2
14+
with:
15+
node-version: 12
16+
- uses: actions/cache@v1
17+
with:
18+
path: ~/.npm
19+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
20+
restore-keys: |
21+
${{ runner.os }}-node-
22+
- run: npm ci
23+
- run: npx semantic-release
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
NPM_TOKEN: ${{ secrets.__npmTokenSecretName__ }}

templates/github-actions/test.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Test
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
types: [opened, synchronize]
8+
9+
jobs:
10+
test_matrix:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
node_version: ["12", "14"]
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Use Node.js ${{ matrix.node_version }}
19+
uses: actions/setup-node@v2
20+
with:
21+
node-version: ${{ matrix.node_version }}
22+
- uses: actions/cache@v2
23+
with:
24+
path: ~/.npm
25+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
26+
restore-keys: |
27+
${{ runner.os }}-node-
28+
- run: npm ci
29+
- run: npm test
30+
31+
# The "test" step can be required in branch protection and does not
32+
# change each time the test matrix changes.
33+
test:
34+
runs-on: ubuntu-latest
35+
needs: test_matrix
36+
steps:
37+
- run: echo ok

0 commit comments

Comments
 (0)