-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildspec.yml
More file actions
70 lines (63 loc) · 3.24 KB
/
Copy pathbuildspec.yml
File metadata and controls
70 lines (63 loc) · 3.24 KB
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
---
version: 0.2
phases:
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
# Fetch Docker credentials from AWS Secrets Manager
- echo "Fetching Docker credentials from AWS Secrets Manager"
- RECITER_SCORE_SECRET=$(aws secretsmanager get-secret-value --secret-id reciterscore --query "SecretString" --output text)
# Extract username and password from the JSON response
- REPO_NAME=$(echo $RECITER_SCORE_SECRET | sed -n 's/.*"repoName":"\([^"]*\)".*/\1/p')
- echo "Repo Name $REPO_NAME"
- ACCOUNT_ID=$(echo $RECITER_SCORE_SECRET | sed -n 's/.*"accountId":"\([^"]*\)".*/\1/p')
- AWS_REGION=$(echo $RECITER_SCORE_SECRET | sed -n 's/.*"awsRegion":"\([^"]*\)".*/\1/p')
- echo "AWS_REGION $AWS_REGION"
- DEV_LAMBDA_NAME=$(echo $RECITER_SCORE_SECRET | sed -n 's/.*"devlambdaFunctionName":"\([^"]*\)".*/\1/p')
- echo "DEV LAMBDA_NAME $DEV_LAMBDA_NAME"
- PROD_LAMBDA_NAME=$(echo $RECITER_SCORE_SECRET | sed -n 's/.*"prodlambdaFunctionName":"\([^"]*\)".*/\1/p')
- echo "PROD LAMBDA_NAME $PROD_LAMBDA_NAME"
- echo " BRANCH NAME ${BRANCH}"
- echo Logging in to Amazon ECR...
- TAG="${BRANCH}-$CODEBUILD_BUILD_NUMBER.$(date +%Y-%m-%d.%H.%M.%S).$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)"
- echo "Tag Name $TAG"
- aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
- REPO_URI=$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO_NAME
- echo "REPO_URI $REPO_URI"
build:
commands:
- echo Building Docker image...
- docker build -t $REPO_NAME .
- docker tag $REPO_NAME:latest $REPO_URI:$TAG
- echo Pushing Docker image...
- docker push $REPO_URI:$TAG
# Log out from the Docker registry to clear credentials
post_build:
commands:
- echo Pushing image to ECR...
- docker push $REPO_URI:$TAG
- docker logout $REPO_URI
- echo Deploying new image to Lambda function...
- |
if expr "${BRANCH}" : ".*dev" >/dev/null; then
aws lambda update-function-code --function-name $DEV_LAMBDA_NAME --image-uri $REPO_URI:$TAG --region $AWS_REGION
echo Deployment complete.
fi
- |
if expr "${BRANCH}" : ".*master" >/dev/null; then
aws lambda update-function-code --function-name $PROD_LAMBDA_NAME --image-uri $REPO_URI:$TAG --region $AWS_REGION
echo Deployment complete.
fi
artifacts:
files:
- '**/*'