-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathk8-buildspec.yml
75 lines (69 loc) · 3.99 KB
/
k8-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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
version: 0.2
phases:
install:
commands:
- kubectl version --short --client
pre_build:
commands:
# Fetch Docker credentials from AWS Secrets Manager
- echo "Fetching Docker credentials from AWS Secrets Manager"
- SECRET=$(aws secretsmanager get-secret-value --secret-id docker-hub-credentials --query "SecretString" --output text)
# Extract username and password from the JSON response
- DOCKER_USERNAME=$(echo $SECRET | sed -n 's/.*"username":"\([^"]*\)".*/\1/p')
- DOCKER_PASSWORD=$(echo $SECRET | sed -n 's/.*"password":"\([^"]*\)".*/\1/p')
# Print the Docker username to the CodeBuild log
- echo "Docker Username $DOCKER_USERNAME"
# Log in to Docker registry
- echo "Logging in to Docker registry"
- echo "$DOCKER_PASSWORD" | docker login --username $DOCKER_USERNAME --password-stdin
- TAG="${BRANCH}-$CODEBUILD_BUILD_NUMBER.$(date +%Y-%m-%d.%H.%M.%S).$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)"
- echo "${CODEBUILD_WEBHOOK_HEAD_REF}"
- echo "${BRANCH}"
- |
if expr "${BRANCH}" : ".*dev" >/dev/null; then
sed -i.bak -e 's@RECITER-SERVICE@'"reciter-prod"'@' -e 's@RECITER-PUBMED-SERVICE@'"reciter-pubmed-dev"'@' config/local.js;
aws s3 cp s3://reciter-config/config/dev/saml/idp.crt ./config/certs/idp.crt
aws s3 cp s3://reciter-config/config/dev/saml/reciter-saml.crt ./config/certs/reciter-saml.crt
aws s3 cp s3://reciter-config/config/dev/saml/reciter-saml.key ./config/certs/reciter-saml.key
fi
- |
if expr "${BRANCH}" : ".*master" >/dev/null; then
sed -i.bak -e 's@RECITER-SERVICE@'"reciter-prod"'@' -e 's@RECITER-PUBMED-SERVICE@'"reciter-pubmed-prod"'@' config/local.js;
aws s3 cp s3://reciter-config/config/prod/saml/idp.crt ./config/certs/idp.crt
aws s3 cp s3://reciter-config/config/prod/saml/reciter-saml.crt ./config/certs/reciter-saml.crt
aws s3 cp s3://reciter-config/config/prod/saml/reciter-saml.key ./config/certs/reciter-saml.key
fi
- $(aws ecr get-login --no-include-email)
# - sed -i -e "s/ADMIN_API_KEY/$ADMIN_API_KEY/g" config/local.js
# - sed -i -e "s/TOKEN_SECRET/$TOKEN_SECRET/g" config/local.js
- cat config/local.js
# #Install Trivy for container and dependency scanning
# - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
# - apt-get install wget apt-transport-https gnupg lsb-release
# - wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | apt-key add -
# - echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | tee -a /etc/apt/sources.list.d/trivy.list
# - apt-get update
# - apt-get install -y trivy
# trivy --no-progress --exit-code 1 --severity HIGH,CRITICAL $REPOSITORY_URI:$TAG
build:
commands:
- |
if expr "${BRANCH}" : ".*master" >/dev/null || expr "${BRANCH}" : ".*dev" >/dev/null; then
docker build --build-arg NEXT_PUBLIC_RECITER_API_KEY=$ADMIN_API_KEY --build-arg NEXT_PUBLIC_RECITER_TOKEN_SECRET=$TOKEN_SECRET --tag $REPOSITORY_URI:$TAG .
fi
post_build:
commands:
- |
if expr "${BRANCH}" : ".*master" >/dev/null || expr "${BRANCH}" : ".*dev" >/dev/null; then
docker push $REPOSITORY_URI:$TAG
# Log out from the Docker registry to clear credentials
docker logout $REPOSITORY_URI
aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region us-east-1 --role-arn $EKS_KUBECTL_ROLE_ARN
fi
if expr "${BRANCH}" : ".*master" >/dev/null; then
kubectl set image deployment/reciter-pm-prod reciter-pm=$REPOSITORY_URI:$TAG -n $EKS_CLUSTER_NAME
fi
if expr "${BRANCH}" : ".*dev" >/dev/null; then
kubectl set image deployment/reciter-pm-dev reciter-pm=$REPOSITORY_URI:$TAG -n $EKS_CLUSTER_NAME
fi