【问题标题】:AWS CodeBuild and CodeCommit repo as npm dependencyAWS CodeBuild 和 CodeCommit 存储库作为 npm 依赖项
【发布时间】:2020-01-02 16:23:00
【问题描述】:

我们有 2 份报告

  1. 回购 1
  2. 回购 2

在 Repo 1 中 > package.json 有一个依赖项

"dependencies": {
    "repo-2": "git+https://git-codecommit.us-east-1.amazonaws.com/v1/repos/repo-2/"
}

然后,在“repo-1”的 CodeBuild 中,我们有以下构建规范

version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 10
    commands:
      - mkdir -p ./deploy
  build:
    commands:
      - echo "Server copy START $(date)"
      - cp -r ./index.js ./deploy/index.js
      - cp -r ./package.json ./deploy/package.json
      - cp -r ./buildspec.yml ./deploy/buildspec.yml
      - echo "Server copy END $(date)"
      - echo "Server npm install START $(date)"
      - cd ./deploy && npm install --production
      - echo "Server npm install END $(date)"
  post_build:
    commands:
artifacts:
  files:
        - '**/*'
  base-directory: 'deploy'

CodeBuild 抛出的错误如下

npm ERR! fatal: unable to access 'https://git-codecommit.us-east-1.amazonaws.com/v1/repos/repo-2/': The requested URL returned error: 403 

基本上,问题是:我可以使用 CodeCommit 存储库作为 npm 依赖项吗?正确的方法是什么?

试试#1

我尝试添加这个(以及类似的变体)但没有成功 https://medium.com/@ngchiwang/aws-npm-install-private-codecommit-module-8512c3203c37

#Try 2

我也试过把依赖URL改成这个

"repo-2": "git://git-codecommit.us-east-1.amazonaws.com/v1/repos/repo-2"

但出现以下错误

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fatal: unable to connect to git-codecommit.us-east-1.amazonaws.com: 
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: git-codecommit.us-east-1.amazonaws.com[0: 52.94.233.146]: errno=Connection refused

【问题讨论】:

  • 403 是“禁止的”。您的 CodeBuild 角色需要访问存储库的权限。
  • CodeBuild 分配的角色可以访问存储库。
  • 我的错@AndrejKaurin,它实际上是一个 NPM 错误;您是否在 CodeBuild 环境中配置了 NPM 凭证?你需要登录 NPM 才能访问私有包。
  • 但是私有包在 CodeCommit 上。
  • 我正在使用“依赖项”,但错误地发布了“devDependencies”。尝试了您的建议,但没有成功。我用错误更新了问题。

标签: amazon-web-services aws-codebuild aws-codecommit


【解决方案1】:

我今天遇到了同样的问题,并通过在 buildspec 文件的 env 部分中启用 git-credential-helper 使其正常工作.

例子:

version: 0.2
env:
  git-credential-helper: yes
phases:
  install:
    runtime-versions:
      nodejs: 10
    commands:
      - npm install
  build:
    commands:
      - npm run build

这与策略中的 CodeCommit 权限(您说您已经拥有)相结合,可以使用来自 CodeCommit 的私有 npm 包进行构建。

【讨论】:

  • 这对我来说是最后一块拼图。我必须执行以下操作:1. 将 package.json 中的依赖项定义为 git+https://... 2. 授予 CodeBuild 角色 CodeCommit 读取权限 3. 此评论
  • 这也解决了我的问题。使用这一行 git-credential-helper: yes codebuild 可以在配置中使用访问令牌来安装私有 repo
【解决方案2】:

我上周遇到了类似的问题,因此将分享为亚马逊团队推荐的解决方案。

为此,更好的方法是在 buildspec 文件的 env 部分中将“git-credential-helper”设置为 yes [1],然后可以使用 https 访问存储库。请参考下面的 BuildSpec 示例。

================Buildspec Snippet=================

版本:0.2

env:
    git-credential-helper: yes

phases:
    pre_build:
        commands:
        - /usr/bin/git ls-remote -h -t https://git-codecommit.us-east-1.amazonaws.com/v1/repos/repo-2/

================Buildspec Snippet=================

另外,请确保您已在 CodeBuild IAM 角色中提供了访问 CodeCommit 存储库所需的权限。我在下面提供了示例 IAM 策略,您可以参考这些策略,根据您的用例提供权限:

===========IAM 政策示例=============

   {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": [
                    "codecommit:GetRepository",
                    "codecommit:GitPull",
                    "codecommit:GetFolder"
                ],
                "Resource": "arn:aws:codecommit:us-east-1:<put repo Name or *>"
            },
            {
                "Sid": "VisualEditor1",
                "Effect": "Allow",
                "Action": "codecommit:ListRepositories",
                "Resource": "*"
            }
        ]
    }

===========IAM 政策示例=============

请检查上述方法是否有助于实现您的用例。

请注意,上述 buildspec sn-p 只是说明如何访问 CodeCommit 存储库的示例,需要根据您的要求进行修改。例如,您可以在 package.json 中描述您的存储库依赖项,如下所示,我假设您已经在这样做,并通过 codebuild 中的 buildspec 文件运行 npm install。

"dependencies": {
    "my-npm": "git+https://git-codecommit.us-east-1.amazonaws.com/v1/repos/<repo name>"
},

【讨论】:

    【解决方案3】:

    尝试使用以下命令将您的私有 AWS CodeCommit 存储库用作您的 npm 模块:

    git config --global credential.helper '!aws codecommit credential-helper $@'
    git config --global credential.UseHttpPath true
    git config --global url."ssh://".insteadOf https://
    npm install --save git+https://<your_repo_url>#master
    

    如果您想改用 npm 依赖项,请在此处查看类似问题的答案:npm install private github repositories by dependency in package.json

    【讨论】:

    • 我已经尝试过了(请参阅我的问题中的链接)但它不起作用
    猜你喜欢
    • 2022-11-19
    • 1970-01-01
    • 2022-08-08
    • 2015-04-28
    • 1970-01-01
    • 2017-12-11
    • 2019-02-25
    • 2020-06-03
    • 2023-04-01
    相关资源
    最近更新 更多