Implementing CI - CD Aws Code Pipeline
Implementing CI - CD Aws Code Pipeline
Implementing CI - CD Aws Code Pipeline
In the Source provider section, select “GitHub (Version 2)” and then
click “Connect to GitHub” to establish the connection first.
After filling in the connection name and clicking “Connect to GitHub”
you will be redirected to the GitHub authorization page. Follow this
AWS documentation about connecting to GitHub for further
instructions.
Choose the repository and branch you want to use. Then, click “Next.”
In the build provider option, select “AWS CodeBuild,” and then choose
your region. After that, click on “Create project.”
After clicking “Create project,” a pop-up window will appear, prompting
you to create a build project. Provide a name for the build project.
Select the “Managed image” option for the environment image and
follow the instructions in the screenshot below for the remaining settings.
For the Service role, choose “New service role,” and you can either
provide a role name or leave it as the default.
In the build specifications option, select “Use a buildspec file” and
name it “buildspec.yml.”
Click “Continue to CodePipeline”
You will return to the build stage interface. Select the Project name that we
created earlier, then click “Next.”
Choose “AWS CodeDeploy” in the Deploy provider option and select
your region. In the Application name option, you need to create the
Application first.
To create an application, click on “Applications” on the left side under
CodeDeploy.
Click Create application
Enter your application name and for the compute platform choose
EC2/On-premises. Then click Create application
Choose “AWS service” and for the use case, select “CodeDeploy.”
Add the policy named “AWSCodeDeployRole.”
Review the configuration you’ve set up and then click on “Create pipeline.”
Once the pipeline is created, CodePipeline will start running
automatically. Here, we can observe that there is an error in the build
stage.
If you click “View in CodeBuild” and then navigate to the “build logs”
tab, you’ll see an error log indicating that buildspec.yml was not found.
You need to add buildspec.yml to your React project directory.
version: 0.2
phases:
pre_build:
commands:
- npm install
build:
commands:
- npm run build
post_build:
commands:
- echo Build completed on `date`
artifacts:
files:
- public/**/*
- src/**/*
- package.json
- appspec.yml
- scripts/**/*
- build/**/*
git add .
git commit -am "add buildspec.yml"
git push
After pushing the changes, the CodePipeline will start running. However,
in the deployment stage, the process takes a long time, and an error
occurs. This is because we haven’t added appspec.yml to the React
project directory.
Create a file named appspec.yml in the root directory of your React
project.
files:
- source: /
destination: /home/ubuntu/my-app/rp-medium-react
overwrite: true
file_exists_behavior: OVERWRITE
permissions:
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/after_install.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/app_start.sh
timeout: 300
runas: root
#!/bin/bash
cd /home/ubuntu/my-app/rp-medium-react
sudo cp -R build/ /var/www/html/my-react-app/
Create a file named before_install.sh inside the “scripts” folder
#!/bin/bash
cd /home/ubuntu/my-app/rp-medium-react
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
#!/bin/bash
sudo nginx -t && sudo systemctl reload nginx
This picture is my EC2 instance that serve react app, in this case, I create folder structure call
/home/ubuntu/my-app/rp-medium-react
After you’ve finished creating the files mentioned above, commit and push
the changes again.
The error occurs because the CodeDeploy agent is not installed on the
EC2 instance. Follow this link to install the CodeDeploy agent.
Make sure the CodeDeploy agent is installed. Check using the command:
Enter your EC2 Public IP into the browser to check the web page before we
make changes through the pipeline
The web show page “hello world-cicd-2” before we doing perubahan
Edit the code on your local machine to “hello world-cicd-3” and make a
commit & push.
CodePipeline successfully ran the build and deploy for our new code
Now refresh the browser again and the page has changed to “hello world-
cicd-3”
After making code changes on the local machine the web page changes to the words “hello world-cicd-3”