You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions更新version.go文件触发语法错误的问题排查与修复请求

GitHub Actions更新version.go文件触发语法错误的问题排查与修复请求

你遇到的这个SyntaxError: Unexpected identifier错误,核心原因是用错了工具——actions/github-script@v6是专门用来运行JavaScript脚本的,但你在script字段里写的是Bash命令,Node.js解析这些Bash代码自然会报语法错误啦!

下面给你两种清晰的修复方案,你可以根据自己的习惯选择:

方案一:改用Bash步骤执行命令(推荐)

直接把原本的脚本内容放到run指令里,用普通的Bash步骤完成操作,这是最直观且易维护的方式:

name: Update Values on Tag
        
on:
  push:
    tags:
      - 'v*' # Trigger only in case of new tag

jobs:
  update_values:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        # 关闭默认凭证,避免后续推送权限冲突
        with:
          persist-credentials: false
      - name: Update version and push changes
        run: |
          git config --global user.email "jdbdsj@gmail.com"
          git config --global user.name "Github actions"
          echo "Release Tag: ${{ github.ref_name }}"
          release_tag="${{ github.ref_name }}"
          # 更新指定文件中的版本号
          sed -i "s/const version = .*/const version = \"$release_tag\"/" ./hackkerRankSolutions/version.go
          # 注意:这里要指定完整的文件路径,之前的路径会找不到文件
          git add ./hackkerRankSolutions/version.go
          git commit -m "Update version to $release_tag"
          # 使用GITHUB_TOKEN完成推送,确保权限足够
          git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git main

这里还有几个关键细节要注意:

  • actions/checkout添加persist-credentials: false,避免默认的Git凭证干扰后续推送操作
  • git add的路径必须正确,你之前写的version.go没有包含子目录,会导致Git找不到文件,所以要改成./hackkerRankSolutions/version.go
  • 推送时使用https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git的格式,确保拥有仓库的推送权限

方案二:如果坚持使用github-script,转换为JavaScript代码

如果一定要用actions/github-script,你需要把Bash逻辑转换成Node.js的JavaScript代码,示例如下:

name: Update Values on Tag
        
on:
  push:
    tags:
      - 'v*' # Trigger only in case of new tag

jobs:
  update_values:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          persist-credentials: false
      - uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const { execSync } = require('child_process');
            const fs = require('fs');
            const releaseTag = "${{ github.ref_name }}";
            
            // 配置Git用户信息
            execSync('git config --global user.email "jdbdsj@gmail.com"');
            execSync('git config --global user.name "Github actions"');
            
            console.log(`Release Tag: ${releaseTag}`);
            
            // 读取并修改version.go文件
            const filePath = './hackkerRankSolutions/version.go';
            let fileContent = fs.readFileSync(filePath, 'utf8');
            fileContent = fileContent.replace(/const version = .*/, `const version = "${releaseTag}"`);
            fs.writeFileSync(filePath, fileContent);
            
            // 提交并推送代码
            execSync('git add ./hackkerRankSolutions/version.go');
            execSync(`git commit -m "Update version to ${releaseTag}"`);
            execSync(`git push https://${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git main`);

这个方案通过Node.js的child_process模块执行Git命令,用fs模块直接读写文件内容,从而兼容github-script的运行环境。

总的来说,方案一的逻辑更简单,不需要额外的语言转换,后续维护成本更低,更推荐使用。

备注:内容来源于stack exchange,提问作者aman chhabra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 12:43:08