Skip to content

Commit

Permalink
chore(ci): use circleci for controls and releasing
Browse files Browse the repository at this point in the history
  • Loading branch information
amille44420 committed Jun 2, 2021
1 parent 8fade9e commit 09cfd01
Show file tree
Hide file tree
Showing 10 changed files with 3,188 additions and 39 deletions.
84 changes: 84 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
version: 2.1

executors:
node-standalone:
docker:
- image: circleci/node:14.16.0

jobs:
deps-install:
executor: node-standalone
steps:
- checkout
- restore_cache:
keys:
- yarn-cache-node14-{{ checksum "package.json" }}-{{ checksum "yarn.lock" }}
- yarn-cache-node14-{{ checksum "package.json" }}
- yarn-cache-node14
- run:
name: Install depdencies
command: yarn install --frozen-lockfile --cache-folder ~/.cache/yarn
- save_cache:
key: yarn-cache-node14-{{ checksum "package.json" }}-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn
- persist_to_workspace:
root: ~/project
paths:
- node_modules

validate-project:
executor: node-standalone
steps:
- checkout
- attach_workspace:
at: ~/project
- run:
name: Lint source code
command: yarn lint --format junit -o ./junit/js-lint-results.xml
- run:
name: Build
command: yarn build
environment:
NODE_ENV: production
- persist_to_workspace:
root: ~/project
paths:
- build
- store_artifacts:
path: ~/project/build
destination: build
- run:
name: Lint commit messages
command: node ./lint-commits.js
- store_test_results:
path: ~/project/junit/

release:
executor: node-standalone
steps:
- checkout
- attach_workspace:
at: ~/project
- run:
name: semantic-release
command: yarn semantic-release

workflows:
version: 2

main:
jobs:
- deps-install
- validate-project:
requires:
- deps-install
- release:
context:
- html2pdf-release
filters:
branches:
only:
- master
requires:
- validate-project
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2

updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
day: "sunday"
time: "22:00"
2 changes: 2 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- master
tags:
- v*

env:
IMAGE_NAME: api
Expand Down
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn commitlint --edit $1
6 changes: 6 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'body-max-line-length': [2, 'always', Infinity],
},
};
80 changes: 80 additions & 0 deletions lint-commits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const { Octokit } = require('@octokit/rest');
const execa = require('execa');

const git = async (...args) => {
const { stdout } = await execa('git', args);

return stdout;
};

const checkCommit = async (...refs) => Promise.all(refs.map(ref => git('cat-file', '-e', ref)));

const matchGithub = (url, prop) => {
if (!url) {
throw new Error();
}

const match = url.match(new RegExp(`github\\.com/(.+)/(.+)/${prop}/(.+)`));

if (!match) {
throw new Error();
}

const [, owner, repo, data] = match;

return { owner, repo, data };
};

const getRangeFromPr = async () => {
const { owner, repo, data: pull } = matchGithub(process.env.CIRCLE_PULL_REQUEST, 'pull');
const github = new Octokit({ auth: process.env.GH_TOKEN });

console.info('📡 Looking up PR #%s...', pull);

const {
data: { base, head },
} = await github.pulls.get({ owner, repo, pull_number: +pull });

await checkCommit(base.sha, head.sha);
console.info('🔀 Linting PR #%s', pull);

return [base.sha, head.sha];
};

const getRangeFromCompare = async () => {
const [from, to] = matchGithub(process.env.CIRCLE_COMPARE_URL, 'compare').data.split('...');

await checkCommit(from, to);
console.info('🎏 Linting using comparison URL %s...%s', from, to);

return [from, to];
};

const getRangeFromSha = async () => {
const sha = process.env.CIRCLE_SHA1;

if (!sha) {
throw new Error('Cannot find CIRCLE_SHA1 environment variable');
}

await checkCommit(sha);
console.info('⚙️ Linting using CIRCLE_SHA1 (%s)', sha);

return [`${sha}^1`, sha];
};

const runLint = ([from, to]) => execa('yarn', ['commitlint', '--from', from, '--to', to, '-V'], { stdio: 'inherit' });

const run = () =>
getRangeFromPr()
.catch(getRangeFromCompare)
.catch(getRangeFromSha)
.then(runLint, error => {
console.error(error);
process.exit(1);
})
.catch(() => {
process.exit(1);
});

run();
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"build": "tsc"
},
"devDependencies": {
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@octokit/rest": "^18.5.6",
"@types/express": "^4.17.12",
"@types/lodash": "^4.14.170",
"@types/node": "^15.6.1",
Expand All @@ -22,7 +25,9 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-prettier": "^3.4.0",
"execa": "^5.0.1",
"prettier": "^2.3.0",
"semantic-release": "^17.4.3",
"typescript": "^4.3.2"
},
"dependencies": {
Expand Down
13 changes: 13 additions & 0 deletions release.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
branches: [
{
name: 'master',
channel: false,
},
],
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
'@semantic-release/github',
],
};
Loading

0 comments on commit 09cfd01

Please sign in to comment.