主题
自动化 Publish & Release
作为一个开源爱好者,自动化 NPM Publish 和 GitHub Release 是非常重要的,可以极大地提高工作效率和发布质量。
GitHub Release 脚本
js
import { execSync } from 'child_process'
const version = process.argv[2]
const tags = ['alpha', 'beta', 'rc', 'next']
function main() {
console.log(`Creating GitHub release for version: ${version}`)
try {
if (tags.some((tag) => version.includes(`-${tag}`))) {
console.log('Creating prerelease GitHub release...')
execSync(
`gh release create ${version} --title "${version}" --notes "For complete changelog, see [CHANGELOG.md](https://github.com/VfanLee/hello-npm/blob/main/CHANGELOG.md)." --prerelease`,
{ stdio: 'inherit' },
)
} else {
console.log('Creating stable GitHub release...')
execSync(
`gh release create ${version} --title "${version}" --notes "For complete changelog, see [CHANGELOG.md](https://github.com/VfanLee/hello-npm/blob/main/CHANGELOG.md)."`,
{ stdio: 'inherit' },
)
}
} catch (error) {
console.error(`Error while creating GitHub release: ${error.message}`)
process.exit(1)
}
}
main()Github Actions 脚本
yaml
name: release
on:
push:
tags:
- 'v*'
jobs:
release:
name: Release
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.node-version'
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
- name: Install deps
run: npm install
- name: Build
run: npm run build
- name: Publish to npm
run: |
VERSION=$(node -p "require('./package.json').version")
echo "Publishing version: $VERSION"
if [[ "$VERSION" == *"-alpha"* ]]; then
TAG="alpha"
echo "Publishing as alpha tag..."
elif [[ "$VERSION" == *"-beta"* ]]; then
TAG="beta"
echo "Publishing as beta tag..."
elif [[ "$VERSION" == *"-rc"* ]]; then
TAG="rc"
echo "Publishing as release candidate tag..."
elif [[ "$VERSION" == *"-next"* ]]; then
TAG="next"
echo "Publishing as next tag..."
else
TAG="latest"
echo "Publishing as latest stable version..."
fi
npm publish --tag $TAG
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create GitHub Release
run: |
npm run release ${{ github.ref_name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}yaml
name: release
on:
push:
tags:
- 'v*'
jobs:
release:
name: Release
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: latest
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.node-version'
registry-url: 'https://registry.npmjs.org'
cache: 'pnpm'
- name: Install deps
run: pnpm install
- name: Build
run: pnpm run build
- name: Publish to npm
run: |
VERSION=$(node -p "require('./package.json').version")
echo "Publishing version: $VERSION"
if [[ "$VERSION" == *"-alpha"* ]]; then
TAG="alpha"
echo "Publishing as alpha tag..."
elif [[ "$VERSION" == *"-beta"* ]]; then
TAG="beta"
echo "Publishing as beta tag..."
elif [[ "$VERSION" == *"-rc"* ]]; then
TAG="rc"
echo "Publishing as release candidate tag..."
elif [[ "$VERSION" == *"-next"* ]]; then
TAG="next"
echo "Publishing as next tag..."
else
TAG="latest"
echo "Publishing as latest stable version..."
fi
pnpm publish --tag $TAG --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create GitHub Release
run: |
pnpm run release ${{ github.ref_name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}