name: Release
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- name: Cache node modules
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-20.x-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-20.x-
${{ runner.os }}-node-
- name: Install dependencies
run: npm ci
- name: Build packages
run: npm run build
- name: Run tests
run: npm test
- name: Extract version from tag
id: version
run: |
# Remove 'v' prefix from tag if present
VERSION=${GITHUB_REF_NAME#v}
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Publishing version: $VERSION"
- name: Update package versions
run: |
# Update all package versions to match the release tag
# The --allow-same-version flag prevents errors if versions are already correct
npm version ${{ steps.version.outputs.version }} --workspaces --no-git-tag-version --allow-same-version
# Also update the root package.json
npm version ${{ steps.version.outputs.version }} --no-git-tag-version --allow-same-version
- name: Publish to npm
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
# Set npm token
npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
# Publish each package
echo "Publishing @context-pods/core..."
npm publish --workspace=@context-pods/core --access public
echo "Publishing @context-pods/testing..."
npm publish --workspace=@context-pods/testing --access public
echo "Publishing @context-pods/server..."
npm publish --workspace=@context-pods/server --access public
echo "Publishing @context-pods/cli..."
npm publish --workspace=@context-pods/cli --access public
echo "Publishing @context-pods/create..."
npm publish --workspace=@context-pods/create --access public
echo "Publishing @context-pods/templates..."
npm publish --workspace=@context-pods/templates --access public
- name: Update release notes
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
const release = context.payload.release;
const version = '${{ steps.version.outputs.version }}';
// Append published package info to release notes
const packages = [
'@context-pods/core',
'@context-pods/testing',
'@context-pods/server',
'@context-pods/cli',
'@context-pods/create',
'@context-pods/templates'
];
const publishedInfo = `\n\n## Published Packages (v${version})\n\n` +
packages.map(pkg => `- [\`${pkg}@${version}\`](https://www.npmjs.com/package/${pkg}/v/${version})`).join('\n') +
`\n\n### Installation\n\`\`\`bash\nnpm install ${packages.join(' ')}\n\`\`\``;
await github.rest.repos.updateRelease({
owner,
repo,
release_id: release.id,
body: release.body + publishedInfo
});