Skip to content

Commit

Permalink
ci: migrate to gh-actions (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Mar 14, 2023
1 parent d44a44c commit cf7ec0b
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 48 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/ci.yaml
@@ -0,0 +1,61 @@
# This is a Github Workflow that runs tests on any push or pull request.
# If the tests pass and this is a push to the master branch it also runs Semantic Release.
name: CI
on: [push, pull_request]
jobs:
test_pr:
if: github.event_name == 'pull_request'
name: Test PR (Node v${{ matrix.node-version }}, OS ${{ matrix.os }})
strategy:
matrix:
os: [ ubuntu-22.04 ]
node-version: [ 14, 18 ]

runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Fetch deps
run: yarn

- name: Run tests
# Workaround like TRAVIS_PULL_REQUEST_BRANCH=master
run: |
git branch master
yarn test
release:
name: Release
# https://github.community/t/trigger-job-on-tag-push-only/18076
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
persist-credentials: false

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18

- name: Fetch deps
run: yarn

- name: Semantic Release
run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_AUTHOR_NAME: ${{ secrets.GIT_AUTHOR_NAME }}
GIT_AUTHOR_EMAIL: ${{ secrets.GIT_AUTHOR_EMAIL }}
GIT_COMMITTER_NAME: ${{ secrets.GIT_COMMITTER_NAME }}
GIT_COMMITTER_EMAIL: ${{ secrets.GIT_COMMITTER_EMAIL }}
File renamed without changes.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -18,8 +18,8 @@
"bin"
],
"scripts": {
"watch": "TRAVIS_PULL_REQUEST_BRANCH=master jest --watchAll",
"jest": "TRAVIS_PULL_REQUEST_BRANCH=master jest --coverage",
"watch": "jest --watchAll",
"jest": "jest --coverage",
"lint": "eslint ./",
"lint:fix": "eslint --fix ./",
"test": "yarn lint && yarn jest",
Expand Down
16 changes: 14 additions & 2 deletions test/bin/cli.test.js
Expand Up @@ -11,6 +11,12 @@ const {
gitGetTags,
} = require("../helpers/git");

const env = {
GH_TOKEN: "test",
NPM_TOKEN: "test",
PATH: process.env.PATH,
};

// Tests.
describe("multi-semantic-release CLI", () => {
test("Initial commit (changes in all packages)", async () => {
Expand All @@ -25,7 +31,7 @@ describe("multi-semantic-release CLI", () => {
const filepath = `${__dirname}/../../bin/cli.js`;

// Run via command line.
const out = (await execa("node", [filepath], { cwd })).stdout;
const out = (await execa("node", [filepath], { cwd, env, extendEnv: false })).stdout;
expect(out).toMatch("Started multirelease! Loading 4 packages...");
expect(out).toMatch("Released 4 of 4 packages, semantically!");
});
Expand All @@ -41,7 +47,13 @@ describe("multi-semantic-release CLI", () => {
const filepath = `${__dirname}/../../bin/cli.js`;

// Run via command line.
const out = (await execa("node", [filepath, "--ignore-packages=packages/c/**,packages/d/**"], { cwd })).stdout;
const out = (
await execa("node", [filepath, "--ignore-packages=packages/c/**,packages/d/**"], {
cwd,
env,
extendEnv: false,
})
).stdout;
expect(out).toMatch("Started multirelease! Loading 2 packages...");
expect(out).toMatch("Released 2 of 2 packages, semantically!");
});
Expand Down
43 changes: 24 additions & 19 deletions test/helpers/git.js
Expand Up @@ -9,6 +9,7 @@ const execa = require("execa");
const fileUrl = require("file-url");
const gitLogParser = require("git-log-parser");
const { array: getStreamArray } = require("get-stream");
const env = {};

/**
* @typedef {Object} Commit
Expand All @@ -32,12 +33,16 @@ function gitInit(branch = "master") {

// Init Git in a temp directory.
const cwd = tempy.directory();
execa.sync("git", ["init"], { cwd });
execa.sync("git", ["checkout", "-b", branch], { cwd });
execa.sync("git", ["init", "-b", "master"], { cwd, env });
execa.sync("git", ["checkout", "-b", branch], { cwd, env });

// Disable GPG signing for commits.
gitConfig(cwd, "commit.gpgsign", false);

// Set user profile.
gitConfig(cwd, "user.name", "Test");
gitConfig(cwd, "user.email", "test@example.com");

// Return directory.
return cwd;
}
Expand All @@ -46,12 +51,12 @@ function gitInit(branch = "master") {
* Create a remote Git repository.
* _Created in a temp folder._
*
* @return {Promise<string>} Promise that resolves to string URL of the of the remote origin.
* @return {Promise<string>} Promise that resolves to string URL of the remote origin.
*/
function gitInitRemote() {
// Init bare Git repository in a temp directory.
const cwd = tempy.directory();
execa.sync("git", ["init", "--bare"], { cwd });
execa.sync("git", ["init", "--bare"], { cwd, env });

// Turn remote path into a file URL.
const url = fileUrl(cwd);
Expand All @@ -76,15 +81,15 @@ function gitInitOrigin(cwd, releaseBranch = null) {
const url = gitInitRemote();

// Set origin on local repo.
execa.sync("git", ["remote", "add", "origin", url], { cwd });
execa.sync("git", ["remote", "add", "origin", url], { cwd, env });

// Set up a release branch. Return to master afterwards.
if (releaseBranch) {
execa.sync("git", ["checkout", "-b", releaseBranch], { cwd });
execa.sync("git", ["checkout", "master"], { cwd });
execa.sync("git", ["checkout", "-b", releaseBranch], { cwd, env });
execa.sync("git", ["checkout", "master"], { cwd, env });
}

execa.sync("git", ["push", "--all", "origin"], { cwd });
execa.sync("git", ["push", "--all", "origin"], { cwd, env });

// Return URL for remote.
return url;
Expand All @@ -104,7 +109,7 @@ function gitAdd(cwd, file = ".") {
check(cwd, "cwd: absolute");

// Await command.
execa.sync("git", ["add", file], { cwd });
execa.sync("git", ["add", file], { cwd, env });
}

// Commits.
Expand All @@ -123,7 +128,7 @@ function gitCommit(cwd, message) {
check(message, "message: string+");

// Await the command.
execa.sync("git", ["commit", "-m", message, "--no-gpg-sign"], { cwd });
execa.sync("git", ["commit", "-m", message, "--no-gpg-sign"], { cwd, env });

// Return HEAD SHA.
return gitGetHead(cwd);
Expand Down Expand Up @@ -167,7 +172,7 @@ function gitPush(cwd, remote = "origin", branch = "master") {
check(branch, "branch: lower");

// Await command.
execa.sync("git", ["push", "--tags", remote, `HEAD:${branch}`], { cwd });
execa.sync("git", ["push", "--tags", remote, `HEAD:${branch}`], { cwd, env });
}

// Branches.
Expand All @@ -185,7 +190,7 @@ function gitBranch(cwd, branch) {
check(branch, "branch: lower");

// Await command.
execa.sync("git", ["branch", branch], { cwd });
execa.sync("git", ["branch", branch], { cwd, env });
}

/**
Expand All @@ -201,7 +206,7 @@ function gitCheckout(cwd, branch) {
check(branch, "branch: lower");

// Await command.
execa.sync("git", ["checkout", branch], { cwd });
execa.sync("git", ["checkout", branch], { cwd, env });
}

// Hashes.
Expand All @@ -217,7 +222,7 @@ function gitGetHead(cwd) {
check(cwd, "cwd: absolute");

// Await command and return HEAD SHA.
return execa.sync("git", ["rev-parse", "HEAD"], { cwd }).stdout;
return execa.sync("git", ["rev-parse", "HEAD"], { cwd, env }).stdout;
}

// Tags.
Expand All @@ -237,7 +242,7 @@ function gitTag(cwd, tagName, hash = undefined) {
check(hash, "hash: alphanumeric{40}?");

// Run command.
execa.sync("git", hash ? ["tag", "-f", tagName, hash] : ["tag", tagName], { cwd });
execa.sync("git", hash ? ["tag", "-f", tagName, hash] : ["tag", tagName], { cwd, env });
}

/**
Expand All @@ -253,7 +258,7 @@ function gitGetTags(cwd, hash) {
check(hash, "hash: alphanumeric{40}");

// Run command.
return execa.sync("git", ["describe", "--tags", "--exact-match", hash], { cwd }).stdout;
return execa.sync("git", ["describe", "--tags", "--exact-match", hash], { cwd, env }).stdout;
}

/**
Expand All @@ -269,7 +274,7 @@ function gitGetTagHash(cwd, tagName) {
check(tagName, "tagName: string+");

// Run command.
return execa.sync("git", ["rev-list", "-1", tagName], { cwd }).stdout;
return execa.sync("git", ["rev-list", "-1", tagName], { cwd, env }).stdout;
}

// Configs.
Expand All @@ -288,7 +293,7 @@ function gitConfig(cwd, name, value) {
check(name, "name: string+");

// Run command.
execa.sync("git", ["config", "--add", name, value], { cwd });
execa.sync("git", ["config", "--add", name, value], { cwd, env });
}

/**
Expand All @@ -304,7 +309,7 @@ function gitGetConfig(cwd, name) {
check(name, "name: string+");

// Run command.
execa.sync("git", ["config", name], { cwd }).stdout;
execa.sync("git", ["config", name], { cwd, env }).stdout;
}

// Exports.
Expand Down
12 changes: 9 additions & 3 deletions test/lib/git.test.js
Expand Up @@ -4,6 +4,12 @@ const { copyDirectory, createNewTestingFiles } = require("../helpers/file");
const { gitInit, gitCommitAll, gitInitOrigin, gitPush } = require("../helpers/git");
const { getTags } = require("../../lib/git");

const env = {
GH_TOKEN: "test",
NPM_TOKEN: "test",
PATH: process.env.PATH,
};

test("Fetch all tags on master after two package release", async () => {
const packages = ["packages/c/", "packages/d/"];

Expand All @@ -25,7 +31,7 @@ test("Fetch all tags on master after two package release", async () => {
{
branches: [{ name: "master" }, { name: "release" }],
},
{ cwd, stdout, stderr }
{ cwd, stdout, stderr, env }
);

const tags = getTags("master", { cwd }).sort();
Expand Down Expand Up @@ -53,7 +59,7 @@ test("Fetch only prerelease tags", async () => {
{
branches: [{ name: "master" }, { name: "release" }],
},
{ cwd, stdout, stderr }
{ cwd, stdout, stderr, env }
);

// Add new testing files for a new release.
Expand All @@ -73,7 +79,7 @@ test("Fetch only prerelease tags", async () => {
{
branches: [{ name: "master", prerelease: "beta" }, { name: "release" }],
},
{ cwd, stdout, stderr }
{ cwd, stdout, stderr, env }
);

const tags = getTags("master", { cwd }, ["beta"]).sort();
Expand Down

0 comments on commit cf7ec0b

Please sign in to comment.