Skip to content

feat: autopublish to npm registry in GH #2

feat: autopublish to npm registry in GH

feat: autopublish to npm registry in GH #2

name: Publish to GitHub Packages
on:
# Trigger on pull requests (for testing/validation)
pull_request:
types: [opened, synchronize, reopened]
# Trigger on pushes to main branch
push:
branches:
- main
# Allow manual triggering
workflow_dispatch:
env:
NODE_VERSION: lts/jod
REGISTRY: npm.pkg.github.com
# Use the repository owner as the scope
SCOPE: '@cowprotocol'
permissions:
contents: read
packages: write
jobs:
publish:
name: Publish to GitHub Packages
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
- name: Remove broken apt repos [Ubuntu]
run: |
for apt_file in `grep -lr microsoft /etc/apt/sources.list.d/`; do sudo rm $apt_file; done
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: https://${{ env.REGISTRY }}
scope: ${{ env.SCOPE }}
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 10.8.0
- name: Cache pnpm store
uses: actions/cache@v4
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Generate code
run: pnpm codegen
- name: Build packages
run: pnpm build
- name: Run tests
run: pnpm test
- name: Run linting
run: pnpm lint
- name: Configure npm for GitHub Packages
run: |
# Override registry for @cowprotocol scope to use GitHub Packages
echo "@cowprotocol:registry=https://${{ env.REGISTRY }}" >> ~/.npmrc
echo "//${{ env.REGISTRY }}/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> ~/.npmrc
# Keep default registry as npmjs.org for other packages
echo "registry=https://registry.npmjs.org/" >> ~/.npmrc
- name: Determine version strategy
id: version
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
# For PRs, use a pre-release version with PR number
VERSION_SUFFIX="pr-${{ github.event.number }}"
echo "version_suffix=$VERSION_SUFFIX" >> $GITHUB_OUTPUT
echo "tag=pr" >> $GITHUB_OUTPUT
echo "is_pr=true" >> $GITHUB_OUTPUT
else
# For main branch, use latest tag
echo "version_suffix=" >> $GITHUB_OUTPUT
echo "tag=latest" >> $GITHUB_OUTPUT
echo "is_pr=false" >> $GITHUB_OUTPUT
fi
- name: Update package versions for PRs
if: steps.version.outputs.is_pr == 'true'
run: |
# Update all package.json files to use PR version
find packages -name "package.json" -exec sh -c '
for file; do
# Extract current version
CURRENT_VERSION=$(node -p "require(\"$file\").version")
# Create PR version
PR_VERSION="${CURRENT_VERSION}-${{ steps.version.outputs.version_suffix }}"
# Update package.json
node -e "
const fs = require(\"fs\");
const pkg = JSON.parse(fs.readFileSync(\"$file\", \"utf8\"));
pkg.version = \"$PR_VERSION\";
fs.writeFileSync(\"$file\", JSON.stringify(pkg, null, 2) + \"\\n\");
"
done
' _ {} \;
- name: Publish packages to GitHub Packages
run: |
# Publish all packages
pnpm publish -r --filter="./packages/**" --tag ${{ steps.version.outputs.tag }} --no-git-checks --registry https://${{ env.REGISTRY }}
- name: Create PR comment with package info
if: steps.version.outputs.is_pr == 'true'
uses: actions/github-script@v7
with:
script: |
const packages = [
'@cowprotocol/cow-sdk',
'@cowprotocol/sdk-trading',
'@cowprotocol/sdk-bridging',
'@cowprotocol/sdk-common',
'@cowprotocol/sdk-config',
'@cowprotocol/sdk-contracts-ts',
'@cowprotocol/sdk-order-book',
'@cowprotocol/sdk-order-signing',
'@cowprotocol/sdk-cow-shed',
'@cowprotocol/sdk-composable',
'@cowprotocol/sdk-weiroll',
'@cowprotocol/sdk-subgraph',
'@cowprotocol/sdk-app-data'
];
const prVersion = '${{ steps.version.outputs.version_suffix }}';
const comment = `## 📦 GitHub Packages Published
The following packages have been published to GitHub Packages with version suffix \`${prVersion}\`:
${packages.map(pkg => `- \`${pkg}@*-${prVersion}\``).join('\n')}
### Installation
To install these packages, configure npm to use GitHub Packages:
\`\`\`bash
npm config set @cowprotocol:registry https://npm.pkg.github.com
npm config set //npm.pkg.github.com/:_authToken YOUR_GITHUB_TOKEN
\`\`\`
Then install with:
\`\`\`bash
npm install @cowprotocol/cow-sdk@*-${prVersion}
\`\`\`
### View Packages
You can view the published packages at: https://github.com/cowprotocol/cow-sdk/packages`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});