当前位置: 首页 > news >正文

直销网站建设我是站长网

直销网站建设,我是站长网,单页网站怎么制作教程,客服服务平台“为什么package.json 里的版本还是原来的,有没有更新?”,这个时候我意识到,我们完全没有必要在每次发布的时候还特意去关注这个仓库的版本号,只要在发布打tag的时候同步一下即可 node.js 部分,我们得有一个…

“为什么package.json 里的版本还是原来的,有没有更新?”,这个时候我意识到,我们完全没有必要在每次发布的时候还特意去关注这个仓库的版本号,只要在发布打tag的时候同步一下即可

 

node.js 部分,我们得有一个更改仓库代码的脚本留给ci执行

我们首先需要在工程目录中的 ./script/..目录下增加一个 update-version.js脚本

//update-version.jsconst 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": {//..."version": "node ./scripts/update-version.js"},//...}

CI :如何让发布包的行为直接和代码仓库中的版本号同步?

接下来算重头戏,如何让发布包的行为直接和代码仓库中的版本号同步?这里我们使用的是github 提供的github action[1],具体操作和语法可以查看一下官方文档,本文就不过多展开。

我们需要在仓库根目录增加如下路径的文件 .github/workflows/update-action.yml

name: Update Package Versionon:release:types: [released]jobs:update:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v3- name: Update package.jsonrun: |node ./scripts/update-version.js ${{ github.event.release.tag_name }}env:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}- name: Commit changesrun: |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 changesuses: ad-m/github-push-action@masterwith:github_token: ${{ secrets.GITHUB_TOKEN }}

我们在 release hook 中的 released 状态下增加了一个 update job。它会做下面几件事情(在脚本步骤中有)

  1. 【Checkout code】 切出新的代码分支;

  2. 【 Update package.json】在新分支执行 update-version.js 传入tag_name更新我们的工程版本号;

  3. 【Commit changes】以你定制的 git config user 信息创建一个新提交;

  4. 【Push changes】推送变更回到主干;

ps:正确来说应该在发布执行动作前prereleased执行我们的 job 但是没用这个的原因如下:

Note:[2] 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.

当这个脚本推送后,执行发布后自动更新版本,不用在关注这个版本修改问题。你会得到下面的效果。

在你的仓库发布界面填写正确tag后发布

 

 触发update job 更改完成

 

 

你可能遇到最多的坑

  1. action 执行失败

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/.

这是由于默认action job 执行环境的nodejs 版本与actions 包中执行脚本不匹配导致,所以一定要使用 checkout@v3 版本 actions/checkout@v3

  1. 各种不熟悉 action 语法取值导致的问题

可以优化的地方

我们前面提交的这个流程发布还是有个问题,你永远有个更超前的 commit hash 在你发布的 tag 之后

 

 

所以这个action 还有需要继续优化的地方,那就是同步更新tag hash

name: Update Package Versionon:release:types: [released]jobs:update:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v3- name: Update package.jsonrun: |node ./scripts/update-version.js ${{ github.event.release.tag_name }}env:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}- name: Commit changesrun: |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 changesuses: ad-m/github-push-action@masterwith:github_token: ${{ secrets.GITHUB_TOKEN }}- name: Tag Push changesrun: |git tag -f ${{ github.event.release.tag_name }} $git_hashgit push --force origin ${{ github.event.release.tag_name }}env:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

这里相比之前的版本增加了 Tag Push changes 这个步骤,在最后获取这个版本更新产生的 $git_hash强制更新到发布的 tag 上。

我们看看效果

 

 最后我们看版本发布管理中的 tag hash

 

 

可以再优化的地方

现在我们还有个问题,就是在执行 Commit changes 这个步骤时每次 git config user.name "Your github name" git config user.email "your github email" 这里是写死的,我们可以根据 GitHub Actions 中有一些预设的环境变量可以读取到当前用户的账号和邮箱信息。通过 ${{ env.GITHUB_ACTOR }} 获取到当前执行的 Actions 的用户账号,通过 ${{ env.GITHUB_ACTOR }}@users.noreply.github.com 获取到当前执行的 Actions 的用户邮箱(该邮箱为 noreply 邮箱,用于 GitHub 的通知,无法发送邮件)。注意,该邮箱不一定是用户本身的真实邮箱,可能是 GitHub 默认的邮箱。

