简体   繁体   English

"在 AWS CodeBuild 中为 https git clone 设置凭证"

[英]Setting credentials for https git clone in AWS CodeBuild

I am running a CodeBuild on a project that has private requirements stored in CodeCommit.我正在对存储在 CodeCommit 中的私有需求的项目运行 CodeBuild。

I need to add a command in buildspec.yml<\/code> that loads the https git credentials so git clone<\/code> works when CodeBuild runs pip install<\/code> .我需要在buildspec.yml<\/code>中添加一个命令来加载 https git 凭据,以便git clone<\/code>在 CodeBuild 运行pip install<\/code>时工作。

The build fails with fatal: could not read Username for 'https:\/\/git-codecommit.us-west-2.amazonaws.com': No such device or address<\/code>构建失败并出现fatal: could not read Username for 'https:\/\/git-codecommit.us-west-2.amazonaws.com': No such device or address<\/code>

"

Since the CodeBuild environment uses an IAM role for credentials (not a username and password), you will need to configure the CodeCommit credential helper in your buildspec: 由于CodeBuild环境使用IAM角色作为凭证(而不是用户名和密码),因此您需要在buildspec中配置CodeCommit凭证帮助程序:

phases:
  install:
    commands:
      - git config --global credential.helper '!aws codecommit credential-helper $@'
      - git config --global credential.UseHttpPath true

CodeBuild now provides an easier dial for this buildspec by setting the "git-credential-helper" to yes. CodeBuild现在通过将“git-credential-helper”设置为yes,为此buildspec提供更简单的拨号。 Documentation @ https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-syntax 文档@ https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-syntax

You can use de credentils helper from git, you can put the following commands into builspec pipeline 您可以使用来自git的de credentils helper,您可以将以下命令放入builspec管道中

phases:
  install:
    commands:    
    - echo "https://username:password@bitbucket.org" > ~/.git-credentials
    - git config credential.helper 'store'

Git documentation https://git-scm.com/docs/git-credential-store Git文档https://git-scm.com/docs/git-credential-store

So I was looking for a method to do this as well.所以我也在寻找一种方法来做到这一点。 I figured out two ways to clone the repo without the use of assume roles.我想出了两种不使用假设角色来克隆 repo 的方法。 What I am gathering from your post you want to clone the repo via the buildspec.yml我从您的帖子中收集到的内容您想通过 buildspec.yml 克隆存储库

The first option as mentioned in an earlier post is to use the native function that is offered by Codebuild.前一篇文章中提到的第一个选项是使用 Codebuild 提供的本机功能。 The caveat is that it's limited to the AWS account you are in (at least that's where my research has led me without using Codepipeline).需要注意的是,它仅限于您所在的 AWS 账户(至少这是我的研究在没有使用 Codepipeline 的情况下引导我的地方)。 I am providing a sample for you to review as well.我也提供了一个样本供您查看。

I will also assume that most of you have worked with AWS Codecommit before and know how to set up users to connect to repos.我还将假设你们中的大多数人之前都使用过 AWS Codecommit,并且知道如何设置用户以连接到存储库。 If you haven't please visit this page and get familiar with Codecommit configurations.如果您还没有,请访问此页面并熟悉 Codecommit 配置。 Links are listed below that can help you with this.下面列出了可以帮助您解决此问题的链接。

Using One AWS Account to clone Codecommit repository in Single Account:<\/strong>使用一个 AWS 账户在单一账户中克隆 Codecommit 存储库:<\/strong>

version: 0.2
env:

  git-credential-helper: yes
    
phases:
  install:
    commands:
      - echo "STARTING PYTHON INSTALLATION"
      - "curl -s -qL -o python.tgz https://www.python.org/ftp/python/${PY_VERSION}/Python-${PY_VERSION}.tgz"
      - "tar xf python.tgz -C /usr/bin/"
      - "python --version"
      - python -m pip install -U pip
      - pip install git-remote-codecommit

  pre_build:
    commands:
      - aws --version
      - git --version

      # Clone directories
      - echo CLONE DIRECTORIES
      - mkdir /usr/bin/repo
      - cd /usr/bin/repo

      #Leveraging git remote clone for codecommit
      - git clone codecommit://your-repo1-name new-repo1-name 

  build:
    commands: 
      - cd /usr/bin/new-repo1-name 
      - do your git commands from here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM