Skip to content

Commit

Permalink
add workflow files for npm
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSulpiride committed May 9, 2024
1 parent 06a3116 commit 4377a2d
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 1 deletion.
58 changes: 58 additions & 0 deletions .github/actions/setup-and-build/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: "Setup and build the repo"
description: "A task to reuse setup steps during multiple jobs"
inputs:
node:
description: Node version
required: true

runs:
using: "composite"
steps:
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{inputs.node}}
check-latest: true
cache: yarn

- name: Node.js version
id: node
shell: bash
run: echo "v8CppApiVersion=$(node --print "process.versions.modules")" >> $GITHUB_OUTPUT

- name: Restore build
uses: actions/cache/restore@v4
id: cache-build-restore
with:
path: |
node_modules
packages/*/node_modules
lib/
packages/*/lib
packages/*/.git-data.json
key: ${{ runner.os }}-${{ runner.arch }}-node-${{ inputs.node }}-${{ github.sha }}

- name: Install & build
if: steps.cache-build-restore.outputs.cache-hit != 'true'
shell: bash
run: yarn install --frozen-lockfile && yarn build

- name: Build
if: steps.cache-build-restore.outputs.cache-hit == 'true'
shell: bash
run: yarn build

- name: Check Build
shell: bash
run: yarn check-build

- name: Cache build artifacts
uses: actions/cache@master
with:
path: |
node_modules
packages/*/node_modules
lib/
packages/*/lib
packages/*/.git-data.json
key: ${{ runner.os }}-${{ runner.arch }}-node-${{ inputs.node }}-${{ github.sha }}
56 changes: 56 additions & 0 deletions .github/workflows/binaries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Build binaries

on:
workflow_dispatch:
inputs:
version:
required: true
type: string
workflow_call:
inputs:
version:
required: true
type: string

jobs:
binaries:
name: Build skandha binaries
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
platform: linux
arch: amd64
- os: skandha-arm64-runner
platform: linux
arch: arm64
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- name: Install arm64 specifics
if: matrix.arch == 'arm64'
run: |-
# Install missing yarn
# See https://github.com/github-early-access/arm-runners-beta/issues/5
curl -fsSL --create-dirs -o $HOME/bin/yarn \
https://github.com/yarnpkg/yarn/releases/download/v1.22.22/yarn-1.22.22.js
chmod +x $HOME/bin/yarn
echo "$HOME/bin" >> $GITHUB_PATH
# Install missing build-essential
sudo apt-get update
sudo apt-get install -y build-essential
- uses: "./.github/actions/setup-and-build"
with:
node: 20
- run: |
mkdir -p dist
yarn global add [email protected]
npx caxa -m "Unpacking skandha binary, please wait..." -D -p "yarn install --frozen-lockfile --production" --input . --output "skandha" -- "{{caxa}}/node_modules/.bin/node" "--max-old-space-size=8192" "{{caxa}}/node_modules/.bin/skandha"
tar -czf "dist/skandha-${{ inputs.version }}-${{ matrix.platform }}-${{ matrix.arch }}.tar.gz" "skandha"
- name: Upload binaries
uses: actions/upload-artifact@v4
with:
name: binaries-${{ matrix.os }}
path: dist/
if-no-files-found: error
92 changes: 91 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,97 @@ on:
- "master"

jobs:
build:
tag:
name: Check tag
runs-on: buildjet-4vcpu-ubuntu-2204
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get tag
id: get_tag
run: echo "tag=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT

- name: Assert tag == package.json version
run: .github/workflows/scripts/assert-same-package-version.sh
env:
TAG: ${{ steps.get_tag.outputs.tag }}

- name: Get previous tag
id: get_prev_tag
run: node tasks/get_prev_tag.js
env:
CURRENT_TAG: ${{ steps.get_tag.outputs.tag }}
IGNORE_PATTERN: rc

- name: Determine release type
id: release_type
run: |
TAG_COMMIT=$(git log --pretty="%h" -n 1 $GITHUB_REF)
echo "tag_commit=$TAG_COMMIT" >> $GITHUB_OUTPUT
outputs:
tag: ${{ steps.get_tag.outputs.tag }}
prev_tag: ${{ steps.get_prev_tag.outputs.prev_tag }}

binaries:
name: Build lodestar binaries
uses: ./.github/workflows/binaries.yml
needs: tag
with:
version: ${{ needs.tag.outputs.tag }}

npm:
name: Publish to NPM & Github
runs-on: buildjet-4vcpu-ubuntu-2204
needs: [tag, binaries]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Needs full depth for changelog generation

- uses: "./.github/actions/setup-and-build"
with:
node: 20

- name: Get binaries
uses: actions/download-artifact@v4
with:
path: dist/
merge-multiple: true

- name: Create Release
id: create_release
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
files: dist/*
fail_on_unmatched_files: true
tag_name: ${{ needs.tag.outputs.tag }}
body_path: "CHANGELOG.md"
name: Release ${{ needs.tag.outputs.tag }}
prerelease: false
- run: echo //registry.npmjs.org/:_authToken=${NPM_TOKEN} > .npmrc
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Publish to npm registry (release)
run: yarn run release:publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Rollback on failure
if: failure()
uses: author/action-rollback@9ec72a6af74774e00343c6de3e946b0901c23013
with:
id: ${{ steps.create_release.outputs.id }}
tag: ${{ needs.tag.outputs.tag }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

docker:
runs-on: ubuntu-latest
steps:
-
Expand Down
46 changes: 46 additions & 0 deletions tasks/get_prev_tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable no-console,
@typescript-eslint/explicit-function-return-type,
@typescript-eslint/no-var-requires,
@typescript-eslint/no-require-imports */

// Script used in .github/workflows/release.yml to get the previous tag
// to generate a changelog from 'prev_tag' to 'tag'
// Returns the most recent tag that:
// - is not equal to CURRENT_TAG
// - does not contain IGNORE_PATTERN
//
// Outputs to output.prev_tag

const { exec } = require("node:child_process");
const { promisify } = require("node:util");

async function run() {
const { CURRENT_TAG, IGNORE_PATTERN } = process.env;
if (!CURRENT_TAG) throw Error("CURRENT_TAG must be defined");
if (!IGNORE_PATTERN) throw Error("IGNORE_PATTERN must be defined");

const { stdout } = await promisify(exec)("git tag --sort=-version:refname");
// Returns sorted list of tags
// v0.32.0
// v0.31.0
// v0.30.0
// v0.29.3

// Pick the first tag that doesn't match current tag
const tags = stdout.trim().split("\n");
for (const tag of tags) {
if (tag !== CURRENT_TAG && !tag.includes(IGNORE_PATTERN)) {
const cmd = `echo "prev_tag=${tag}" >> ${process.env.GITHUB_OUTPUT}`;
console.log("Execute command on shell", cmd);
await promisify(exec)(cmd);
return;
}
}

throw Error("No tag found");
}

run().catch((e) => {
console.error(e);
process.exit(1);
});

0 comments on commit 4377a2d

Please sign in to comment.