Skip to content

Instantly share code, notes, and snippets.

@pandaedward
Last active October 2, 2019 02:35
Show Gist options
  • Select an option

  • Save pandaedward/c4a1fdfe7ece0a8a0265eee8b6385ffe to your computer and use it in GitHub Desktop.

Select an option

Save pandaedward/c4a1fdfe7ece0a8a0265eee8b6385ffe to your computer and use it in GitHub Desktop.
AWS Codebuild buildspec.yml file
version: 0.2
env:
variables:
APP_NAME: "cicd-art-web"
NS_NAME: "cicd"
parameter-store:
EKS_SECRET_NAME: "/eks/secret-name"
EKS_SECRET_NS: "/eks/secret-namespace"
EKS_SECRET_TOKEN: "/eks/secret-token"
phases:
install:
runtime-versions:
python: 3.7
pre_build:
commands:
- echo Pre-Build started on `date`
- echo Logging in to Amazon ECR...
- aws --version
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- REPOSITORY_URI='<YOUR_CONTAINER_IMAGE_REPO>'
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH:=latest}
- docker-compose run web python basic_test.py
finally:
- echo Pre Build phase completed
build:
commands:
- echo Build started on `date`
- echo Building Docker image...
- docker build -t $REPOSITORY_URI:latest .
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
finally:
- echo Build phase completed
post_build:
commands:
- echo Post-Build started on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- aws ssm put-parameter --name /eks/image-tag --value $IMAGE_TAG --type SecureString --overwrite
- sed -i -e "s/TO_BE_REPLACED/$IMAGE_TAG/" deployment.yml
- echo Setting up kubectl for deployment...
- kubectl config set-cluster eks-ins --server='<YOUR_EKS_API_SERVER_URL>' --certificate-authority=ca.crt
- kubectl config set-context eks-ins --cluster eks-ins
- kubectl config set-credentials $EKS_SECRET_NAME --token=$EKS_SECRET_TOKEN
- kubectl config set-context eks-ins --user=$EKS_SECRET_NAME
- kubectl config use-context eks-ins
- kubectl apply -f deployment.yml
- kubectl config view > kubeconfig.file
finally:
- echo Post-Build phase completed
artifacts:
files:
- '**/*'
name: cicd-art-build-$(date +%Y%m%d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment