git 在 github push 上将静态站点部署到 s3 的最佳策略?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32530352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:22:54  来源:igfitidea点击:

Best strategy to deploy static site to s3 on github push?

gitamazon-web-servicesgithubamazon-s3deployment

提问by Mike Douglas

I'd like to automate deploying our site to AWS S3. I've written a node script to automate building and uploading the site, but I'd like to have the script automatically run whenever the masterbranch of our repo is updated on github.

我想自动将我们的站点部署到 AWS S3。我已经编写了一个节点脚本来自动构建和上传站点,但我希望只要master我们的 repo 分支在 github 上更新,脚本就会自动运行。

I looked into AWS CodeDeploy, but it looks like that's for specifically deploying to EC2. I've also looked at AWS Lambda, but there doesn't seem to be a clear way to pull a copy of the repo using gitso I can run the script.

我查看了 AWS CodeDeploy,但看起来这是专门部署到 EC2 的。我还查看了 AWS Lambda,但似乎没有一种明确的方法可以使用它来提取存储库的副本,git以便我可以运行脚本。

Any services (preferably tied to AWS) that I can use?

我可以使用任何服务(最好与 AWS 相关联)?

采纳答案by ocean

Rather than using an AWS service directly (as you say they nearly all expect a much more complicated setup, deploying to EC2 etc), you might be better off using a CI provider such as Shippable, Codeshipor Wercker.

而不是直接使用的AWS服务(如你所说,他们几乎都希望一个更复杂的设置,部署到EC2等),你可能会更好使用CI提供商,如可发运CodeshipWercker

These all have the ability to fire from gitupdates, run build commands, install utilities into their CI images/containers and copy files to S3.

这些都能够从git更新中触发、运行构建命令、将实用程序安装到它们的 CI 映像/容器中并将文件复制到 S3。

There's probably some startup which has built an exact tool for your purpose, but they haven't appeared on my radar yet :-)

可能有一些初创公司为您的目的构建了一个精确的工具,但它们还没有出现在我的雷达上:-)

回答by Matthias Rosenstock

I had the same goal some time ago and have now released a little tool, which solves the problem at least for me. It uses AWS Lambdaand deploys a specific branch of the repository to S3after push. You can take full advantage of the GitHub deploy key, which has fewer permissions as personal access tokens and can be configured per repository.

前段时间我也有同样的目标,现在发布了一个小工具,至少对我来说解决了这个问题。它使用AWS Lambda存储库的特定分支并将其部署到S3after push。您可以充分利用 GitHub 部署密钥,该密钥作为个人访问令牌的权限较少,并且可以针对每个存储库进行配置。

Please take a look at github-bucket, it might help you too.

请查看github-bucket,它也可能对您有所帮助。

github-s3-deploy-architecture

github-s3-deploy-架构

回答by Damien Sawyer

I know it's not git deploy.... But Instead of setting up a CI box, I just used s3cmd.

我知道这不是 git deploy .... 但是我没有设置 CI 框,而是使用了 s3cmd。

http://s3tools.org/s3cmd

http://s3tools.org/s3cmd

Executing this command syncs my build directory with s3.

执行此命令会将我的构建目录与 s3 同步。

s3cmd?sync -r ~/code/mysite/build s3://www.mysite.com?--delete-removed

I'm using it on Linux. Not sure what their OSX and Windows stories are.

我在 Linux 上使用它。不确定他们的 OSX 和 Windows 故事是什么。

If you're really after a git push solution, you could set up a timed job which pulls your git repo to a folder and then executes this against it. I do this elsewhere on a cheap Linux VM. Unless you're going to go full CI though, there's probably not much point.

如果你真的想要一个 git push 解决方案,你可以设置一个定时作业,将你的 git repo 拉到一个文件夹,然后对其执行。我在其他地方的廉价 Linux VM 上这样做。除非你打算使用完整的 CI,否则可能没有多大意义。

回答by Patrick Chu

