Integrate and deploy code from GitHub to aws ec2 - elastic beanstalk

This is a continuous of CI CD process . 

Visit and configure all parts before doing this.

Part One  |  Part Two  |  Part Three





Navigate to the Jenkins and click on New Item to set up a new project.



There are many ways to setup a project, here will do Freestyle project. Enter the name of the item.









Next will setup a elastic beanstalk app on AWS.



Fill the basic information for the application, and click on create application. This will take some time to setup a elastic beanstalk application and assign all the provisions. This may take a while.







Next, navigate to github and select the project repository, which want to integrate and deploy using CI CD.
Here using a Node Js application. And this is a git url https://github.com/avinashkumar-github/elasticbeanstalk-nodejs-sample-app.git

Copy the URL and navigate to the Jenkins page.


















Navigate to the General tab, in Jenkins and select the Github project, then paste the URL of the github repository.





Navigate to the Source Code Management section and paste the same repository URL



On build trigger, select the Github hook trigger for GitScm polling option. And click save.



Now navigate to the github repository, and click on setting > Webhooks section.
Add a new webhooks

In the payload URL : <Jenkins URL>/github-webhooks/
Make sure to use the trailing slash


Now, navigate back to the Jenkins. and shoose the execute shell, under the Build section. And add the below code. This will be vary based on the region and platform you are using.
All these information, you can collect from the AWS console.

#!/bin/bash -xe

APPLICATION=YOUR_APPLICATION_NAME_HERE
REGION=YOUR_REGION_NAME_HERE
ENVIRONMENT=YOUR_ENVIRONMENT_NAME_HERE
PLATFORM=YOUR_PLATFORM_HERE

# confirm the role being used
aws sts get-caller-identity

# intialize the application
eb init "${APPLICATION}" --platform "${PLATFORM}"  --region "${REGION}"

# select the environment
eb use "${ENVIRONMENT}"

# deploy the application
eb deploy

# get the deployment's health and status information
eb health
eb status

 



All is set now, make any changes now in your project repository in github, commit and push the code . Automatically a build gets triggered in Jenkins using github webhook. and the build pull and integrate the code from github, and deploy it to the elastic beanstalk application.




This way a CI CD process can be set up .

To get the URL of beanstalk application, navigate to the EC2 dashboard or beanstalk dashboard, a public URL will be there to access.