使用Bitbucket Pipeline和AWS CodeDeploy自动部署Angular 4 App [英] Auto Deploy Angular 4 App using Bitbucket Pipeline and AWS CodeDeploy

查看:119
本文介绍了使用Bitbucket Pipeline和AWS CodeDeploy自动部署Angular 4 App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Angular 4 应用程序,该应用程序具有 MongoDB NodeJS 。我一直在研究在Amazon AWS EC2 实例上自动部署应用程序。



我正在使用 BitBucket 回购为我的项目。阅读完文章后,我遇到了 Bitbucket Pipeline AWS CodeDeploy 。为了完成它,有许多方案和配置需要完成。一整天看完所有文档后,我真的很困惑。



我想在将代码推送到特定分支时自动部署我的应用程序。



有人可以提供逐步指南,以使用Bitbucket Pipeline和AWS CodeDeploy自动部署Angular 4(MEAN)应用程序吗?



请让我知道您是否需要其他信息。谢谢。

解决方案

如何使用Bitbucket Pipelines和AWS CodeDeploy自动部署?



有关如何自动部署应用的分步指南:



先决条件:
-BitBucket存储库
-AWS账户
-腻子



步骤1。创建新的IAM用户



登录到您的AWS账户。访问

附加现有策略-搜索 codedeploy并选择AmazonEC2RoleforAWSCodeDeploy,AWSCodeDeployDeployerAccess,AWSCodeDeployFullAccess,AWSCodeDeployRole










下一步:查看>创建用户-重要说明:
1。下载CSV
2。注意向下访问键ID
3。注意Down Secret访问密钥










STEP2。为CodeDeploy应用程序创建角色



它是代码部署的服务角色。您分配给将在后续步骤中创建的代码部署应用程序的服务角色。



登录到您的AWS账户。访问

在下面选择CodeDeploy>










下一步:权限>下一步查看>输入角色名称(例如CodeDeployServiceRole)>输入描述(非强制性)>创建角色。



步骤3。为EC2实例创建角色



这是分配给EC2实例的角色,您将在以后创建步骤。



登录到您的AWS账户。访问

验证策略>创建策略。



步骤B:创建角色
角色>创建角色> AWS服务> EC2>选择用例>单击EC2>下一步:权限>搜索r ec2并选择您在步骤A中创建的 CodeDeploy-EC2-Permissions>










下一步:查看>名称: CodeDeploy-EC2-Instance -Profile>角色描述: CodeDeploy-EC2-Instance-Profile>创建角色



STEP 4.创建EC2实例。



登录到您的AWS账户。
服务> EC2>启动实例> Amazon Linux AMI(或根据需要选择)>选择实例类型>下一步:配置实例详细信息> IAM角色>从下拉列表中选择您在步骤3步骤中创建的角色B(CodeDeploy-EC2-Instance-Profile)>








< h2>

下一步:添加存储>下一步:添加标签>添加标签>(重要步骤! ;您分配的键)输入键:名称,值:staging-auto-deploy(或您喜欢的任何内容)>










下一步:配置安全组>选择现有安全性分组或创建新>查看并启动>启动>选择ex浪费密钥对或创建新的>启动实例>查看实例>记下IPv4公共IP



STEP5。在EC2实例上安装CodeDeploy代理



在本地计算机上打开Putty>输入在步骤4>端口22>在连接中> SSH> Auth>用于身份验证的私钥文件中获得的公共IP>浏览>链接您的密钥对文件步骤4中的实例>打开>登录为:输入用户名


现在根据您的实例类型安装CodeDeploy代理



Linux服务器:

步骤6.在BitBucket上添加CodeDeploy插件



登录到您的BitBucket帐户
集成>搜索AWS CodeDeploy>添加AWS CodeDeploy










STEP 7.在AWS上创建CodeDeploy应用程序