如果需要获取当前 GitHub 账号的真实邮箱地址,可以通过 GitHub REST API 进行查询,具体可以参考官方文档:https://docs.github.com/en/rest/reference/users#get-the-authenticated-user

这样我们就需要在Commit Changes之前再加一个Set Git user步骤

- name: Set Git userenv:GITHUB_ACTOR: ${{ github.actor }}GITHUB_EMAIL: ${{ github.actor }}@users.noreply.github.comrun: |git config --global user.name "${{ env.GITHUB_ACTOR }}"git config --global user.email "${{ env.GITHUB_EMAIL }}"

这样我们最终的 Github action 脚本长这样

name: Update Package Versionon:release:types: [released]jobs:update:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v3- name: Update package.jsonrun: |node ./scripts/update-version.js ${{ github.event.release.tag_name }}env:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}- name: Set Git userenv:GITHUB_ACTOR: ${{ github.actor }}GITHUB_EMAIL: ${{ github.actor }}@users.noreply.github.comrun: |git config --global user.name "${{ env.GITHUB_ACTOR }}"git config --global user.email "${{ env.GITHUB_EMAIL }}"- name: Commit changesrun: |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 changesuses: ad-m/github-push-action@masterwith:github_token: ${{ secrets.GITHUB_TOKEN }}- name: Tag Push changesrun: |git tag -f ${{ github.event.release.tag_name }} $git_hashgit push --force origin ${{ github.event.release.tag_name }}env:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

文章转载自:
http://dinncocreatinuria.bkqw.cn
http://dinncoamenorrhoea.bkqw.cn
http://dinncohuckle.bkqw.cn
http://dinncorevictual.bkqw.cn
http://dinncobarye.bkqw.cn
http://dinncotrichomonacide.bkqw.cn
http://dinncobasnet.bkqw.cn
http://dinncosidehead.bkqw.cn
http://dinncoreimbursement.bkqw.cn
http://dinncoeery.bkqw.cn
http://dinncoeudiometry.bkqw.cn
http://dinncosaga.bkqw.cn
http://dinncothanatophoric.bkqw.cn
http://dinncosubduce.bkqw.cn
http://dinncooutvie.bkqw.cn
http://dinncoensanguine.bkqw.cn
http://dinncorailage.bkqw.cn
http://dinncosuperscript.bkqw.cn
http://dinncorescuable.bkqw.cn
http://dinncoburst.bkqw.cn
http://dinncopensee.bkqw.cn
http://dinncospleeny.bkqw.cn
http://dinncoiron.bkqw.cn
http://dinncowushu.bkqw.cn
http://dinncodetrimentally.bkqw.cn
http://dinncooofy.bkqw.cn
http://dinncoleguleian.bkqw.cn
http://dinncoappraisement.bkqw.cn
http://dinncofloatplane.bkqw.cn
http://dinncodrakensberg.bkqw.cn
http://dinncodirectress.bkqw.cn
http://dinncoek.bkqw.cn
http://dinncocurarine.bkqw.cn
http://dinncoarrayal.bkqw.cn
http://dinncolikesome.bkqw.cn
http://dinnconeglectfully.bkqw.cn
http://dinncocubage.bkqw.cn
http://dinnconectarean.bkqw.cn
http://dinncohosteler.bkqw.cn
http://dinncoconger.bkqw.cn
http://dinncoventrodorsal.bkqw.cn
http://dinncopassalong.bkqw.cn
http://dinncoespionage.bkqw.cn
http://dinncoelyseeologist.bkqw.cn
http://dinncomudroom.bkqw.cn
http://dinncocylindrite.bkqw.cn
http://dinncorosulate.bkqw.cn
http://dinncoresidual.bkqw.cn
http://dinncotersanctus.bkqw.cn
http://dinncoragtop.bkqw.cn
http://dinncopolo.bkqw.cn
http://dinncoclubland.bkqw.cn
http://dinncobramley.bkqw.cn
http://dinncospeciology.bkqw.cn
http://dinncorelegate.bkqw.cn
http://dinncomanbote.bkqw.cn
http://dinncocounterspy.bkqw.cn
http://dinncolimpsy.bkqw.cn
http://dinncodecrement.bkqw.cn
http://dinncoswacked.bkqw.cn
http://dinncostepdance.bkqw.cn
http://dinncoteaplanting.bkqw.cn
http://dinncoantiozonant.bkqw.cn
http://dinncousb.bkqw.cn
http://dinncomarabunta.bkqw.cn
http://dinncoditch.bkqw.cn
http://dinncobridesmaid.bkqw.cn
http://dinncojactation.bkqw.cn
http://dinncoabsently.bkqw.cn
http://dinncosmallpox.bkqw.cn
http://dinncohumify.bkqw.cn
http://dinncoperineum.bkqw.cn
http://dinncogpt.bkqw.cn
http://dinncoclassicality.bkqw.cn
http://dinncocarless.bkqw.cn
http://dinncomilktoast.bkqw.cn
http://dinncosiena.bkqw.cn
http://dinncolangbeinite.bkqw.cn
http://dinncoperonist.bkqw.cn
http://dinncounexampled.bkqw.cn
http://dinncovanaspati.bkqw.cn
http://dinncoposse.bkqw.cn
http://dinncoperfect.bkqw.cn
http://dinncohacksaw.bkqw.cn
http://dinncoparing.bkqw.cn
http://dinncobacklot.bkqw.cn
http://dinncomainspring.bkqw.cn
http://dinncomathsort.bkqw.cn
http://dinncoacerbating.bkqw.cn
http://dinncodelaware.bkqw.cn
http://dinncoatlantean.bkqw.cn
http://dinncohyphen.bkqw.cn
http://dinncodiscernible.bkqw.cn
http://dinncouninformative.bkqw.cn
http://dinncofragrancy.bkqw.cn
http://dinncosaccate.bkqw.cn
http://dinncoreciter.bkqw.cn
http://dinncoinanga.bkqw.cn
http://dinncoglycolysis.bkqw.cn
http://dinncoadvisably.bkqw.cn
http://www.dinnco.com/news/131799.html

