Skip to content

Commit 9aaa859

Browse files
author
100daysofdevops
committed
adding code to create cd cd pipeline for EKS using CodeCommit, CodeBuild and CodePipeline
1 parent 0d38984 commit 9aaa859

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

Diff for: eks-codepipeline/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM public.ecr.aws/nginx/nginx:latest
2+
COPY app /usr/share/nginx/html/app

Diff for: eks-codepipeline/app/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<h1>Welcome to Pipeline for EKS using CodeCommit, CodeBuild and CodePipeline </h1>
4+
<h3> This is demo pipeline for EKS - v1</h3>
5+
</body>
6+
</html>

Diff for: eks-codepipeline/buildspec.yml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
version: 0.2
2+
phases:
3+
install:
4+
commands:
5+
- echo "Install Phase - if you need additional package, add it in this stage"
6+
pre_build:
7+
commands:
8+
# This Docker Image tag will have date, time and Codecommit version
9+
- TAG="$(date +%Y-%m-%d.%H.%M.%S).$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)"
10+
# Updating Docker Image tag in your Kubernetes Deployment Manifest
11+
- echo "Update Image tag in kubernetes manifest"
12+
- sed -i 's@CONTAINER_IMAGE@'"$REPOSITORY_URL:$TAG"'@' manifests/deployment.yaml
13+
# Check AWS CLI Version
14+
- echo "Checking AWS CLI Version..."
15+
- aws --version
16+
# Login to ECR Registry
17+
- echo "Login in to Amazon ECR Registry"
18+
- $(aws ecr get-login --no-include-email)
19+
# Update Kube config Home Directory
20+
- export KUBECONFIG=$HOME/.kube/config
21+
build:
22+
commands:
23+
# Building Docker Image
24+
- echo "Docker build started on `date`"
25+
- echo "Building the Docker image..."
26+
- docker build --tag $REPOSITORY_URL:$TAG .
27+
post_build:
28+
commands:
29+
# Push Docker Image to ECR Repository
30+
- echo "Docker build completed on `date`"
31+
- echo "Pushing the Docker image to ECR Repository"
32+
- docker push $REPOSITORY_URL:$TAG
33+
- echo "Docker Push to ECR Repository Completed - $REPOSITORY_URL:$TAG"
34+
# Get AWS Credential using STS Assume Role for kubectl
35+
- echo "Setting Environment Variables related to AWS CLI for Kube Config Setup"
36+
- CREDENTIALS=$(aws sts assume-role --role-arn $EKS_ROLE_ARN --role-session-name eks-codebuild --duration-seconds 900)
37+
- export AWS_ACCESS_KEY_ID="$(echo ${CREDENTIALS} | jq -r '.Credentials.AccessKeyId')"
38+
- export AWS_SECRET_ACCESS_KEY="$(echo ${CREDENTIALS} | jq -r '.Credentials.SecretAccessKey')"
39+
- export AWS_SESSION_TOKEN="$(echo ${CREDENTIALS} | jq -r '.Credentials.SessionToken')"
40+
- export AWS_EXPIRATION=$(echo ${CREDENTIALS} | jq -r '.Credentials.Expiration')
41+
# Updating kubectl with your EKS Cluster
42+
- echo "Update Kube Config configuration"
43+
- aws eks update-kubeconfig --name $EKS_CLUSTERNAME
44+
# Show time, applying manifests changes using kubectl
45+
- echo "Apply changes to kube manifests"
46+
- kubectl apply -f manifests/
47+
- echo "All done!!!! Kubernetes changes applied"
48+
# Create Artifacts which we can use if we want to continue our pipeline for other stages
49+
- printf '[{"name":"deployment.yaml","imageUri":"%s"}]' $REPOSITORY_URL:$TAG > build.json
50+
artifacts:
51+
files:
52+
- build.json
53+
- manifests/*

Diff for: eks-codepipeline/manifests/deployment.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: my-eks-pipeline-deployment
5+
labels:
6+
app: my-eks-pipeline-deployment
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: my-eks-pipeline-deployment
12+
template:
13+
metadata:
14+
labels:
15+
app: my-eks-pipeline-deployment
16+
spec:
17+
containers:
18+
- name: my-eks-pipeline-deployment
19+
image: CONTAINER_IMAGE
20+
ports:
21+
- containerPort: 80

0 commit comments

Comments
 (0)