Created
July 26, 2023 20:30
-
-
Save vatsal2210/05f2f7f6095df4109aa0c7e74600ad3f to your computer and use it in GitHub Desktop.
AWS Codebuild File to Deploy a node.js application to ECR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 0.2 | |
phases: | |
pre_build: | |
commands: | |
- set -e | |
- echo - Set parameters | |
- AWS_DEFAULT_REGION=YOUR_REGION | |
- AWS_ACCOUNT_ID=YOUR_ACCOUNT_ID | |
- REGISTRY_NAME=YOUR_REGISTRY_NAME | |
- EKS_ROLE_ARN=YOUR_EKS_ROLE_ARN | |
- EKS_CLUSTERNAME=YOUR_EKS_CLUSTERNAME | |
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$REGISTRY_NAME | |
- echo - Repository URI - $REPOSITORY_URI | |
# ECR Login | |
- echo Logging in to Amazon ECR... | |
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $REPOSITORY_URI | |
- IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F":" '{print $2}') | |
# Update Kube config Home Directory | |
- export KUBECONFIG=$HOME/.kube/config | |
build: | |
commands: | |
- echo - Build started on `date` | |
- echo - Building the Docker image... | |
- docker build -t $REPOSITORY_URI:latest . | |
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG | |
post_build: | |
commands: | |
- echo - Build completed on `date` | |
- echo - Pushing the Docker images... | |
- docker push $REPOSITORY_URI:latest | |
- echo "Pushed docker image to ECR" | |
- echo - Writing image definitions file... | |
- printf '{"AWSEBDockerrunVersion":"1","Image":{"Name":"%s","Update":"true"},"Ports":[{"ContainerPort":80,"HostPort":80}]}' $REPOSITORY_URI:$IMAGE_TAG > Dockerrun.aws.json | |
- cat Dockerrun.aws.json | |
# 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 deployment.yml | |
- 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.yml","imageUri":"%s"}]' $REPOSITORY_URI:latest > build.json | |
artifacts: | |
files: | |
- build.json | |
- deployment.yml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment