forked from Privado-Inc/privado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildspec.yml
54 lines (52 loc) · 2.31 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
version: 0.2
env:
shell: bash
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- docker login -u $DOCKER_USER -p $DOCKER_ACCESS_TOKEN
- aws --version
- ACCOUNT_ID=${CODEBUILD_BUILD_ARN} && IFS=':' && set -- $ACCOUNT_ID && ACCOUNT_ID=$5
- aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
- REPOSITORY_URI=$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/privado-main
- |
if [[ $AWS_REGION = "eu-west-1" ]]
then
LATEST_TAG="$(git describe --tags --abbrev=0)"
VERSION=${LATEST_TAG:1}
else
LATEST_TAG=$(curl -i "Accept: application/vnd.github+json" -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/Privado-Inc/privado/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
VERSION_INITIAL=${LATEST_TAG:1}
VERSION=$VERSION_INITIAL-$CODEBUILD_BUILD_NUMBER
fi
- TAG_1=latest
- TAG_2=$VERSION
- echo $CODEBUILD_WEBHOOK_HEAD_REF
- TAG_3=${CODEBUILD_WEBHOOK_HEAD_REF##*/}
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $REPOSITORY_URI:latest --build-arg VERSION=$VERSION .
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$TAG_1
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$TAG_2
post_build:
commands:
- echo Build completed on `date`
- echo "Removing older <untagged> images"
- IMAGES_TO_DELETE=$(aws ecr list-images --region $AWS_REGION --repository-name privado-main --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --max-items 100 --output json)
- echo "$IMAGES_TO_DELETE"
- aws ecr batch-delete-image --region $AWS_REGION --repository-name privado-main --image-ids "$IMAGES_TO_DELETE" || true
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:$TAG_1
- docker push $REPOSITORY_URI:$TAG_2
- |
if [[ -z "$TAG_3" ]]
then
echo "\$TAG_3 is empty"
else
docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$TAG_3-$VERSION
docker push $REPOSITORY_URI:$TAG_3-$VERSION
fi
- echo completed pushing image to ecr