//update-version.js const path = require('path'); const fs = require('fs'); const newVersion = process.argv[2].replace(/^v/, '');; // 获取命令行参数中的新版本号,并过滤v字头 if (!newVersion) { console.log('请传入新版本号,版本号遵循semver规范 .eg: 1.0.0, 1.0.1, 1.1.0'); process.exit(1); } // 获取当前命令行上下文路径 const currentDirectory = process.cwd(); // 获取 package.json 文件中的版本号 const packageJsonPath = path.join(currentDirectory, 'package.json'); const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8'); const packageJson = JSON.parse(packageJsonContent); const currentVersion = packageJson.version; // 更新 package.json 文件中的版本号 packageJson.version = newVersion; fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); console.log(`版本号已从 ${currentVersion} 更新为 ${newVersion}`);接下来在 package.json script 配置后可以直接使用 npm run version <version> 中触发变更版本号脚本。当然这个前提是想要让这个脚本保留给开发者命令行使用。
{ "name": "version workflow", "version": "1.0.0", "description": "version update demo", "main": "index.js", "scripts": { // 堆代码 duidaima.com "version": "node ./scripts/update-version.js" }, //... }CI :如何让发布包的行为直接和代码仓库中的版本号同步?
name: Update Package Version on: release: types: [released] jobs: update: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Update package.json run: | node ./scripts/update-version.js ${{ github.event.release.tag_name }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Commit changes run: | git config user.name "Your github name" git config user.email "your github email" git add . git commit -m "Update version to ${{ github.event.release.tag_name }} for release ${{ github.ref }}" - name: Push changes uses: ad-m/github-push-action@master with: github_token: ${{ secrets.GITHUB_TOKEN }}我们在 release hook 中的 released 状态下增加了一个 update job。它会做下面几件事情(在脚本步骤中有):
在你的仓库发布界面填写正确tag后发布
2.各种不熟悉 action 语法取值导致的问题
name: Update Package Version on: release: types: [released] jobs: update: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Update package.json run: | node ./scripts/update-version.js ${{ github.event.release.tag_name }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Commit changes run: | git config user.name "Your github name" git config user.email "your github email" git add . git commit -m "Update version to ${{ github.event.release.tag_name }} for release ${{ github.ref }}" git_hash=$(git rev-parse --short HEAD) - name: Push changes uses: ad-m/github-push-action@master with: github_token: ${{ secrets.GITHUB_TOKEN }} - name: Tag Push changes run: | git tag -f ${{ github.event.release.tag_name }} $git_hash git push --force origin ${{ github.event.release.tag_name }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}这里相比之前的版本增加了 Tag Push changes 这个步骤,在最后获取这个版本更新产生的 $git_hash强制更新到发布的 tag 上。
最后我们看版本发布管理中的 tag hash
搞定!
- name: Set Git user env: GITHUB_ACTOR: ${{ github.actor }} GITHUB_EMAIL: ${{ github.actor }}@users.noreply.github.com run: | git config --global user.name "${{ env.GITHUB_ACTOR }}" git config --global user.email "${{ env.GITHUB_EMAIL }}"这样我们最终的 Github action 脚本长这样:
name: Update Package Version on: release: types: [released] jobs: update: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Update package.json run: | node ./scripts/update-version.js ${{ github.event.release.tag_name }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Set Git user env: GITHUB_ACTOR: ${{ github.actor }} GITHUB_EMAIL: ${{ github.actor }}@users.noreply.github.com run: | git config --global user.name "${{ env.GITHUB_ACTOR }}" git config --global user.email "${{ env.GITHUB_EMAIL }}" - name: Commit changes run: | git add . git commit -m "Update version to ${{ github.event.release.tag_name }} for release ${{ github.ref }}" git_hash=$(git rev-parse --short HEAD) - name: Push changes uses: ad-m/github-push-action@master with: github_token: ${{ secrets.GITHUB_TOKEN }} - name: Tag Push changes run: | git tag -f ${{ github.event.release.tag_name }} $git_hash git push --force origin ${{ github.event.release.tag_name }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}最后