-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildspec.yml
75 lines (72 loc) · 2.91 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
version: 0.2
env:
variables:
IMAGE_BASE_NAME: "service-name"
REGISTRY: "974517877189.dkr.ecr.eu-central-1.amazonaws.com"
SHELL: /bin/bash
AWS_DEFAULT_REGION: eu-central-1
USER: "aws_code_build"
PIPENV_NOSPIN: 1
phases:
install:
runtime-versions:
python: 3.9
commands:
- aws --version
- echo "Login to AWS ECR docker registry"
- aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${REGISTRY}
pre_build:
commands:
- echo "Export of the image tag for build and push purposes"
# Reading git branch (the utility in the deploy script is unable to read it automatically on CodeBuild)
# see https://stackoverflow.com/questions/47657423/get-github-git-branch-for-aws-codebuild
- export GITHUB_BRANCH="$(git symbolic-ref HEAD --short 2>/dev/null)"
- |
if [ "${GITHUB_BRANCH}" = "" ] ; then
GITHUB_BRANCH="$(git branch -a --contains HEAD | sed -n 2p | awk '{ printf $1 }')";
export GITHUB_BRANCH=${GITHUB_BRANCH#remotes/origin/};
fi
- export GITHUB_COMMIT=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- export GITHUB_TAG="$(git describe --tags || echo 'no-version-info')"
- echo "GITHUB_BRANCH=${GITHUB_BRANCH} GITHUB_COMMIT=${GITHUB_COMMIT} GITHUB_TAG=${GITHUB_TAG} DOCKER_IMG_TAG=${DOCKER_IMG_TAG}"
- echo "Creating a clean environment"
- make ci
build:
commands:
- echo "Check code formatting"
- make ci-check-format
- echo "Linting code"
- make lint
- echo "Linting openapi spec"
- make lint-spec
- echo Build started on $(date)
- export DOCKER_IMG_TAG=${REGISTRY}/${IMAGE_BASE_NAME}:${GITHUB_TAG}
- export DOCKER_IMG_TAG_LATEST=${REGISTRY}/${IMAGE_BASE_NAME}:${GITHUB_BRANCH}.latest
- |-
if [ "${GITHUB_TAG}" = "" ] ; then
export DOCKER_IMG_TAG=${REGISTRY}/${IMAGE_BASE_NAME}:${GITHUB_BRANCH}.${GITHUB_COMMIT}
export GITHUB_TAG=${GITHUB_COMMIT}
fi
- echo "Building docker image with tags ${DOCKER_IMG_TAG} and ${DOCKER_IMG_TAG_LATEST}"
- >
docker build
--build-arg GIT_HASH="${GITHUB_COMMIT}"
--build-arg GIT_BRANCH="${GITHUB_BRANCH}"
--build-arg VERSION="${GITHUB_TAG}"
--build-arg AUTHOR="CI"
-t ${DOCKER_IMG_TAG} -t ${DOCKER_IMG_TAG_LATEST} .
post_build:
commands:
- echo "Unit testing..."
- make test
# Only push image to registry for merge to develop and master
- |
if [ "${GITHUB_BRANCH}" = "master" ] || [ "${GITHUB_BRANCH}" = "develop" ]; then
echo "Pushing image ${DOCKER_IMG_TAG} to the registry"
docker push ${DOCKER_IMG_TAG}
fi
- |
if [ "${GITHUB_BRANCH}" = "develop" ]; then
echo "Pushing image ${DOCKER_IMG_TAG_LATEST} to the registry"
docker push ${DOCKER_IMG_TAG_LATEST}
fi