如何在Package.json依赖项中使用Gitlab CI_JOB_TOKEN

问题描述 投票:0回答:1

以下问题目前阻止我们使用 Gitlab CICD 管道。

我们在自我管理的 Gitlab(版本 17.6.2-ee)服务器上有一个 ReactNative 项目(pr-service),并具有最终订阅。外部环境如下

  • 使用yarn进行包管理
  • Gitlab 运行程序正在运行最新 MacOS 的 MacStudio 机器上运行
  • Runner 目前配置有 shell 执行器
  • Gitlab服务器只支持https,无意使用ssh
  • Package.json 文件,在依赖项下,有一个对单独存储库(在同一 gitlab 服务器中)的引用,如下所示
    "react-native-square-pos": "git+https://gitlab-ci-token:${CI_JOB_TOKEN}@git.ourowncloud.io/app/react-native-square-pos.git"

运行 CICD 作业时,我们收到以下错误

`$ yarn install
yarn install v1.22.22
[1/4] Resolving packages...
error Command failed.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads https://gitlab-ci-token:%24%7BCI_JOB_TOKEN%[email protected]/app/react-native-square-pos.git
Directory: /Users/mobileappbuilder/.colima/default/builds/t2_RoTZgC/0/app/mobile-applications/pronto-service
Output:
remote: HTTP Basic: Access denied. If a password was provided for Git authentication, the password was incorrect or you're required to use a token instead of a password. If a token was provided, it was either incorrect, expired, or improperly scoped. See https://git.ourowncloud.io/help/topics/git/troubleshooting_git.md#error-on-git-fetch-http-basic-access-denied
fatal: Authentication failed for 'https://git.ourowncloud.io/app/react-native-square-pos.git/'`

我们在实验的不同阶段尝试了以下方法

  • 在react-native-square-pos项目的Job Token Permission的允许列表下添加ReactNative项目(pr-service)

  • 授权“所有组和项目”在相同设置下访问react-native-square-pos(因为上述不起作用)

  • 依赖字符串的不同变体

    "react-native-square-pos": "git+https://gitlab-ci-token:[email protected]/app/react-native-square-pos.git"
    "react-native-square-pos": "git+https://${CI_JOB_TOKEN}@git.ourowncloud.io/app/react-native-square-pos.git"

  • 在安装纱线之前直接尝试从管道作业克隆第二个项目。 这次成功了

    $ git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@git.ourowncloud.io/app/react-native-square-pos.git

  • 在 package.json 的依赖项字符串中使用个人令牌 - 这也成功了

    "react-native-square-pos": "git+https://<username>:<personal_access_token>@git.ourowncloud.io/app/react-native-square-pos.git"
    
  • 通过变量使用个人访问令牌 - 同样的错误

    `"react-native-square-pos": "git+https://<username>:${PAT_VAR}@git.ourowncloud.io/app/react-native-square-pos.git"`
    

因此我们假设问题在于如何在 Package.json 依赖变量中指定 CI_JOB_TOKEN (或任何 gitlab 变量)

  • 通过 .npmrc 提供 authToken - 同样的错误

     `//git.ourowncloud.io/:_authToken=${CI_JOB_TOKEN}`
    

有人可以帮我们解决这个问题吗?

gitlab gitlab-ci package.json
1个回答
0
投票

提出问题使问题变得更加清晰。

终于解决了。问题是 Gitlab 变量未在 package.json 中进行评估。它在直接 git 克隆中工作,因为它是在 shell 中评估的。

因此,解决方案是在 shell 级别评估 CI_JOB_TOKEN 并在管道期间将其注入 package.json 中。更改是在管道作业的 before_script 部分中完成的

before_script:
- sed -i '' "s/git.ourowncloud.io/gitlab-ci-token:${CI_JOB_TOKEN}@git.ourowncloud.io/g" package.json

脚本: - 纱线安装

这是理想的,因为它也适用于开发环境(开发人员使用他们的个人访问令牌)

`sed -i ''“s/git.ourowncloud.io/gitlab-ci-token:${CI_JOB_TOKEN}@git.ourowncloud.io/g”package.json'

请注意,git.ourowncloud.io的所有引用都将被上述命令替换。在我们的例子中,package.json 中只有一个

另请注意,sed 命令在 GNU 和 BSD/MacOS 中的工作方式略有不同(BSD/MacOS 需要附加 '')

尝试: `sed -i.bak "s/git.ourowncloud.io/gitlab-ci-token:${CI_JOB_TOKEN}@git.ourowncloud.io/g" package.json'

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.