3

围绕这个话题似乎有很多讨论,但没有什么完全适合我的情况,到目前为止还没有为我解决。

我将我的代码放在 aws codecommit 中。

我已经为我在 AWS 中运行的一个 Ubuntu 实例创建了一个 AMI,并使用这个 AMI 和一个自动扩展组创建了一个启动配置。

我想每个月左右对我的启动配置 AMI 进行基础/修改,以确保 AMI 本身具有最近更新的代码,因此新启动的实例(通过自动缩放)可以在启动时从 codecommit 存储库中提取最新更改 - 从而减少启动时间。

为了实现这一点,我在用户数据 (cloud-init) 脚本中放置了以下代码,并选择了一个 IAM 角色,该角色对所有 EC2 和 codecommit 以及 IAM:Passrole 权限具有完全权限。但是在启动时,脚本总是会抛出错误并且不会拉动更改(我故意在 repo 中保留一个文件以进行测试)

选项1

#!/bin/bash
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
cd /path/to/my/folder/
git remote set-url origin https://git-codecommit.ap-southeast-2.amazonaws.com/v1/repos/reponame
git pull origin master

它抛出以下错误

Error
fatal: $HOME not set
fatal: $HOME not set
fatal: Not a git repository (or any of the parent directories): .git
fatal: could not read Username for 'https://git-codecommit.ap-southeast-2.amazonaws.com': No such device or address

选项 2 -

使用 SSH 也尝试过此选项(尽管尚未尝试对此进行任何进一步的修复)

#!/bin/bash
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
cd /path/to/my/folder/
git remote set-url origin ssh://git-codecommit.ap-southeast-2.amazonaws.com/v1/repos/reponame
git pull origin master

有一个不同的错误 -

Errpr: 
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

有人可以帮我理解我哪里出错了吗?

谢谢。

4

1 回答 1

3

在选项 1 中,似乎尚未创建主目录。当您设置全局 git 配置时,它将进入主目录的 .gitconfig 文件。尽管该选项不需要是全局的,例如您可以将行的顺序切换为:

cd /path/to/my/folder/ git config credential.helper '!aws codecommit credential-helper $@' git config credential.UseHttpPath true

前提是您已正确设置 EC2 实例角色,并且您的 AWS CLI 能够从 EC2 元数据中获取 EC2 实例角色凭证以调用 AWS API。

虽然从输出中不清楚是否安装了 AWS CLI。需要为您发布的 git config 行安装 CLI,因为它会调用“aws codecommit credential-helper”来根据实例角色凭据获取临时用户名和密码。

在选项 2 中,您根本不需要使用凭证助手。如果文档中没有明确说明,我很抱歉。但是,您确实需要将公钥上传到 IAM(此处的说明:http: //docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-unixes.html#setting-up-ssh -unixes 键

您还需要想办法将您的公钥和私钥对分配给您尝试扩展的 EC2 实例,这可能非常麻烦。

您还可以为 CodeCommit ( http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html#setting-up-gc-iam ) 生成静态凭证并将它们放在您的 EC2 实例上在类似 .netrc 文件中。

IMO 选项 1 似乎是最安全的,因为您不必处理传递秘密的问题。

于 2017-09-12T02:21:35.653 回答