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

Publish to NPM #7

Merged
merged 21 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2cfd403
Copy https://github.com/eosnetworkfoundation/aws-cloudwatch-alarm-han…
kj4ezj Aug 1, 2024
7c55412
Add reset script to package.json
kj4ezj Aug 1, 2024
c4e4d01
package.json: Change email
kj4ezj Aug 1, 2024
fd40e73
Change default node environment from 18 (hydrogen) to 20 (iron)
kj4ezj Aug 1, 2024
713e929
Add package-lock.json to node environment reset script
kj4ezj Aug 1, 2024
d174c35
Delete build artifacts in node environment reset script
kj4ezj Aug 1, 2024
8694a28
Add npm install validation step per NPM docs
kj4ezj Aug 1, 2024
6549555
Add "--non-interactive" flag to yarn init in CI
kj4ezj Aug 1, 2024
a6a2ce0
Copy https://github.com/eosnetworkfoundation/telegram-bot:v0.3.0/scri…
kj4ezj Aug 1, 2024
c5d491c
Rough out a build script to pack build metadata into the package.json
kj4ezj Aug 1, 2024
be07878
Add NPM publish step to CI
kj4ezj Aug 1, 2024
f85197c
Try a global install to address CI error on node 14 (fermium)
kj4ezj Aug 1, 2024
e23719a
Fix "error: malformed object name tags/"
kj4ezj Aug 2, 2024
15f8694
Moar print statements
kj4ezj Aug 2, 2024
ea0d55d
We are not using sanitize()
kj4ezj Aug 2, 2024
3a90ef0
Roll install validation into build script
kj4ezj Aug 2, 2024
8bd905c
Only do global install test in CI
kj4ezj Aug 2, 2024
d664aba
Move npm publish step into build script, but only for newest supporte…
kj4ezj Aug 2, 2024
9650e83
Add "yarn act" command to package.json
kj4ezj Aug 2, 2024
7d660f3
Moar print statements
kj4ezj Aug 2, 2024
7e8c3c0
Stop running duplicate builds on pull requests
kj4ezj Aug 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
name: is.js CICD

on: [push, pull_request, workflow_dispatch]
on:
push:
branches:
- develop
- main
- master
tags:
- '*'
pull_request:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write

strategy:
matrix:
Expand All @@ -22,16 +34,19 @@ jobs:
node-version: ${{ matrix.node-version }}

- name: Init - node v${{ matrix.node-version }}
run: yarn --frozen-lockfile
run: yarn --frozen-lockfile --non-interactive

- name: Lint - node v${{ matrix.node-version }}
run: yarn lint

- name: Test - node v${{ matrix.node-version }}
run: yarn test

- name: Package - node v${{ matrix.node-version }}
run: npm pack
- name: Build - node v${{ matrix.node-version }}
run: yarn build
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_API_KEY }}
NODE_MAJOR_VERSION: ${{ matrix.node-version }}

