gpt4 book ai didi

git - AWS 在 EC2 实例启动时从代码提交中提取最新代码

转载 作者:太空狗 更新时间:2023-10-29 14:11:04 25 4
gpt4 key购买 nike

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

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

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

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

为了实现这一点,我在用户数据 (cloud-init) 脚本中放置了以下代码,并选择了一个 IAM 角色,该角色对所有 EC2 和代码提交具有完全权限以及 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.

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

谢谢。

最佳答案

在选项 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 配置行安装 CLI 才能工作,因为它会调用“aws codecommit credential-helper”以根据实例角色凭证获取临时用户名和密码。

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

您还需要想办法将您的公钥和私钥对分发到您要扩展的 EC2 实例,这可能会很麻烦。

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

IMO 选项 1 似乎是最安全的,因为您不必处理四处传递的 secret 。

关于git - AWS 在 EC2 实例启动时从代码提交中提取最新代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46164223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com