In this article we will use a simple C# code to present how CICD pipeline build by using AWS CodeBuild
, CodeDeploy
and CodePipeline
. Fortunately, in last article, we have already build the CI pipeline using CodeBuild
with other services, therefore, now we can use the pipeline and continue build the left part.
0. Simple Code Prepare (DONE)
1. Build Stage (CHANGE)
If you remember what we have done in last article, we use the AWS CodeBuild
panel and select the output
destination as S3
bucket and the type is zip
file, this is one way to define a build procedure, we can also define it inside the buildspec.yml
.
1.1 change the artifacts
in buildspec.yml
file
Here we need to notice that, because the CodeDeploy
step, we need the appspec.yml
file and also some scripts, so at artifacts, we need to not only include the output/
directry, but also with these necessary files.
artifacts:
files:
- appspec.yml
- scripts/*
- output/**/*
discard-paths: no # Keep folder structure
1.2 appspec.yml
file
appsepc.yml
is used to tell codedeploy-agent
what to do with the files, later we will see how to install codedeploy-agent
on the EC2 instance.
In the GitHub repo, add appspec.yml
for CodeDeploy
:
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/DemoConsoleApp
permissions:
- object: /home/ubuntu/DemoConsoleApp/scripts
pattern: "*.sh"
owner: ubuntu
mode: 755
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300
runas: ubuntu
ApplicationStart:
- location: scripts/start.sh
timeout: 300
runas: ubuntu
Here mainly we do 3 things:
- Deploy/copy all files to
/home/ubuntu/DemoConsoleApp
on EC2. - Sets correct permissions (chmod +x) for the
*.sh
file. - Runs
start.sh
automatically after deployment.
Notice, here we don't need to manually copy the
appspec.yml
or scripts to the folder where we need (eg./home/ubuntu/DemoConsoleApp/
). This is whatcodedeploy-agent
have done according toappspec.yml
file.
1.3 Create start.sh
for Running the App
Inside our GitHub repo, we also need to add a scripts/start.sh
file:
#!/bin/bash
cd /home/ubuntu/DemoConsoleApp/output
nohup dotnet DemoConsoleApp.dll > app.log 2>&1 &
echo "App started successfully!"
and also a before_install.sh
script to garantee the folder exists.
#!/bin/bash
set -e
TARGET_DIR="/home/ubuntu/DemoConsoleApp"
echo "Checking and creating directory: $TARGET_DIR"
if [ ! -d "$TARGET_DIR" ]; then
mkdir -p "$TARGET_DIR"
echo "Directory created successfully."
elif [ -d "$TARGET_DIR" ]; then
echo "Directory already exists."
else
echo "Error: $TARGET_DIR exists but is not a directory."
exit 1
fi
2. Deploy Stage
2.1 Install codedeploy-agent
on EC2
instance
login to EC2
instance and install codedeploy-agent
on the instance.
# ubuntu
sudo apt update -y
sudo apt install ruby-full -y
sudo apt install wget -y
cd /home/ubuntu
wget https://aws-codedeploy-eu-central-1.s3.eu-central-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo systemctl start codedeploy-agent
sudo systemctl enable codedeploy-agent
sudo systemctl status codedeploy-agent
here as I used ubuntu instance, I used above commands. If you use Redhat-like instance, you can use yum
cmd, more info about codedeploy-agent
pls check here).
Now codedeploy-agent
is running
2.2 Create role for EC2
(if not) and CodeDeploy
Attach the AmazonEC2RoleforAWSCodeDeploy
and AWSCodeDeployFullAccess
policy to the EC2 instance role.
Also for CodeDeploy
, we need another role, with policy AWSCodeDeployRole
.
2.3 Create an Application in CodeDeploy
2.3.1 Create an Application
- Application Name:
DemoConsoleAppDeploy
2.3.2 Create the Deployment Group
- Deployment Group Name:
DemoConsoleApp-Group
- Environment configuration:
- Amazon EC2 instances
- Service Role: Attach the role we created above
- Deployment Type: In-Place
- In-Place will stop the app on EC2, updates it, and restarts it.
- Blue/Green will create new instances and routes traffic to them.
- Deployment Strategy: All at Once
- One at a Time: there will be no downtime, but it will be slow
- Half at a Time
- All at Once: Fastest, but downtime occurs, as here, this is not so critical as a demo example.
- Load Balancer: Disabled
2.3.3 Create the Deployment
- Revision:
- A revision is the application package that CodeDeploy fetches and deploys to our EC2 instance. Here we can use the previous s3 bucket.
3. CodePipeline
As we already have setup the source, and connected source provider Github, and also setup the CodeBuild
project, then as above show, we have setup the CodeDeploy
application. Therefore in the CodePipeline
, we just need to choose each stages provider and choose corresponding items.
- Pipeline Name: demo-consoleapp-pipeline
- Source Provider: GitHub
- Build Provider: CodeBuild
- Deploy Provider: CodeDeploy
Then we can start the pipeline. And after some time, we can see the deploy is successful finally.
4. Error Analysis
4.1 Build Stage Errors
For Build Stage, the error we can directly checked in the panel, "Build logs" section.
4.2 Deploy Stage Errors
Here as we deployed on EC2 instance, so we can check the corresponding logs on the instance. The default codedeploy-agent
logs stored at /var/log/aws/codedeploy-agent/
dir.
tail -100 /var/log/aws/codedeploy-agent/codedeploy-agent.log
And the standard output/error log we can also check here:
cat /opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log
And we can also find the file stored place where it copyed from s3 bucket /opt/codedeploy-agent/deployment-root
.
find /opt/codedeploy-agent/deployment-root -maxdepth 3 -type d -name deployment-archive
Summary
Here the whole workflow is like:
Top comments (0)