在 AWS CodeBuild 中为 https git clone 设置凭证
Setting credentials for https git clone in AWS CodeBuild
我是 运行 一个项目的 CodeBuild,该项目的私有需求存储在 CodeCommit 中。
我需要在 buildspec.yml
中添加一个命令来加载 https git 凭据,以便 git clone
在 CodeBuild 运行 pip install
时工作。
构建失败 fatal: could not read Username for 'https://git-codecommit.us-west-2.amazonaws.com': No such device or address
由于 CodeBuild 环境使用 IAM 角色作为凭据(而不是用户名和密码),您需要在构建规范中配置 CodeCommit credential helper:
phases:
install:
commands:
- git config --global credential.helper '!aws codecommit credential-helper $@'
- git config --global credential.UseHttpPath true
CodeBuild 现在通过将 "git-credential-helper" 设置为 yes 来为此构建规范提供更简单的拨号。文档@https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-syntax
您可以使用 git 中的 de credentils 助手,您可以将以下命令放入 builspec 管道
phases:
install:
commands:
- echo "https://username:password@bitbucket.org" > ~/.git-credentials
- git config credential.helper 'store'
- 列表项
所以我也在寻找一种方法来做到这一点。我想出了两种无需使用代入角色即可克隆存储库的方法。我从你的 post 收集到的信息你想通过 buildspec.yml
克隆回购协议
前面提到的第一个选项 post 是使用 Codebuild 提供的本机功能。需要注意的是,它仅限于您所在的 AWS 帐户(至少这是我的研究在不使用 Codepipeline 的情况下引导我的地方)。我也提供了一个示例供您查看。
我还假设你们中的大多数人以前使用过 AWS Codecommit,并且知道如何设置用户以连接到存储库。如果您还没有,请访问此页面并熟悉 Codecommit 配置。下面列出了可以帮助您解决此问题的链接。
使用一个 AWS 账户克隆该 AWS 账户内的 Codecommit 存储库:
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
这项工作的关键是确保启用某些设置。
- git-credential-helper: 是
- Python
- pip 安装git-remote-codecommit
- git 克隆 codecommit://your-repo1-name。 (命令必须完全像这样)
我再次重申,我只在一个 AWS 账户中完成了这项工作。迄今为止,如果不利用其他 AWS 服务,我无法完成这项工作 cross-account。为了避免利用其他服务,我能够将它们放在一起,创建一个可以使用 SSH 的 AWS Codecommit 用户。对于此示例,我将我的 ssh 私钥以及 ssh 密钥 ID 存储在参数存储区中。我将分享其他可以利用我将附加的 S3 存储桶的方法,但下面的示例是动态构建 RSA 和配置。
连接不同AWS账户Codecommit仓库的SSH方法
version: 0.2
env:
parameter-store:
ssh_key: variable_ssh_key
cc_user: variable_codecommit_user
git-credential-helper: yes
phases:
install:
commands:
- echo UPDATING SSH CLIENT
- "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
- 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
pre_build:
commands:
- aws --version
- git --version
# Adds a private SSH key to allow us to clone or npm install Git repositories
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
# Configure SSH Key
#- ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa <<< y. #generate a new ssh key on demand
- echo "$ssh_key" > ~/.ssh/id_rsa
- cd ~/.ssh/
- cat id_rsa
- |
echo "Multiline command"
cat > ~/.ssh/config <<EOL
Host host-unique-name
Hostname git-codecommit.us-east-1.amazonaws.com
User ${cc_user}
IdentityFile ~/.ssh/id_rsa
EOL
- cat ~/.ssh/config
# Configure SSH Permissions
- chmod 700 ~/.ssh
- chmod 600 ~/.ssh/config
- chmod 600 ~/.ssh/id_rsa
- ssh-keyscan -t rsa1,rsa,dsa git-codecommit.us-east-1.amazonaws.com >> ~/.ssh/known_hosts
# Clone directories
- echo CLONE DIRECTORIES
- mkdir /usr/bin/repo
- cd /usr/bin/repo
#leveraging typical git clone
- git clone ssh://host-unique-name/v1/repos/your-repo1-name
build:
commands:
- cd /usr/bin/new-repo1-name
- do your git commands from here
如您所见,这将创建 ssh 密钥并允许 Codebuild 在本地克隆存储库。请注意,我正在为使用 S3 下载 RSA_ID.
的类似示例添加 link
示例 SSH 克隆 S3 存储桶:
https://gist.github.com/gemmadlou/36deec54dea3defbdd8cbd6574e0261d
这项工作的关键是确保启用某些设置。
环境阶段
- git-credential-helper: 是
安装阶段
- “哪个 ssh-agent || ( apt-get 更新 -y && apt-get 安装 openssh-client -y )”
- Python
- pip 安装git-remote-codecommit
pre_build:
- 创建 .ssh 目录
- 添加 rsa 私钥
- 创建 AWS 配置文件
- 配置 ssh 密钥和配置文件的权限
- 创建 known_host 文件(非常重要)
这里有额外的 link 供参考,帮助我将这些放在一起:
- Setup steps for HTTPS Connections to AWS Codecommit
- AWS Cross account access to Codecommit repo with roles only
- Fetch AWS Secret keys from Bash script
- CWN connecting to codecommit repos using AWS Pipeline (codecommit, codebuild, codepipeline)
- Codecommit setup ssh key
我是 运行 一个项目的 CodeBuild,该项目的私有需求存储在 CodeCommit 中。
我需要在 buildspec.yml
中添加一个命令来加载 https git 凭据,以便 git clone
在 CodeBuild 运行 pip install
时工作。
构建失败 fatal: could not read Username for 'https://git-codecommit.us-west-2.amazonaws.com': No such device or address
由于 CodeBuild 环境使用 IAM 角色作为凭据(而不是用户名和密码),您需要在构建规范中配置 CodeCommit credential helper:
phases:
install:
commands:
- git config --global credential.helper '!aws codecommit credential-helper $@'
- git config --global credential.UseHttpPath true
CodeBuild 现在通过将 "git-credential-helper" 设置为 yes 来为此构建规范提供更简单的拨号。文档@https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-syntax
您可以使用 git 中的 de credentils 助手,您可以将以下命令放入 builspec 管道
phases:
install:
commands:
- echo "https://username:password@bitbucket.org" > ~/.git-credentials
- git config credential.helper 'store'
- 列表项
所以我也在寻找一种方法来做到这一点。我想出了两种无需使用代入角色即可克隆存储库的方法。我从你的 post 收集到的信息你想通过 buildspec.yml
克隆回购协议前面提到的第一个选项 post 是使用 Codebuild 提供的本机功能。需要注意的是,它仅限于您所在的 AWS 帐户(至少这是我的研究在不使用 Codepipeline 的情况下引导我的地方)。我也提供了一个示例供您查看。
我还假设你们中的大多数人以前使用过 AWS Codecommit,并且知道如何设置用户以连接到存储库。如果您还没有,请访问此页面并熟悉 Codecommit 配置。下面列出了可以帮助您解决此问题的链接。
使用一个 AWS 账户克隆该 AWS 账户内的 Codecommit 存储库:
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
这项工作的关键是确保启用某些设置。
- git-credential-helper: 是
- Python
- pip 安装git-remote-codecommit
- git 克隆 codecommit://your-repo1-name。 (命令必须完全像这样)
我再次重申,我只在一个 AWS 账户中完成了这项工作。迄今为止,如果不利用其他 AWS 服务,我无法完成这项工作 cross-account。为了避免利用其他服务,我能够将它们放在一起,创建一个可以使用 SSH 的 AWS Codecommit 用户。对于此示例,我将我的 ssh 私钥以及 ssh 密钥 ID 存储在参数存储区中。我将分享其他可以利用我将附加的 S3 存储桶的方法,但下面的示例是动态构建 RSA 和配置。
连接不同AWS账户Codecommit仓库的SSH方法
version: 0.2
env:
parameter-store:
ssh_key: variable_ssh_key
cc_user: variable_codecommit_user
git-credential-helper: yes
phases:
install:
commands:
- echo UPDATING SSH CLIENT
- "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
- 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
pre_build:
commands:
- aws --version
- git --version
# Adds a private SSH key to allow us to clone or npm install Git repositories
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
# Configure SSH Key
#- ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa <<< y. #generate a new ssh key on demand
- echo "$ssh_key" > ~/.ssh/id_rsa
- cd ~/.ssh/
- cat id_rsa
- |
echo "Multiline command"
cat > ~/.ssh/config <<EOL
Host host-unique-name
Hostname git-codecommit.us-east-1.amazonaws.com
User ${cc_user}
IdentityFile ~/.ssh/id_rsa
EOL
- cat ~/.ssh/config
# Configure SSH Permissions
- chmod 700 ~/.ssh
- chmod 600 ~/.ssh/config
- chmod 600 ~/.ssh/id_rsa
- ssh-keyscan -t rsa1,rsa,dsa git-codecommit.us-east-1.amazonaws.com >> ~/.ssh/known_hosts
# Clone directories
- echo CLONE DIRECTORIES
- mkdir /usr/bin/repo
- cd /usr/bin/repo
#leveraging typical git clone
- git clone ssh://host-unique-name/v1/repos/your-repo1-name
build:
commands:
- cd /usr/bin/new-repo1-name
- do your git commands from here
如您所见,这将创建 ssh 密钥并允许 Codebuild 在本地克隆存储库。请注意,我正在为使用 S3 下载 RSA_ID.
的类似示例添加 link示例 SSH 克隆 S3 存储桶: https://gist.github.com/gemmadlou/36deec54dea3defbdd8cbd6574e0261d
这项工作的关键是确保启用某些设置。
环境阶段
- git-credential-helper: 是
安装阶段
- “哪个 ssh-agent || ( apt-get 更新 -y && apt-get 安装 openssh-client -y )”
- Python
- pip 安装git-remote-codecommit
pre_build:
- 创建 .ssh 目录
- 添加 rsa 私钥
- 创建 AWS 配置文件
- 配置 ssh 密钥和配置文件的权限
- 创建 known_host 文件(非常重要)
这里有额外的 link 供参考,帮助我将这些放在一起:
- Setup steps for HTTPS Connections to AWS Codecommit
- AWS Cross account access to Codecommit repo with roles only
- Fetch AWS Secret keys from Bash script
- CWN connecting to codecommit repos using AWS Pipeline (codecommit, codebuild, codepipeline)
- Codecommit setup ssh key