You can set this up with a very simple 2-step CodePipeline. Basically, you just fill out the different sections in the AWS Console. There's no need for a separate CI tool and its added complexity.

您可以使用非常简单的两步 CodePipeline 进行设置。基本上,您只需填写 AWS 控制台中的不同部分。不需要单独的 CI 工具及其增加的复杂性。

In the first step of the pipline, pull from Github and store in S3. You can easily set this up through the AWS Console.

在管道的第一步中,从 Github 拉取并存储在 S3 中。您可以通过 AWS 控制台轻松进行设置。

In the next CodeDeploy step, you can use the AWS CLI (pre-installed in CodeDeploy) to do a

在下一个 CodeDeploy 步骤中,您可以使用 AWS CLI(预安装在 CodeDeploy 中)执行

cd /path/to/public/directory && aws s3 sync --acl public-read --delete . s3://your.bucket.name

You'll have to set the environment variables for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY so that AWS CLI can run during your deploy step, and that can also be done in the AWS console for CodeDeploy, in the Advanced section under "environment variables". Once the environment variables have been set and if that AWS user has the correct permissions, you can run any aws-cli command you want inside your CodeDeploy.

您必须为 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 设置环境变量,以便 AWS CLI 可以在您的部署步骤中运行,也可以在 CodeDeploy 的 AWS 控制台的“环境变量”下的“高级”部分中完成。一旦设置了环境变量并且该 AWS 用户具有正确的权限,您就可以在 CodeDeploy 中运行您想要的任何 aws-cli 命令。

Once this is done, when you check in to Github, CodePipeline will kick off, and a few minutes later your files will be on S3.

完成此操作后,当您登录 Github 时,CodePipeline 将启动,几分钟后您的文件将在 S3 上。

回答by Michal Frystacky

If you are using TravisCI, the deploymentis pretty straight forward. The only part you will need to add to your .travis.ymlis :

如果您使用 TravisCI,部署非常简单。您需要添加到您的唯一部分.travis.yml是:

deploy:                         
  provider: s3                         
  access_key_id: "YOUR AWS ACCESS KEY"
  secret_access_key:
    secure: "w/DlbHt1+IAMENCRYPTED"
  bucket: "YOUR BUCKET"

My blog postexplains all the details for the AWS side (user setup, IAM and S3 bucket configurations) as well as the github and travisCI side.

我的博客文章解释了 AWS 端(用户设置、IAM 和 S3 存储桶配置)以及 github 和 travisCI 端的所有细节。

回答by Dave Maple

Perhaps overpowered for your simple use case, but you could create a very simple CodePipelineto push your github repository to S3.

也许对于您的简单用例来说已经不堪重负,但您可以创建一个非常简单的CodePipeline将您的 github 存储库推送到 S3。

回答by Gabriel Wu

i also recommend to use codeship,simple and easy, but you need to create IAM user with proper permission (which is policy) to S3 bucket.

我还建议使用 codeship,简单易行,但您需要创建具有适当权限(这是策略)的 IAM 用户到 S3 存储桶。

the basic plan for codeship is free.

Codeship 的基本计划是免费的。

Well there might be a problem so far i can see codeship will not remove files as you remove files in github, after all, s3 is not github repo, but anyway, the putObject operations for lots of github update just works good enough to me.

好吧,到目前为止可能存在问题,我可以看到 codeship 不会在您删除 github 中的文件时删除文件,毕竟,s3 不是 github repo,但无论如何,许多 github 更新的 putObject 操作对我来说已经足够好了。

回答by Anthony Delgado

I have used Deploy Bot in the past and I have been quite happy with it.

我过去使用过 Deploy Bot,我对它非常满意。

It can push to S3 or even FTP via Git and also it can run your build scripts and even push a notification to Slack for you.

它可以通过 Git 推送到 S3 甚至 FTP,还可以运行您的构建脚本,甚至可以为您推送通知到 Slack。

https://deploybot.com/

https://deploybot.com/