Skip to content

Commit

Permalink
chore: initial ci commit
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed May 3, 2024
1 parent 8549978 commit e8936ae
Show file tree
Hide file tree
Showing 10 changed files with 1,130 additions and 27 deletions.
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
29 changes: 29 additions & 0 deletions .github/workflows/changeset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Changeset

on:
push:
branches:
- main

jobs:
create-release-pr:
name: Create Changeset PR
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- name: Install Dependencies
run: yarn --frozen-lockfile
- name: Create Release Pull Request
uses: changesets/action@v1
with:
# Calls out to `changeset version`, but also runs prettier
version: yarn release
title: Version Packages - plugins
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Node.js CI

on:
pull_request:
branches:
- main

jobs:
build:
env:
NODE_OPTIONS: --max-old-space-size=4096

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: '0'

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- run: yarn install --frozen-lockfile
- run: yarn lint:all
- run: yarn tsc
- run: yarn test:all
- run: yarn build:all
96 changes: 96 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: Publish Build

on:
workflow_dispatch:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

outputs:
needs_release: ${{ steps.release_check.outputs.needs_release }}

env:
CI: true
NODE_OPTIONS: --max-old-space-size=4096

steps:
- name: Checkout repository
uses: actions/checkout@v3

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

- run: yarn install --frozen-lockfile

- name: Fetch previous commit for release check
run: git fetch origin '${{ github.event.before }}'

- name: Check if release
id: release_check
run: node scripts/check-if-release.js
env:
COMMIT_SHA_BEFORE: '${{ github.event.before }}'

- name: build type declarations
run: yarn tsc

- name: build packages
run: yarn build

# A separate release build that is only run for commits that are the result of merging the "Version Packages" PR
# We can't re-use the output from the above step, but we'll have a guaranteed node_modules cache and
# only run the build steps that are necessary for publishing
release:
needs: build

if: needs.build.outputs.needs_release == 'true'

runs-on: ubuntu-latest

env:
CI: 'true'
NODE_OPTIONS: --max-old-space-size=4096

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- run: yarn install --frozen-lockfile

- run: yarn tsc

- run: yarn build:all

- name: 'Login to npmjs npm repo .npmrc'
shell: bash
run: |
echo "//registry.npmjs.org/:_authToken=${{secrets.NPM_AUTH_TOKEN}}" >> .npmrc
echo "always-auth = true" >> .npmrc
# Publishes current version of packages that are not already present in the registry.
- name: Run publish
run: |
git config --global user.email "[email protected]"
git config --global user.name "humanitec"
lerna publish from-package --yes --no-verify-access
# Creates the next available tag with format "release-<year>-<month>-<day>[.<n>]"
- name: Create a release tag
id: create_tag
run: node scripts/create-release-tag.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# Convert the newly created tag into a release with changelog information
- name: Create release on GitHub
run: node scripts/create-github-release.js ${{ steps.create_tag.outputs.tag_name }} 1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"lint": "backstage-cli repo lint --since origin/main",
"lint:all": "backstage-cli repo lint",
"prettier:check": "prettier --check .",
"new": "backstage-cli new --scope internal"
"new": "backstage-cli new --scope internal",
"release": "changeset version && yarn prettier --write '{packages,plugins}/*/{package.json,CHANGELOG.md}' && yarn install"
},
"workspaces": {
"packages": [
Expand Down Expand Up @@ -54,5 +55,8 @@
"*.{json,md}": [
"prettier --write"
]
},
"dependencies": {
"@changesets/cli": "^2.27.1"
}
}
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"app": "link:../app",
"better-sqlite3": "^9.0.0",
"dockerode": "^3.3.1",
"express": "^4.19.2",
"node-gyp": "^9.0.0",
"pg": "^8.11.3",
"winston": "^3.2.1"
Expand Down
128 changes: 128 additions & 0 deletions scripts/check-if-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env node
/* eslint-disable @backstage/no-undeclared-imports */
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// This script is used to determine whether a particular commit has changes
// that should lead to a release. It is run as part of the main master build
// to determine whether the release flow should be run as well.
//
// It has the following output which can be used later in GitHub actions:
//
// needs_release = 'true' | 'false'

const { execFile: execFileCb } = require('child_process');
const { resolve: resolvePath } = require('path');
const { promises: fs } = require('fs');
const { promisify } = require('util');
const { EOL } = require('os');

const parentRef = process.env.COMMIT_SHA_BEFORE || 'HEAD^';

const execFile = promisify(execFileCb);

async function runPlain(cmd, ...args) {
try {
const { stdout } = await execFile(cmd, args, { shell: true });
return stdout.trim();
} catch (error) {
if (error.stderr) {
process.stderr.write(error.stderr);
}
if (!error.code) {
throw error;
}
throw new Error(
`Command '${[cmd, ...args].join(' ')}' failed with code ${error.code}`,
);
}
}

async function main() {
process.cwd(resolvePath(__dirname, '..'));

if (!process.env.GITHUB_OUTPUT) {
throw new Error('GITHUB_OUTPUT environment variable not set');
}

const diff = await runPlain(
'git',
'diff',
'--name-only',
parentRef,
"'*/package.json'", // Git treats this as what would usually be **/package.json
);
const packageList = diff
.split('\n')
.filter(path => path.match(/^(packages|plugins)\/[^/]+\/package\.json$/));

const packageVersions = await Promise.all(
packageList.map(async path => {
let name;
let newVersion;
let oldVersion;

try {
const data = JSON.parse(
await runPlain('git', 'show', `${parentRef}:${path}`),
);
name = data.name;
oldVersion = data.version;
} catch {
oldVersion = '<none>';
}

try {
const data = JSON.parse(await fs.readFile(path, 'utf8'));
name = data.name;
newVersion = data.version;
} catch (error) {
if (error.code === 'ENOENT') {
newVersion = '<none>';
}
}

return { name, oldVersion, newVersion };
}),
);

const newVersions = packageVersions.filter(
({ oldVersion, newVersion }) =>
oldVersion !== newVersion &&
oldVersion !== '<none>' &&
newVersion !== '<none>',
);

if (newVersions.length === 0) {
console.log('No package version bumps detected, no release needed');
await fs.appendFile(process.env.GITHUB_OUTPUT, `needs_release=false${EOL}`);
return;
}

console.log('Package version bumps detected, a new release is needed');
const maxLength = Math.max(...newVersions.map(_ => _.name.length));
for (const { name, oldVersion, newVersion } of newVersions) {
console.log(
` ${name.padEnd(maxLength, ' ')} ${oldVersion} to ${newVersion}`,
);
}
await fs.appendFile(process.env.GITHUB_OUTPUT, `needs_release=true${EOL}`);
}

main().catch(error => {
console.error(error.stack);
process.exit(1);
});
Loading

0 comments on commit e8936ae

Please sign in to comment.