Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: automate release process #635

Merged
merged 1 commit into from Jan 31, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions .github/workflows/create-release.yml
Expand Up @@ -2,21 +2,36 @@ name: Create a release on tag

on:
push:
tags:
- "*"
branches:
- main

jobs:
release:
timeout-minutes: 15
runs-on: ubuntu-latest
if: "contains(github.event.head_commit.message, 'prepare release')"
strategy:
matrix:
node: [14]
node: [15]

env:
CI: true
steps:
- uses: actions/checkout@v2
with:
# pulls all commits (needed for lerna / semantic release to correctly version)
fetch-depth: "0"

# pulls all tags (needed for lerna / semantic release to correctly version)
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*

- name: Git Identity
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/$GITHUB_REPOSITORY
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Node.js ${{ matrix.node }}
uses: actions/setup-node@v1
Expand All @@ -38,6 +53,10 @@ jobs:
${{ runner.os }}-yarn-${{ matrix.node }}-
${{ runner.os }}-yarn-

- run: echo //registry.npmjs.org/:_authToken=${NPM_TOKEN} > .npmrc
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Create release
run: |
yarn install
Expand Down
41 changes: 35 additions & 6 deletions scripts/create-release.js
@@ -1,14 +1,43 @@
import { readFile } from "fs/promises";
import { mainFn } from "@compas/stdlib";
import { exec, mainFn, spawn } from "@compas/stdlib";
import axios from "axios";

mainFn(import.meta, main);

async function main() {
const rawRef = process.env.GITHUB_REF;
async function main(logger) {
const [githubToken] = process.argv.slice(2);

const tag = rawRef.replace(/^refs\/tags\//, "");
const newVersion = await getVersionFromGitLog();
if (!/^\d+\.\d+\.\d+$/g.test(newVersion)) {
logger.error(`Invalid version, found: ${newVersion}`);

process.exit(1);
}

const { exitCode } = await spawn(`yarn`, [
"lerna",
"publish",
"--exact",
"--yes",
newVersion,
]);

if (exitCode !== 0) {
process.exit(exitCode);
}

await addChangelogToGithubRelease(`v${newVersion}`, githubToken);
}

async function getVersionFromGitLog() {
const { stdout } = await exec(`git log -1 --pretty=format:"%s"`);
const versionString = stdout.trim().split(" ").pop();

// Drop the 'v'
return versionString.substring(1);
}

async function addChangelogToGithubRelease(version, githubToken) {
const fullChangelog = await readFile("./changelog.md", "utf8");
const changelogPart = parseChangelog(fullChangelog);

Expand All @@ -20,8 +49,8 @@ async function main() {
password: githubToken,
},
data: {
tag_name: tag,
name: tag,
tag_name: version,
name: version,
body: changelogPart,
draft: false,
prerelease: false,
Expand Down