forked from rnzsgh/eks-workshop-sample-api-service-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildspec.yml
51 lines (51 loc) · 2.69 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
---
version: 0.2
phases:
install:
commands:
- curl -sS -o aws-iam-authenticator https://amazon-eks.s3-us-west-2.amazonaws.com/1.10.3/2018-07-26/bin/linux/amd64/aws-iam-authenticator
- curl -sS -o kubectl https://amazon-eks.s3-us-west-2.amazonaws.com/1.14.6/2019-08-22/bin/linux/amd64/kubectl
- curl -sS https://raw.githubusercontent.com/rancher/k3d/master/install.sh | TAG=v1.7.0 bash
- chmod +x ./kubectl ./aws-iam-authenticator
- export PATH=$PWD/:$PATH
- apt-get update && apt-get -y install jq python3-pip python3-dev && pip3 install --upgrade awscli
pre_build:
commands:
- TAG="$REPOSITORY_NAME.$REPOSITORY_BRANCH.$ENVIRONMENT_NAME.$(date +%Y-%m-%d.%H.%M.%S).$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)"
- sed -i 's@CONTAINER_IMAGE@'"$REPOSITORY_URI:$TAG"'@' hello-k8s.yml
- $(aws ecr get-login --no-include-email)
build:
commands:
- docker build --tag $REPOSITORY_URI:$TAG .
- docker push $REPOSITORY_URI:$TAG
# Creating k3d cluster
- k3d create
# Waiting for cluster creation for 20 seconds
- sleep 20
# Configuring kubectl for k3d cluster
- export KUBECONFIG="$(k3d get-kubeconfig --name='k3s-default')"
# Creating secret as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#add-image-pull-secret-to-service-account
# to enable k3d cluster pull images from ECR
- ./create_secret.sh
# Applying our service and deployment manifest
- kubectl apply -f hello-k8s.yml
# Waiting for pods and service to come up
- sleep 20
# Running unit test
- ./unit_test.sh
post_build:
commands:
# Checking if build phase including unit test completed successfully, if not we don't proceed with deployment
- bash -c "if [ /"$CODEBUILD_BUILD_SUCCEEDING/" == /"0/" ]; then exit 1; fi"
- echo Build stage successfully completed on `date`
- CREDENTIALS=$(aws sts assume-role --role-arn $EKS_KUBECTL_ROLE_ARN --role-session-name codebuild-kubectl --duration-seconds 900)
- export KUBECONFIG=$HOME/.kube/config
- 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')
- aws eks update-kubeconfig --name $EKS_CLUSTER_NAME
- kubectl apply -f hello-k8s.yml
- printf '[{"name":"hello-k8s","imageUri":"%s"}]' $REPOSITORY_URI:$TAG > build.json
artifacts:
files: build.json