- name: Upload Artifacts
uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18
20
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
"bugs": "https://github.com/kj4ezj/is/issues",
"author": {
"name": "Zach Butler",
"email": "[email protected]"
"email": "kj4ezj+is@pm.me"
},
"license": "MIT",
"scripts": {
"act": "act --artifact-server-path .github/artifacts --platform ubuntu-latest=catthehacker/ubuntu:js-latest",
"build": "scripts/build.sh",
"lint": "eslint .",
"reset": "scripts/reset.sh",
"test": "jest --coverage"
},
"files": [
Expand Down
108 changes: 108 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/bash
set -eo pipefail

function ee()
{
echo "$ $*"
eval "$@" || :
}

echo "Starting build. - ${BASH_SOURCE[0]}"
# environment
NPM_ROOT="$(npm run env | grep '^PWD' | cut -d '=' -f '2')"
pushd "$NPM_ROOT"
ee node --version
ee yarn --version
ee npm --version
# package info
PACKAGE_NAME="$(cat package.json | jq -r '.name')"
PACKAGE_VERSION="$(cat package.json | jq -r '.version')"
echo "Found package.json for \"$PACKAGE_NAME\" version \"$PACKAGE_VERSION\"."
# git info
export GIT_COMMIT="$(git rev-parse HEAD)"
export GIT_SHORT_COMMIT="$(git rev-parse --short HEAD)"
export GIT_TAG="$(git --no-pager tag --points-at HEAD)"
# verify git tag matches package.json version, if it exists
if [[ -z "$GIT_TAG" ]]; then
printf '\e[1;33mNOTICE: Not a tagged build, no software will be published.\e[0m\n'
elif [[ "$GIT_TAG" != "v$PACKAGE_VERSION" ]]; then
printf '\e[1;31mERROR: The git tag does not match the package.json version!\e[0m\n'
echo " git tag: $GIT_TAG"
echo "package.json version: $PACKAGE_VERSION"
echo 'These must match to build a release. Rejecting build.'
exit 10
else
printf '\e[1;36mNOTICE: Tagged build, software will be published!\e[0m\n'
fi
# git branch
export GIT_BRANCH="$(git branch --show-current)"
if [[ -z "$GIT_BRANCH" ]]; then # detached head, find tag on base branch or return a feature branch
BRANCHES="$(git branch --contains "$GIT_COMMIT" | tail -n +2 | tr -d ' ')" # get branches containing tag
if [[ "$(echo "$BRANCHES" | grep -cP '^develop$')" == '1' ]]; then
export GIT_BRANCH='develop'
elif [[ "$(echo "$BRANCHES" | grep -cP '^main$')" == '1' ]]; then
export GIT_BRANCH='main'
elif [[ "$(echo "$BRANCHES" | grep -cP '^master$')" == '1' ]]; then
export GIT_BRANCH='master'
else
export GIT_BRANCH="$(echo "$BRANCHES" | tail -n 1 | tr -d '[:space:]')"
fi
fi
# github actions info
if [[ -n "$GITHUB_TRIGGERING_ACTOR" ]]; then
export ACTOR="$GITHUB_TRIGGERING_ACTOR"
elif [[ -n "$GITHUB_ACTOR" ]]; then
export ACTOR="$GITHUB_ACTOR"
else
export ACTOR="$USER@$HOSTNAME"
fi
[[ -z "$GITHUB_ACTIONS" ]] && export GITHUB_ACTIONS='false'
# pack metadata into the package.json
echo 'Adding git metadata to package.json.'
export UNIX_TIME="$(date +%s)"
ee "mv package.json package.json.$UNIX_TIME.bak"
cat package.json.$UNIX_TIME.bak | jq \
--arg branch "$GIT_BRANCH" \
--argjson is_gh_action "$GITHUB_ACTIONS" \
--arg node_version "$(node --version)" \
--arg tag "$GIT_TAG" \
'.git += {
actor: env.ACTOR,
branch: (if $branch == "" then null else $branch end),
build: (if $is_gh_action then (env.GITHUB_RUN_NUMBER | tonumber) else "local" end),
build_id: (if $is_gh_action then (env.GITHUB_RUN_ID | tonumber) else null end),
build_node_version: $node_version,
build_time: (env.UNIX_TIME | tonumber),
build_url: (if $is_gh_action then (env.GITHUB_SERVER_URL + "/" + env.GITHUB_REPOSITORY + "/actions/runs/" + env.GITHUB_RUN_ID) else null end),
commit: env.GIT_COMMIT,
short_commit: env.GIT_SHORT_COMMIT,
tag: (if $tag == "" then null else $tag end)
}' > package.json
ee 'cat package.json | jq .git'
# build
echo 'Building...'
ee 'npm pack'
# validate
if [[ "$CI" == 'true' ]]; then
echo 'Validating package can be installed...'
ee 'npm install -g kj4ezj-is-*.tgz'
else
printf '\e[1;33mNOTICE: Skipping validation step for local build.\e[0m\n'
fi
# publish
echo 'Publishing...'
if [[ -z "$GIT_TAG" ]]; then
printf '\e[1;33mNOTICE: Not a tagged build, no software will be published.\e[0m\n'
elif [[ "$CI" != 'true' || -n "$ACT" ]]; then
printf '\e[1;33mNOTICE: Skipping publish step for local build.\e[0m\n'
elif [[ "$NODE_MAJOR_VERSION" != "$(cat .nvmrc | tr -d '[:space:]')" ]]; then
printf '\e[1;33mNOTICE: Skipping publish step for older Node.js matrix job.\e[0m\n'
else
ee 'npm publish --provenance --access public'
printf '\e[1;32mPublished to npm!\e[0m\n'
fi
# clean up
echo 'Cleaning up.'
ee mv package.json.$UNIX_TIME.bak package.json
popd
echo "Done. - ${BASH_SOURCE[0]}"
20 changes: 20 additions & 0 deletions scripts/reset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
set -eo pipefail

function ee()
{
echo "$ $*"
eval "$@" || :
}

echo 'Resetting nodeJS environment...'
NPM_ROOT="$(npm run env | grep '^PWD' | cut -d '=' -f '2')"
pushd "$NPM_ROOT"
ee 'jest --clearCache'
ee 'rm -r coverage node_modules'
ee 'rm package-lock.json'
ee 'rm kj4ezj-is-*.tgz'
ee 'yarn cache clean'
echo 'nodeJS environment sanitized.'
popd
echo "Done. - ${BASH_SOURCE[0]}"