Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,62 @@
version: 0.2
run-as: root

phases:

install:
commands:
- echo Setting up required prerequisites
- pip install --upgrade awscli
- curl --silent --location -o /usr/local/bin/kubectl "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
- chmod +x /usr/local/bin/kubectl
- curl --silent --location -o /usr/local/bin/aws-iam-authenticator "https://amazon-eks.s3-us-west-2.amazonaws.com/1.14.6/2019-08-22/bin/linux/amd64/aws-iam-authenticator"
- chmod +x /usr/local/bin/aws-iam-authenticator
- curl --silent --location "https://github.com/weaveworks/eksctl/releases/download/v0.72.0/eksctl_Linux_amd64.tar.gz" | tar xz -C /tmp
- mv -v /tmp/eksctl /usr/local/bin
- chmod +x /usr/local/bin/eksctl
- AWS_ACCOUNT_ID=684910554524
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way to not hardcode the account id?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we will move these files to Cloud Formation and the account id will be dynamic then.

- IMAGE_TAG=latest

pre_build:
commands:
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/webaverse
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH:=latest}
- echo Logging in to Amazon EKS...
- ls -l
- chmod a+x ./eks/cluster-setup.sh
- sh ./eks/cluster-setup.sh
- aws eks --region $AWS_DEFAULT_REGION update-kubeconfig --name preview-server
- echo check config
- kubectl config view --minify
- echo check kubectl access
- kubectl get svc
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/ecrrepository-app

build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- echo Logging in to Docker Hub...
- echo "${DOCKERHUB_PASSWORD}" | docker login -u "${DOCKERHUB_USERNAME}" --password-stdin
- docker build -t $REPOSITORY_URI:latest .
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
- echo Building the Docker image...
# - docker build --cache-from $REPOSITORY_URI:$IMAGE_TAG -t ecrrepository-app:$IMAGE_TAG .
# - docker tag ecrrepository-app:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/ecrrepository-app:$IMAGE_TAG

post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- echo Logging in to Amazon ECR...
- aws --version
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"webaverse-preview-backend","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files: imagedefinitions.json
- echo Pushing the Docker image...
# - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/ecrrepository-app:$IMAGE_TAG
- echo Push the latest image to cluster
- kubectl apply -f ./eks/deployment.yaml
- kubectl rollout -h
- kubectl rollout restart -f ./eks/deployment.yaml
- kubectl apply -f ./eks/service.yaml
- kubectl get svc preview-server
- export loadbalancer=$(kubectl get svc preview-server -o jsonpath='{.status.loadBalancer.ingress[*].hostname}')
- kubectl get pods -o wide
- while [[ $(kubectl get pods -l app=preview-server-app -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do echo "waiting for pod" && sleep 1; done
- kubectl get pods -o wide
- kubectl get endpoints
- kubectl get deployments
- kubectl logs -l app=preview-server-app
- echo http://${loadbalancer}
- curl -k http://${loadbalancer}

47 changes: 47 additions & 0 deletions buildspec_eks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: 0.2
run-as: root

phases:

install:
commands:
- echo Installing app dependencies...
- curl -o kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.18.9/2020-11-02/bin/linux/amd64/kubectl
- chmod +x ./kubectl
- mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$PATH:$HOME/bin
- echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc
- source ~/.bashrc
- echo 'Check kubectl version'
- kubectl version --short --client
- chmod +x eks_cicd/prereqs.sh
- sh eks_cicd/prereqs.sh

pre_build:
commands:
- echo Logging in to Amazon EKS...
- aws eks --region $AWS_DEFAULT_REGION update-kubeconfig --name $AWS_CLUSTER_NAME
- echo check config
- kubectl config view --minify
- echo check kubectl access
- kubectl get svc
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
- docker pull $REPOSITORY_URI:$IMAGE_TAG


build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build --cache-from $REPOSITORY_URI:$IMAGE_TAG -t $IMAGE_REPO_NAME:$IMAGE_TAG .
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG

post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
- echo Push the latest image to cluster
- kubectl apply -f eks_cicd/deployment.yaml
- kubectl rollout restart -f eks_cicd/deployment.yaml
4 changes: 4 additions & 0 deletions eks/cluster-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "Inside cluster setup"
ls -l
#Create cluster if don't exist
aws eks --region $AWS_DEFAULT_REGION update-kubeconfig --name preview-server || eksctl create cluster -f cluster.yaml
13 changes: 13 additions & 0 deletions eks/cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
name: preview-server
region: us-west-2

nodeGroups:
- name: ng-1-workers
labels: { role: workers }
instanceType: t2.large
desiredCapacity: 2
privateNetworking: true
42 changes: 42 additions & 0 deletions eks/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: preview-server-app-development
spec:
replicas: 1
selector:
matchLabels:
app: preview-server-app
environment: testing
template:
metadata:
labels:
app: preview-server-app
environment: testing
spec:
containers:
- name: preview-server
image: 684910554524.dkr.ecr.us-west-2.amazonaws.com/ecrrepository-app:latest
resources:
requests:
cpu: "100m"
memory: "500Mi"
limits:
cpu: "200m"
memory: "1024Mi"
ports:
- containerPort: 80
name: http
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 60
timeoutSeconds: 5
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 70
timeoutSeconds: 10
failureThreshold: 10
18 changes: 18 additions & 0 deletions eks/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/name: preview-server
app.kubernetes.io/instance: preview-server-instance
app.kubernetes.io/version: "1.0.0"
app.kubernetes.io/component: backend
app.kubernetes.io/managed-by: kubectl
name: preview-server
spec:
selector:
app: preview-server-app
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 80