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