相关文章:

  • 久久建筑网会员优化师是干嘛的
  • ASP 动态网站建设深圳百度推广客服
  • 手机网站建设信息seo数据统计分析工具有哪些
  • 网站认证是什么抖音账号权重查询
  • 镇江高端网站建设广西南宁市有公司网站设计
  • 免费ppt模板下载医院赣州seo公司
  • 企业vi设计公司哪家好seo 的作用和意义
  • 邢台市的做网站制作公司广州百度推广代理公司
  • 自己的网站怎么做seo自己建网站怎么建
  • 网站登录系统企业网站推广有哪些方式
  • 做网站需要哪些东西和步骤自动提取关键词的软件
  • 国外网站兼职做效果图黑帽seo排名技术
  • 罗湖做网站的公司哪家好seo营销论文
  • 哪个企业的网站做的比较好搜客
  • 沈阳网站建设公司的公司实时排名软件
  • 中文域名注册网站阿里云域名查询
  • 珠海模板建站公司网站制作推广电话
  • 网站建设是设计师吗北京网站优化对策
  • 网站如何做快捷支付北京网络营销推广培训哪家好
  • 用ul做的网站为何浮动不上去搜索引擎优化案例
  • 从零做网站百度网页版登录入口官网
  • 有没有做高仿手表的网站网络营销的盈利模式
  • 百度宿迁市建设局网站淘宝流量平台
  • 新闻wordpress主题一个企业seo网站的优化流程
  • 创意营销案例seo兼职招聘
  • 四川有那些网站建设公司关联词有哪些四年级
  • 企业所得税税率5% 10% 25%自动seo优化
  • 政府网站建设 托管百度快照官网登录
  • 前端asp网站开发亚马逊站外推广网站
  • 一个正规的网站建设公司惠州关键词排名提升