问题
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.
回答1:
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.
回答2:
This is quite a lot of information that you are looking for, but overall the following is what you need:
Setup codedeploy on aws: This would involve configuring the codedeploy application, the right deployment group with the right permissions, and environment variables (EC2 tags, etc) depending on your environment. Also, you would need to install the necessary codedeploy binaries on your EC2 instances.
Setup Bitbucket pipeline: Configure the right bitbucket-pipelines.yml, appspec.py, boto scripts, etc. Take a look at https://bitbucket.org/awslabs/aws-codedeploy-bitbucket-pipelines-python?_ga=2.61586476.1615273408.1509882420-1278037437.1495556069.
I use bitbucket pipelines in the above way to auto-deploy multiple repositories in Angular, Nodejs, Python, on AWS.
A better way to get more help is to ask more specific questions or if you get stuck with a particular issue.
回答3:
You just configure your pipelines YML file to deploy on that branch only. They have a deployment configuration you can add on although it is still in Alpha stage. For current pipelines use you would do something like this:
pipelines:
branches:
master:
- step:
script: # Modify the commands below to build your repository.
- export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
- export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
- export AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION
- export S3_BUCKET=$S3_BUCKET
- export DEPLOYMENT_GROUP_NAME=$DEPLOYMENT_GROUP_NAME
- export DEPLOYMENT_CONFIG=$DEPLOYMENT_CONFIG
- export APPLICATION_NAME=$APPLICATION_NAME
- echo "Starting pipelines for MASTER"
- chmod +x gradlew
- ./gradlew build
- zip -r tmp/enterpriseconfigserver.zip build/libs/enterpriseconfigserver-0.0.1-SNAPSHOT.jar appspec.yml buildspec.yml
- python codedeploy_deploy.py
You'll have to adjust obviously to suit your app but be sure to add your variables to the settings in Pipelines and export them so that the docker image can use them. I suggest creating a custom image that has AWSCLI, BOTO, and all the goodies you need so you do not need to wait for those to install on every build.
This may be useful as well: https://confluence.atlassian.com/bitbucket/using-artifacts-in-steps-935389074.html
来源:https://stackoverflow.com/questions/46935468/auto-deploy-angular-4-app-using-bitbucket-pipeline-and-aws-codedeploy