k8s aws 部署_如何在短短30分钟内使用CircleCI设置到AWS S3的持续部署
k8s aws 部署by Adam Watt 通过亚当·瓦特如何在短短30分钟内使用CircleCI设置到AWS S3的持续部署 (How to setup Continuous Deployment to AWS S3 using CircleCI in just 30 minutes)Continuous Deployment might seem complicated at fir...
k8s aws 部署
by Adam Watt
通过亚当·瓦特
如何在短短30分钟内使用CircleCI设置到AWS S3的持续部署 (How to setup Continuous Deployment to AWS S3 using CircleCI in just 30 minutes)
Continuous Deployment might seem complicated at first, but don’t be intimidated. In this tutorial, I’ll show you how to implement Continuous Deployment to AWS S3 for a static website using CircleCI in less than 30 minutes.
乍一看,连续部署似乎很复杂,但不要被吓倒。 在本教程中,我将向您展示如何在不到30分钟的时间内使用CircleCI将静态网站实施到AWS S3的持续部署。
You’ll need both an AWS account and a CircleCI account. If you don’t have these yet, start by opening a free account for AWS here and a free CircleCI account here . Both AWS and CircleCI have a free tier that’s more than enough for what you will need for this tutorial.
您将同时需要一个AWS账户和一个CircleCI账户。 如果您还没有这些,请先在此处开设一个免费的AWS帐户,然后在此处开设一个免费的CircleCI帐户。 AWS和CircleCI都有免费层,足以满足本教程所需。
获取代码 (Getting the code)
First you will start by forking and cloning the following project repo on Github: S3ContinuousDeploy or if you prefer you can try this tutorial with one of your own repos as long as it’s a static site.
首先,您将在Github上创建并克隆以下项目存储库: S3ContinuousDeploy,或者,如果您愿意,可以使用自己的存储库尝试本教程,只要它是静态站点即可。
Next you will add the project to your CircleCI account.
接下来,将项目添加到CircleCI帐户。
Next select the S3ContinuousDeploy repo you just cloned and click build project.
接下来,选择刚刚克隆的S3ContinuousDeploy存储库,然后单击build project。
At this point the build will run, but you will be getting an error message warning you that the settings for your project couldn’t be detected. Which is normal since we don’t have a circle.yml configuration file in place, which is what you will be doing next.
此时,构建将运行,但是您将收到一条错误消息,警告您无法检测到项目的设置。 这是正常的,因为我们没有适当的circle.yml配置文件,这是您下一步要做的。
Looking at the docs at CircleCI you can get an idea of what the circle.yml should look like. Unfortunately the circle.yml file example provided will not work as is and will need some tweaking, so let’s do that.
查看CircleCI上的文档,您可以了解circle.yml的外观。 不幸的是,提供的circle.yml文件示例无法按原样工作,并且需要进行一些调整,所以让我们开始吧。
Below is the modified circle.yml file you will be using:
以下是您将使用的修改后的circle.yml文件:
Basically CircleCI creates the build within a Docker container, and the override under dependencies property (line 3) that I added instructs CircleCI to install the AWS command line interface (awscli) that will be used in this case to help manage and facilitate deployment to AWS S3.
基本上,CircleCI在Docker容器内创建构建,我添加的覆盖项依赖项属性(第3行)之下指示CircleCI安装AWS命令行界面(awscli),在这种情况下将用于帮助管理和促进向AWS的部署S3。
So make sure you add the file and commit it to your repo. Finally make sure you push this and other commits you might have made before you proceed to the next step.
因此,请确保您添加了文件并将其提交到您的仓库中。 最后,在继续下一步之前,请确保已推送此提交以及其他可能提交的提交。
As per CircleCI documentation the command for deploying is:
根据CircleCI文档,部署命令为:
The path-to-file was a bit tricky to figure out but by looking at the error logs I was able to finally get it right: home\ubuntu\projectName. So just replace projectName with the name of your project, in my case that will be S3ContinuousDeploy.
文件路径有点棘手,但是通过查看错误日志,我终于可以正确地找到它:home \ ubuntu \ projectName。 因此,只需将projectName替换为您的项目名称,在我的情况下为S3ContinuousDeploy。
The S3://bucket-URL on the other hand is not correct and should be S3://bucket-Name. Right now we don’t have a bucket name, so let’s get ourselves a bucket.
另一方面,S3:// bucket-URL不正确,应为S3:// bucket-Name。 现在我们没有存储桶名称,所以让我们自己获得一个存储桶。
创建S3存储桶 (Creating the S3 bucket)
In this step we will be heading to the AWS Console to create the S3 bucket for this project:
在此步骤中,我们将前往AWS控制台为该项目创建S3存储桶:
Enter the bucket name you would like to use for this project as well as the region. (The best practice is to use the region nearest to your site’s audience.)
输入您要用于此项目的存储区名称以及区域。 (最佳做法是使用距您的网站受众最近的区域。)
You will skip the other steps for now so press “Next” and then press “Create bucket” on the review screen.
您现在将跳过其他步骤,因此在查看屏幕上按“下一步”,然后按“创建存储桶”。
At this point if you go back to CircleCI and try to run the build again CircleCI will return a fatal error: Unable to locate credentials. So why don’t we fix that next.
此时,如果您返回CircleCI并尝试再次运行该构建,CircleCI将返回一个致命错误:无法找到凭据。 那么,为什么我们接下来不解决这个问题呢?
We need to first get the credentials from AWS and then provide them to CircleCI in order to allow the AWS cli to access and manage the S3 bucket. Best practice for this is to create a new Identity and Access Management (IAM) user specifically for CircleCI.
我们需要首先从AWS获取证书,然后将其提供给CircleCI,以允许AWS cli访问和管理S3存储桶。 最佳做法是为CircleCI创建一个新的身份和访问管理(IAM)用户。
On the AWS console go to Security, Identity & Compliance and press IAM and then Add user.
在AWS控制台上,转到安全,身份和合规性,然后按IAM,然后按添加用户。
In the Add User window type in CircleCI for User name, I already have an IAM user named CircleCI setup, So for the purposes of this tutorial and to illustrate these steps I will be using CircleCI2. Make sure you check Programmatic access for Access type.
在CircleCI中为用户名输入Add User窗口中,我已经有一个名为CircleCI setup的IAM用户,因此,出于本教程的目的并说明这些步骤,我将使用CircleCI2。 确保选中“访问程序类型”的“程序访问”。
For permissions choose Attach existing policies directly, and under Policy name check ‘AdministratorAccess’ and then click Create policy. This will provide your IAM user full access to your AWS S3 bucket.
对于权限,选择“直接附加现有策略”,然后在“策略名称”下选中“ AdministratorAccess”,然后单击“创建策略”。 这将为您的IAM用户提供对您的AWS S3存储桶的完全访问权限。
After creating the IAM user, make sure you keep both the Access key ID and the Secret access key, as we will need them in the next step.
创建IAM用户后,请确保同时保留访问密钥ID和秘密访问密钥,因为在下一步中我们将需要它们。
Now back to CircleCI, click on the settings button next to your project name to reveal the project settings menu then click on AWS Permissions. This is where you will paste the ID and Key from the previous step and then click “Save AWS keys.”
现在回到CircleCI,单击项目名称旁边的设置按钮以显示项目设置菜单,然后单击AWS Permissions。 在此处,您将粘贴上一步中的ID和密钥,然后单击“保存AWS密钥”。
Now our CircleCI Container has both the AWS Command Line Interface tool and the credentials to access the AWS S3 bucket. The following steps will show you how to reveal your static site to the world.
现在,我们的CircleCI容器同时具有AWS Command Line Interface工具和访问AWS S3存储桶的凭据。 以下步骤将向您展示如何向世界展示您的静态站点。
In the AWS console go to Storage and then click on S3 and then click on the bucket we created earlier in this tutorial.
在AWS控制台中,转到存储,然后单击S3,然后单击我们在本教程前面创建的存储桶。
You will notice that the code from the repo has already been successfully deployed.
您会注意到,来自存储库的代码已经成功部署。
Now before you can access this static site you need to configure your S3 bucket for website hosting.
现在,在您可以访问此静态站点之前,您需要配置S3存储桶以用于网站托管。
On the same screen click on Properties and then on Static website hosting.
在同一屏幕上,单击“属性”,然后单击“静态网站托管”。
In the following screen select Use this bucket to host a website and make sure you type in index.html for Index document.
在以下屏幕中,选择“使用此存储桶托管网站”,并确保您输入Index.html作为“索引”文档。
By the way, the HTTP address provided above is your access endpoint. But if you try it in the browser unfortunately it won’t work and you will get an access denied error message. But that’s normal you still have one step to do: Set up your bucket policy.
顺便说一下,上面提供的HTTP地址是您的访问端点。 但是,不幸的是,如果您在浏览器中尝试它,它将无法正常工作,并且您将收到拒绝访问的错误消息。 但这是正常的,您仍然有一个步骤要做:设置存储桶策略。
This Bucket policy will allow access to the AWS S3 bucket to anyone via a browser.
此存储桶策略将允许任何人通过浏览器访问AWS S3存储桶。
You can read up here on bucket policies and examples if you want to learn more.
如果您想了解更多信息,可以在这里阅读存储桶策略和示例。
Now you can copy the code snippet above and paste it in your Bucket policy Editor and Voila!
现在,您可以复制上面的代码片段并将其粘贴到存储桶策略编辑器和Voila中!
If you see the screen above, then Congratulations! You have successfully set up Continuous Deployment to an AWS S3 bucket using CircleCI.
如果您看到上面的屏幕,那么恭喜! 您已使用CircleCI成功设置了对AWS S3存储桶的持续部署。
Now every time you push changes to your Github repo, CircleCI will automatically deploy the changes to your AWS S3 bucket.
现在,每次将更改推送到Github存储库时,CircleCI都会自动将更改部署到您的AWS S3存储桶。
You might have noticed that even though the deployment was successful CircleCI shows you a red NO TESTS warning.
您可能已经注意到,即使部署成功,CircleCI也会显示红色的NO TESTS警告。
This is normal because in a Test Driven Development (TDD) environment you would write tests first, and then before going to production your code needs to pass all the tests. An example with tests is beyond the scope of this tutorial, but suffice it to say that had we written tests, CircleCI would only have deployed if all our tests passed.
这是正常现象,因为在测试驱动开发(TDD)环境中,您将首先编写测试,然后在生产之前,您的代码需要通过所有测试。 关于测试的示例不在本教程的讨论范围之内,但是可以说,如果我们编写了测试,那么只有在所有测试通过的情况下,CircleCI才会部署。
Using your own domain name to access this static site is also beyond the scope of this tutorial, but feel free to look here for instructions on how to configure Amazon Route 53 to route Internet traffic to your new site.
使用您自己的域名访问此静态站点也超出了本教程的范围,但是请随时在此处查找有关如何配置Amazon Route 53将Internet流量路由到新站点的说明。
I am hoping to do a tutorial involving a Continuous Integration/Deployment example with a full battery of tests sometimes in the future. Meanwhile, if you have a moment, answer a short survey about this tutorial here, like it on LinkedIn or post a comment in the comments section.
我希望将来能做一个涉及持续集成/部署示例的教程,其中包含全部测试。 同时,如果您有时间,请在此处回答有关本教程的简短调查,在LinkedIn上喜欢它,或在评论部分中发表评论。
Thanks for reading!
谢谢阅读!
k8s aws 部署
更多推荐
所有评论(0)