This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildspec.yml
100 lines (95 loc) · 4.2 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
95
96
97
98
99
100
version: 0.2
env:
variables:
IMAGE_BASE_NAME: "swisstopo/service-print"
IMAGE_BASE_NAME_NGINX: "swisstopo/service-print-nginx"
IMAGE_BASE_NAME_TOMCAT: "swisstopo/service-print-tomcat"
SHELL: /bin/bash
AWS_DEFAULT_REGION: eu-west-1
parameter-store:
CI_DOCKERHUB_USER: "/dockerhub/user"
CI_DOCKERHUB_PASSWORD: "/dockerhub/password"
phases:
install:
runtime-versions:
docker: 18
commands:
- echo "Installing softwares"
- echo SHELL=$SHELL
- curl -s -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/bin/docker-compose
- chmod +x /usr/bin/docker-compose
- bash --version
pre_build:
commands:
- echo Configuring stuff...
- aws --version
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- GITHUB_BRANCH=${CODEBUILD_WEBHOOK_HEAD_REF#refs/heads/*}
- TODAY=$(date '+%Y%m%d')
- IMAGE_TAG=r_${TODAY}_${COMMIT_HASH}
- CONFIG_TAG=${COMMIT_HASH}
- COMPOSE_PROJECT_NAME=${IMAGE_TAG}
- |-
if [ "${CODEBUILD_WEBHOOK_TRIGGER}" = pr/* ]; then
PULL_REQUEST=${CODEBUILD_WEBHOOK_TRIGGER#pr/*}
fi
- |-
if [ "${GIT_BRANCH}" != master ]; then
LATEST=${GITHUB_BRANCH:-staging}
else
LATEST=latest
fi
- echo "=========== VERSIONS ============="
- docker --version
- docker-compose --version
- python --version
- python2 --version
- bash --version
- echo "=========== VARIABLES ============="
- echo CODEBUILD_RESOLVED_SOURCE_VERSION=${CODEBUILD_RESOLVED_SOURCE_VERSION}
- echo CODEBUILD_SOURCE_VERSION=${CODEBUILD_SOURCE_VERSION}
- echo CODEBUILD_WEBHOOK_BASE_REF=${CODEBUILD_WEBHOOK_BASE_REF}
- echo CODEBUILD_WEBHOOK_HEAD_REF=${CODEBUILD_WEBHOOK_HEAD_REF=}
- echo CODEBUILD_WEBHOOK_TRIGGER=${CODEBUILD_WEBHOOK_TRIGGER}
- echo CODEBUILD_SOURCE_VERSION=${CODEBUILD_SOURCE_VERSION}
- echo CODEBUILD_BUILD_ID=${CODEBUILD_BUILD_ID}
- echo CODEBUILD_SOURCE_REPO_URL=${CODEBUILD_SOURCE_REPO_URL}
- echo COMMIT_HASH=${COMMIT_HASH}
- echo PULL_REQUEST=${PULL_REQUEST}
- echo GITHUB_BRANCH=${GITHUB_BRANCH}
- echo IMAGE_TAG=${IMAGE_TAG}
- echo CONFIG_TAG=${CONFIG_TAG}
- echo "==================================="
build:
commands:
- echo Build started on $(date)
- echo Building the Docker image...
- make cleanall
- echo "export IMAGE_TAG=${IMAGE_TAG}" >> rc_user
- make dockerbuild CI=true
post_build:
commands:
- echo Build completed on `date`
- docker-compose -p "${IMAGE_TAG}" up -d
- docker ps -a
- sleep 5
- echo Testing
- NGINX_OK=$(docker ps -aq --filter status="running" --filter name="${COMPOSE_PROJECT_NAME}_nginx") && if [ -z "$NGINX_OK" ]; then exit 1; fi
- TOMCAT_OK=$(docker ps -aq --filter status="running" --filter name="${COMPOSE_PROJECT_NAME}_tomcat") && if [ -z "$TOMCAT_OK" ]; then exit 1; fi
- WSGI_OK=$(docker ps -aq --filter status="running" --filter name="${COMPOSE_PROJECT_NAME}") && if [ -z "$WSGI_OK" ]; then exit 1; fi
- echo Containers are running!
- DOCKER_CONTAINER_ID="$(docker ps | grep "python3 print3" | awk '{ print $1 }')"
- docker exec -i "$DOCKER_CONTAINER_ID" coverage run --source=print3 --omit=print3/wsgi.py setup.py test
- echo All tests are successful
- echo Pushing the Docker image...
- docker login -u "$CI_DOCKERHUB_USER" -p "$CI_DOCKERHUB_PASSWORD"
- docker tag "${IMAGE_BASE_NAME}:${IMAGE_TAG}" "${IMAGE_BASE_NAME}:${LATEST}"
- docker tag "${IMAGE_BASE_NAME_NGINX}:${IMAGE_TAG}" "${IMAGE_BASE_NAME_NGINX}:${LATEST}"
- docker tag "${IMAGE_BASE_NAME_TOMCAT}:${IMAGE_TAG}" "${IMAGE_BASE_NAME_TOMCAT}:${LATEST}"
- docker push "${IMAGE_BASE_NAME}:${IMAGE_TAG}"
- docker push "${IMAGE_BASE_NAME_NGINX}:${IMAGE_TAG}"
- docker push "${IMAGE_BASE_NAME_TOMCAT}:${IMAGE_TAG}"
- docker push "${IMAGE_BASE_NAME}:${LATEST}"
- docker push "${IMAGE_BASE_NAME_NGINX}:${LATEST}"
- docker push "${IMAGE_BASE_NAME_TOMCAT}:${LATEST}"
- echo "Success"