登录到您的AWS账户。
服务>搜索CodeDeploy>选择CodeDeploy>如果您是第一次,请选择立即开始或创建应用程序>自定义部署>跳过演练>输入详细信息>
应用程序名称:staging-deployment(重要:
部署组名称:staging-deployment(重要:记下)
选择就地部署
环境配置> Amazon EC2 istance>密钥:名称,值:分段自动部署(这些是在步骤4中创建实例时创建的键,值)>










部署配置> CodeDeployDefault.OneAtTime>服务角色ARN:选择您在第2步中创建的角色(CodeDeployServiceRole)>










创建应用程序



STEP 8.创建S3卷



登录到您的AWS账户>服务> S3>创建存储桶>存储桶名称:staging-deployment-bucket>创建



STEP9。存储库的CodeDeploy设置



登录到您的仓库>设置> CodeDeploy设置>配置加载项>按照屏幕上的说明



步骤A >
登录到您的AWS账户>服务> IAM>策略>创建策略>创建自己的策略>(此策略是为bitbucket代码部署附加组件创建的,使用此策略为bitbucket代码部署附加组件创建角色)
策略名称:BitBucketCodeDeployAddOnPolicy
策略文档:将以下内容粘贴到输入框中


{版本: 2012–10–17 ,声明:[{效果:允许,动作:[ s3:ListAllMyBuckets, s3:P utObject],资源: arn:aws:s3 ::: },{效果:允许,操作:[代码部署:],资源 : *}]}


创建策略



步骤B
登录到您的AWS账户>服务> IAM>角色>创建角色>另一个AWS账户>










帐户ID:复制粘贴在bitbucket代码上给出的AWS账户ID在屏幕上部署指令>选中需要外部ID复选框,然后:复制粘贴在bitbucket代码上给出的外部ID在屏幕指令上部署>














下一个:权限>附加权限策略>搜索在步骤A中创建的策略>下一步:查看
角色名称: BitbucketCodeDeployAddon>创建角色
,单击刚创建的角色,然后复制角色ARN,然后将其粘贴到Bitbucket代码部署设置页面上的您的角色ARN中,然后单击保存并添加。继续
在下一页应用程序:选择在步骤7中创建的CodeDeploy应用程序> S3存储桶:选择在步骤8中创建的S3存储桶>保存



STEP 10.启用Bitbucket管道



登录到您的BitBucket帐户回购设置>管道>设置>启用启用管道



对于步骤11、12、13和14,所需文件可在此仓库的源中进行参考:

您可以检查管道的进度








< a href = https://i.stack.imgur.com/0qptF.png rel = noreferrer>



您可以查看与部署相关的日志。有关更多信息,请访问: http://docs.aws。 amazon.com/codedeploy/latest/userguide/deployments-view-logs.html



您可以从AWS监控部署,登录到您的AWS账户,服务> CodeDeploy>部署。


I have an Angular 4 app with MongoDB and NodeJS. I have been looking into auto deployment of my app on amazon AWS EC2 instance.

I am using BitBucket repo for my project. After reading through articles I have come across Bitbucket Pipeline and AWS CodeDeploy. There are many scenarios and configurations to be done in order to get it done. I am really confused after reading all the documentations all day.

I want to auto deploy my app when code is pushed to specific branch.

Can someone please provide step by step guidelines to auto deploy an Angular 4 (MEAN) app using Bitbucket Pipeline and AWS CodeDeploy?

Please let me know if you want any additional info. Thank You.

解决方案

How to auto deploy with Bitbucket Pipelines and AWS CodeDeploy?

Step by step guide on how to auto deploy your app: 

Prerequisites:  - BitBucket Repository - AWS Account - Putty

STEP 1. Create New IAM User

Login to your AWS account. Visit https://console.aws.amazon.com/iam/

Users > Add User > enter username > check box both Access type > choose "Custom Password" radio > Uncheck "Require password reset" > Click Next: Permissions > Attach existing policies directly > search for "s3" and select AmazonS3FullAccess


Attach Existing Policies- search for "codedeploy" and select AmazonEC2RoleforAWSCodeDeploy, AWSCodeDeployDeployerAccess, AWSCodeDeployFullAccess, AWSCodeDeployRole


Next: Review > Create user-IMPORTANT NOTE: 1. Download CSV 2. Note Down Access Key ID 3. Note Down Secret access key


STEP 2. Create Role for CodeDeploy Application

Its is service role for Code Deploy. This service role you assign to the code deploy application that you will create in the later steps.

Login to your AWS account. Visit https://console.aws.amazon.com/iam/

Roles > Create Role > click AWS service >


Below Select CodeDeploy >


Next: permissions > Next Review > enter Role name (e.g CodeDeployServiceRole) > enter description (not compulsory) > Create role.

STEP 3. Create Role for EC2 instance

This is the role which is assigned to EC2 instance which you will create later steps.

Login to your AWS account. Visit https://console.aws.amazon.com/iam/

Step A. Create Policy for this Role. Policies > Create Policy > Select Create Your Own Policy > Policy Name: "CodeDeploy-EC2-Permissions" > Description: "policy for role which is assigned to EC2 instance" > Policy Document: Paste the following in the input box.

{"Version": "2012–10–17","Statement": [{"Action": ["s3:Get*","s3:List*"],"Effect": "Allow","Resource": "*"}]}


Validate Policy > Create Policy.

Step B: Create Role Roles > Create Role > AWS service > EC2 > Select your use case > Click EC2 > Next: Permissions > search for "ec2" and select the "CodeDeploy-EC2-Permissions" which you created in Step A >


Next: Review > Name: "CodeDeploy-EC2-Instance-Profile" > Role Description: "CodeDeploy-EC2-Instance-Profile" > Create Role

STEP 4. Create EC2 Instance.

Login to your AWS Account. Services > EC2 > Launch Instance > Amazon Linux AMI (or choose as per your need) > Choose an instance type > Next: Configure Instance Details > IAM role > from drop down select the role that you created in Step 3 Step B (CodeDeploy-EC2-Instance-Profile) >


Next: Add Storage > Next: Add Tags > Add Tag > (Important step!! note down name & key you assign) Input Key: Name, Value: staging-auto-deploy (or anything you prefer) >


Next: Configure Security Group > select existing security group or create new > Review and Launch > Launch > Select existing key pair or create new > Launch Instances > View Instances > Note down IPv4 Public IP

STEP 5. Install CodeDeploy Agent on EC2 instance

Open Putty on your local machine > Enter the Public IP that you got in Step 4 > Port 22 > In Connection > SSH > Auth > Private key file for authentication> browse> link the key pair file for your instance in step 4 > open > login as: enter your username

Now install CodeDeploy agent as per your instance type

Linux Server: http://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-linux.html

Ubuntu Server: http://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html

Windows Server: http://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-windows.html

Verify that agent is running.


STEP 6. Add CodeDeploy Addon on BitBucket

Login to your BitBucket Account Integrations > Search AWS CodeDeploy > Add AWS CodeDeploy


STEP 7. Create CodeDeploy Application on AWS

Login to your AWS Account. Services > search CodeDeploy > select CodeDeploy > If this is your first time select "get started now" or Create Application > Custom deployment > skip walkthrough > enter details > Application Name: staging-deployment (Important: note it down) Deployment group name: staging-deployment (Important: note it down) Select "In-place deployment" Environment configuration > Amazon EC2 isntance > Key: Name, Value: staging-auto-deploy (these are the key, value which you created when you created instance in Step 4) >


Deployment Configuration > CodeDeployDefault.OneAtTime > Service Role ARN: select the role that you created in step 2 (CodeDeployServiceRole) >


Create Application

STEP 8. Create S3 volume

Login to your AWS account > Services > S3 > Create bucket > Bucket name: staging-deployment-bucket > Create

STEP 9. CodeDeploy settings for Repository

Login to your repo > Settings > CodeDeploy Settings > Configure add-on > Follow the on screen instructions

Step A Login to your AWS account > Services > IAM > Policy > Create Policy > Create Your Own Policy > (This policy is created for bitbucket code deploy add on, use this to create role for bitbucket codeDeploy addon) Policy Name: BitBucketCodeDeployAddOnPolicy Policy Document: paste following into input box

{"Version": "2012–10–17","Statement": [{"Effect": "Allow","Action": ["s3:ListAllMyBuckets","s3:PutObject"],"Resource": "arn:aws:s3:::"},{"Effect": "Allow","Action": ["codedeploy:"],"Resource": "*"}]}

Create Policy

Step B Login to your AWS account > Services > IAM > Roles > create role > another AWS account >


account ID: copy paste the AWS Account ID given on the bitbucket codeDeploy on screen instruction > check require external ID checkbox then: copy paste the External ID given on the bitbucket codeDeploy on screen instruction >


next: Permissions > Attach permissions policies > search for policy that you created in step A > next: review Roll name: "BitbucketCodeDeployAddon" > Create role click on the role you just created and copy Role ARN and paste it into "Your Role ARN" on bitbucket code deploy settings page > click save & continue On next page Application: select CodeDeploy Application that you created in step 7 > S3 Bucket: select S3 bucket that you created in step 8 > save

STEP 10. Enable Bitbucket pipeline

Login to your BitBucket account Repo Settings > Pipelines > settings > Turn on Enable pipeline

For Steps 11, 12, 13 & 14, required files are available for reference at Source of this repo: https://bitbucket.org/bhushanTPL/bitbucket-pipeline-and-aws-codedeploy

STEP 11. Create bitbucket-pipelines.yml

Copy bitbucket-pipelines.yml file.(make changes to this file as per your project requirement) Add this file to root of your project.

Docs: https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html?_ga=2.162970750.315484667.1509451697-1615374000.1508921669#Configurebitbucket-pipelines.yml-ci_imageimage(optional)

STEP 12. Create codedeploy_deploy.py

Copy codedeploy_deploy.py file. Add this file to root of you your project.

Docs & Source: https://bitbucket.org/awslabs/aws-codedeploy-bitbucket-pipelines-python

STEP 13. Create appspec.yml 

Go to Source of this repo: https://bitbucket.org/bhushanTPL/bitbucket-pipeline-and-aws-codedeploy and copy appspec.yml file. (make changes to this file as per your project requirement) Add it to root of your project.

Docs: http://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html

STEP 14. Create scripts

Create scripts folder at root of your project 

Create startApp.sh file in the scripts folder in root of your projects, which will run commands after your build is transferred to your EC2 instance.

STEP 15. Add Environment variables

Login to your BitBucket account > your Repo Settings > Environment Variables Add the following environment variables

AWS_SECRET_ACCESS_KEY: Secret key for a user with the required permissions.

AWS_ACCESS_KEY_ID: Access key for a user with the required permissions.

AWS_DEFAULT_REGION: Region where the target AWS CodeDeploy application is.

APPLICATION_NAME: Name of AWS CodeDeploy application.

DEPLOYMENT_CONFIG: AWS CodeDeploy Deployment Configuration (CodeDeployDefault.OneAtATime|CodeDeployDefault.AllAtOnce|CodeDeployDefault.HalfAtATime|Custom).

DEPLOYMENT_GROUP_NAME: Name of the Deployment group in the application.

S3_BUCKET: Name of the S3 Bucket where source code to be deployed is stored.

Docs & Ref: https://bitbucket.org/awslabs/aws-codedeploy-bitbucket-pipelines-python


You have now configured all the required steps. Now when you commit and push your changes to your branch the auto deployment process starts. Note that in the following bitbucket-pipeline.yml configuration deployment process will start whenever you push your changes to "staging" branch.


You can check progress of your pipeline by clicking on Pipelines in your repo.


You can view deployment related logs. For more info visit : http://docs.aws.amazon.com/codedeploy/latest/userguide/deployments-view-logs.html

You can monitor deployments from AWS, Login to your AWS account, Services > CodeDeploy > Deployments.

这篇关于使用Bitbucket Pipeline和AWS CodeDeploy自动部署Angular 4 App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