
Posted on • Edited on
Simple Demo for AWS CICD Pipeline
In this article we will use a simple C# code to present how CICD pipeline build by using AWSCodeBuild
,CodeDeploy
andCodePipeline
. Fortunately, inlast article, we have already build the CI pipeline usingCodeBuild
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 AWSCodeBuild
panel and select theoutput
destination asS3
bucket and the type iszip
file, this is one way to define a build procedure, we can also define it inside thebuildspec.yml
.
1.1 change theartifacts
inbuildspec.yml
file
Here we need to notice that, because theCodeDeploy
step, we need theappspec.yml
file and also some scripts, so at artifacts, we need to not only include theoutput/
directry, but also with these necessary files.
artifacts:files:-appspec.yml-scripts/*-output/**/*discard-paths:no# Keep folder structure
1.2appspec.yml
file
appsepc.yml
is used to tellcodedeploy-agent
what to do with the files, later we will see how to installcodedeploy-agent
on the EC2 instance.
In the GitHub repo, addappspec.yml
forCodeDeploy
:
version:0.0os:linuxfiles:-source:/destination:/home/ubuntu/DemoConsoleApppermissions:-object:/home/ubuntu/DemoConsoleApp/scriptspattern:"*.sh"owner:ubuntumode:755hooks:BeforeInstall:-location:scripts/before_install.shtimeout:300runas:ubuntuApplicationStart:-location:scripts/start.shtimeout:300runas: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 Createstart.sh
for Running the App
Inside our GitHub repo, we also need to add ascripts/start.sh
file:
#!/bin/bashcd /home/ubuntu/DemoConsoleApp/outputnohupdotnet DemoConsoleApp.dll> app.log 2>&1 &echo"App started successfully!"
and also abefore_install.sh
script to garantee the folder exists.
#!/bin/bashset-eTARGET_DIR="/home/ubuntu/DemoConsoleApp"echo"Checking and creating directory:$TARGET_DIR"if[!-d"$TARGET_DIR"];thenmkdir-p"$TARGET_DIR"echo"Directory created successfully."elif[-d"$TARGET_DIR"];thenecho"Directory already exists."elseecho"Error:$TARGET_DIR exists but is not a directory."exit1fi
2. Deploy Stage
2.1 Installcodedeploy-agent
onEC2
instance
login toEC2
instance and installcodedeploy-agent
on the instance.
# ubuntusudoapt update-ysudoaptinstallruby-full-ysudoaptinstallwget-ycd /home/ubuntuwget https://aws-codedeploy-eu-central-1.s3.eu-central-1.amazonaws.com/latest/installchmod +x ./installsudo ./install autosudosystemctl start codedeploy-agentsudosystemctlenablecodedeploy-agentsudosystemctl status codedeploy-agent
here as I used ubuntu instance, I used above commands. If you use Redhat-like instance, you can useyum
cmd, more info aboutcodedeploy-agent
pls checkhere).
Nowcodedeploy-agent
is running
2.2 Create role forEC2
(if not) andCodeDeploy
Attach theAmazonEC2RoleforAWSCodeDeploy
andAWSCodeDeployFullAccess
policy to the EC2 instance role.
Also forCodeDeploy
, we need another role, with policyAWSCodeDeployRole
.
2.3 Create an Application inCodeDeploy
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 theCodeBuild
project, then as above show, we have setup theCodeDeploy
application. Therefore in theCodePipeline
, 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 defaultcodedeploy-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)
For further actions, you may consider blocking this person and/orreporting abuse