-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildspec.yml
94 lines (80 loc) · 3.12 KB
/
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
version: 0.2
phases:
install:
on-failure: ABORT
commands:
- echo "Downloading AWS Lightsail Control Plugin..."
- curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "/usr/local/bin/lightsailctl"
- chmod +x /usr/local/bin/lightsailctl
- echo "Lightsail plugin successfully installed."
pre_build:
commands:
- echo "Showing env..."
- env
build:
on-failure: ABORT
commands:
- |
echo "Parsing HEAD_REF: $CODEBUILD_WEBHOOK_HEAD_REF"
APP_VERSION=$( echo $CODEBUILD_WEBHOOK_HEAD_REF | cut -d '/' -f 3 )
# Default to main branch (development)
[ -z "$APP_VERSION" ] && APP_VERSION="main"
echo "Building the Docker image for Python Cowsay version ${APP_VERSION}..."
docker build -t $IMAGE_NAME:$APP_VERSION .
docker images $IMAGE_NAME:$APP_VERSION
- |
echo "Installing dev dependencies..."
ls $(pwd)
docker run -v $(pwd):/app --rm \
public.ecr.aws/docker/library/python:3.8-slim \
pip install -r /app/requirements.dev.txt --target=/app/libs.dev
echo "Running unit test..."
docker run -v $(pwd):/app --rm \
-w /app -e PYTHONPATH=/app/libs.dev \
public.ecr.aws/docker/library/python:3.8-slim \
/app/libs.dev/bin/pytest -v
post_build:
on-failure: ABORT
commands:
- |
export APP_VERSION=$( echo $CODEBUILD_WEBHOOK_HEAD_REF | cut -d '/' -f 3 )
# Default to main branch (development)
[ -z "$APP_VERSION" ] && APP_VERSION="main"
echo "Deploying version '${APP_VERSION}'..."
- |
echo "Pushing image to Lightsail container service '${APP_VERSION}'..."
aws lightsail push-container-image --region $AWS_REGION --service-name $LIGHTSAIL_SERVICE_NAME --label $IMAGE_NAME --image $IMAGE_NAME:$APP_VERSION
cat <<EOF > /tmp/lightsail-containers.json
{
"cowsay-api": {
"image": ":${LIGHTSAIL_SERVICE_NAME}.${IMAGE_NAME}.latest",
"environment": {
"APP_WORKER": "4",
"APP_BIND": "0.0.0.0:8080"
},
"ports": {
"8080": "HTTP"
}
}
}
EOF
cat <<EOF > /tmp/lightsail-endpoint.json
{
"containerName": "cowsay-api",
"containerPort": 8080
}
EOF
# Let's see if the files was created
ls -l /tmp/*.json
- |
# Wait previous deployment to finish
while [ "$( aws lightsail get-container-service-deployments --service-name $LIGHTSAIL_SERVICE_NAME | grep state | head -1 | grep ACTIVATING )" ]
do
echo "Waiting previous deployment to finish... (10 secs)"
sleep 10
done
# Deploy container
aws lightsail create-container-service-deployment \
--service-name $LIGHTSAIL_SERVICE_NAME \
--containers file:///tmp/lightsail-containers.json \
--public-endpoint file:///tmp/lightsail-endpoint.json