package.json的版本號(hào)更新優(yōu)化方法
引言
本文的起因是有在代碼倉庫發(fā)包后,同事問我“為什么package.json 里的版本還是原來的,有沒有更新?”,這個(gè)時(shí)候我意識(shí)到,我們完全沒有必要在每次發(fā)布的時(shí)候還特意去關(guān)注這個(gè)倉庫的版本號(hào),只要在發(fā)布打tag的時(shí)候同步一下即可,于是有了本文的實(shí)踐。
node.js 部分,我們得有一個(gè)更改倉庫代碼的腳步留給ci執(zhí)行
我們首先需要在工程目錄中的 ./script/..
目錄下增加一個(gè) update-version.js
腳本
//update-version.js const path = require('path'); const fs = require('fs'); const newVersion = process.argv[2].replace(/^v/, '');; // 獲取命令行參數(shù)中的新版本號(hào),并過濾v字頭 if (!newVersion) { console.log('請(qǐng)傳入新版本號(hào),版本號(hào)遵循semver規(guī)范 .eg: 1.0.0, 1.0.1, 1.1.0'); process.exit(1); } // 獲取當(dāng)前命令行上下文路徑 const currentDirectory = process.cwd(); // 獲取 package.json 文件中的版本號(hào) const packageJsonPath = path.join(currentDirectory, 'package.json'); const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8'); const packageJson = JSON.parse(packageJsonContent); const currentVersion = packageJson.version; // 更新 package.json 文件中的版本號(hào) packageJson.version = newVersion; fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); console.log(`版本號(hào)已從 ${currentVersion} 更新為 ${newVersion}`);
接下來在 package.json script 配置后可以直接使用 npm run version <version>
中觸發(fā)變更版本號(hào)腳本。當(dāng)然這個(gè)前提是想要讓這個(gè)腳本保留給開發(fā)者命令行使用。
{ "name": "version workflow", "version": "1.0.0", "description": "version update demo", "main": "index.js", "scripts": { //... "version": "node ./scripts/update-version.js" }, //... }
CI :如何讓發(fā)布包的行為直接和代碼倉庫中的版本號(hào)同步?
接下來算重頭戲,如何讓發(fā)布包的行為直接和代碼倉庫中的版本號(hào)同步?這里我們使用的是github 提供的github action,具體操作和語法可以查看一下官方文檔,本文就不過多展開。
我們需要在倉庫更目錄增加如下路徑的文件 .github/workflows/update-action.yml
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
狀態(tài)下增加了一個(gè) update job。 它會(huì)做下面幾件事情(在腳本步驟中有)
- 【Checkout code】 切出新的代碼分支;
- 【 Update package.json】在新分支執(zhí)行 update-version.js 傳入
tag_name
更新我們的工程版本號(hào); - 【Commit changes】以你定制的 git config user 信息創(chuàng)建一個(gè)新提交;
- 【Push changes】推送變更回到主干;
ps:正確來說應(yīng)該在發(fā)布執(zhí)行動(dòng)作前prereleased
執(zhí)行我們的 job 但是沒用這個(gè)的原因如下:
Note: The prereleased type will not trigger for pre-releases published from draft releases, but the published type will trigger. If you want a workflow to run when stable and pre-releases publish, subscribe to published instead of released and prereleased.
當(dāng)這個(gè)腳本推送后,執(zhí)行發(fā)布后自動(dòng)更新版本,不用在關(guān)注這個(gè)版本修改問題。 你會(huì)得到下面的效果。
在你的倉庫發(fā)布界面填寫正確tag后發(fā)布
觸發(fā)update job 更改完成
你可能遇到最多的坑
- action 執(zhí)行失敗
Process completed with exit code 129." Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: actions/checkout@v2. For more information, see https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.
這是由于默認(rèn)action job 執(zhí)行環(huán)境的nodejs 版本與actions 包中執(zhí)行腳本不匹配導(dǎo)致,所以一定要使用 checkout@v3 版本 actions/checkout@v3
- 各種不熟悉 action 語法取值導(dǎo)致的問題
可以優(yōu)化的地方
我們前面提交的這個(gè)流程發(fā)布還是有個(gè)問題,你永遠(yuǎn)有個(gè)更超前的 commit hash 在你發(fā)布的 tag 之后
所以這個(gè)action 還有需要繼續(xù)優(yōu)化的地方,那就是同步更新tag hash
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
這個(gè)步驟,在最后獲取這個(gè)版本更新產(chǎn)生的 $git_hash
強(qiáng)制更新到發(fā)布的 tag 上。
我們看看效果
最后我們看版本發(fā)布管理中的 tag hash
搞定!
可以再優(yōu)化的地方
現(xiàn)在我們還有個(gè)問題,就是在執(zhí)行 Commit changes
這個(gè)步驟時(shí)每次 git config user.name "Your github name" git config user.email "your github email"
這里是寫死的,我們可以根據(jù) GitHub Actions 中有一些預(yù)設(shè)的環(huán)境變量可以讀取到當(dāng)前用戶的賬號(hào)和郵箱信息。通過 ${{ env.GITHUB_ACTOR }}
獲取到當(dāng)前執(zhí)行的 Actions 的用戶賬號(hào),通過 ${{ env.GITHUB_ACTOR }}@users.noreply.github.com
獲取到當(dāng)前執(zhí)行的 Actions 的用戶郵箱(該郵箱為 noreply 郵箱,用于 GitHub 的通知,無法發(fā)送郵件)。注意,該郵箱不一定是用戶本身的真實(shí)郵箱,可能是 GitHub 默認(rèn)的郵箱。
如果需要獲取當(dāng)前 GitHub 賬號(hào)的真實(shí)郵箱地址,可以通過 GitHub REST API 進(jìn)行查詢,具體可以參考官方文檔:
這樣我們就需要在Commit Changes
之前再加一個(gè)Set Git user
步驟
- 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 }}
以上就是package.json的版本號(hào)更新優(yōu)化方法的詳細(xì)內(nèi)容,更多關(guān)于package.json版本更新優(yōu)化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
node.js連接mongoDB數(shù)據(jù)庫 快速搭建自己的web服務(wù)
這篇文章主要為大家詳細(xì)介紹了node.js連接mongoDB數(shù)據(jù)庫,如何快速搭建自己的web服務(wù),感興趣的小伙伴們可以參考一下2016-04-04nodejs連接mysql數(shù)據(jù)庫及基本知識(shí)點(diǎn)詳解
這篇文章主要介紹了nodejs連接mysql數(shù)據(jù)庫,結(jié)合實(shí)例形式總結(jié)分析了nodejs連接與操作mysql數(shù)據(jù)庫的相關(guān)模板、配置及mysql數(shù)據(jù)庫查詢、添加數(shù)據(jù)等操作技巧,需要的朋友可以參考下2018-03-03Node.js原理阻塞和EventEmitter及其繼承的運(yùn)用實(shí)戰(zhàn)
這篇文章主要介紹了Node.js原理阻塞和EventEmitter及其繼承的運(yùn)用實(shí)戰(zhàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08利用Node轉(zhuǎn)換Excel成JSON的詳細(xì)步驟
最近工作中遇到一個(gè)需求,大致需求就是將Excel文件在導(dǎo)入時(shí)解析為json格式轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu)再傳輸給后臺(tái),下面這篇文章主要給大家介紹了關(guān)于如何利用Node轉(zhuǎn)換Excel成JSON的詳細(xì)步驟,需要的朋友可以參考下2022-11-11React+react-dropzone+node.js實(shí)現(xiàn)圖片上傳的示例代碼
本篇文章主要介紹了React+react-dropzone+node.js實(shí)現(xiàn)圖片上傳的示例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08