Merge pull request #196 from harris1111/devops/change-logic-build-ci-cd #258
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This GitHub Action will loop through all sub-repos: | |
# If a package is in the publish list and its package.json version is higher than the NPM registry version, it will be published. | |
# For now: The publish list is: | |
# @zkdb/kubo | |
# @zkdb/api | |
# @zkdb/smart-contract | |
# zkdb | |
name: Publish packages in zkDatabase Repository | |
on: | |
push: | |
branches: ['main'] | |
env: | |
NPM_TOKEN: ${{ secrets.NPM_ACCESS_TOKEN }} | |
jobs: | |
build: | |
runs-on: [self-hosted, linux] | |
env: | |
working-directory: ./packges/zkdb | |
strategy: | |
matrix: | |
node-version: [20.x] | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v4 | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Install dependencies & build root | |
run: | | |
echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > ~/.npmrc | |
yarn install | |
yarn build | |
- name: Install compare-versions package | |
run: yarn add -W -D compare-versions@6.1.0 | |
- name: Loop through all packages and publish them if necessary | |
env: | |
NPM_TOKEN: ${{ env.NPM_TOKEN }} | |
run: | | |
echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN} \n always-auth=true" > ~/.npmrc | |
PUBLISH_LIST=("@zkdb/kubo" "@zkdb/api" "@zkdb/smart-contract" "zkdb") | |
packages_dir="packages" | |
# Loop through all packages | |
for package_dir in $(find "$packages_dir" -mindepth 1 -maxdepth 1 -type d); | |
do | |
echo "Processing: $package_dir" | |
cd $package_dir | |
PACKAGE_NAME=$(node -p "require('./package.json').name") | |
if jq -e '.scripts.lint' package.json > /dev/null; then | |
echo "Running lint script for $PACKAGE_NAME" | |
yarn run lint | |
fi | |
yarn build | |
if [[ " ${PUBLISH_LIST[@]} " =~ " ${PACKAGE_NAME} " ]]; then | |
echo "$PACKAGE_NAME is in the publish list" | |
NPM_VERSION=$(npm show "$PACKAGE_NAME" version 2>/dev/null || echo 0.0.0) | |
PACKAGE_VERSION=$(node -p "require('./package.json').version") | |
echo "NPM_VERSION = " $NPM_VERSION | |
echo "PACKAGE_VERSION = " $PACKAGE_VERSION | |
# Doing version comparison | |
VERSION_COMPARE=$(node -e " | |
const compareVersions = require('compare-versions'); | |
const npmVersion = '$NPM_VERSION'; | |
const packageVersion = '$PACKAGE_VERSION'; | |
if (compareVersions.compare(packageVersion, npmVersion, '>')) { | |
console.log('true'); | |
} else { | |
console.log('false'); | |
} | |
") | |
if [ "$VERSION_COMPARE" = "false" ]; then | |
echo "Ignore ${PACKAGE_NAME} since no changes" | |
else | |
echo "Detected: New version of ${PACKAGE_NAME}" | |
npm run release | |
fi | |
else | |
echo "$PACKAGE_NAME is not in the publish list" | |
fi | |
cd - | |
done |