Skip to content

Commit 7f3faa4

Browse files
authored
Merge pull request #17 from geoadmin/task_BGDIINF_SB-1839_migration_dockerhub_to_ecr
BGDIINF_SB-1839: Migration from dockerhub to ecr
2 parents 7e61008 + c3f8bba commit 7f3faa4

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

Makefile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,21 @@ ISORT_CMD := $(PIPENV_RUN) isort
3939
NOSE_CMD := $(PIPENV_RUN) nose2
4040
PYLINT_CMD := $(PIPENV_RUN) pylint
4141

42-
# Docker variables
43-
DOCKER_IMG_LOCAL_TAG = swisstopo/$(SERVICE_NAME):local
42+
# AWS variables
43+
AWS_DEFAULT_REGION = eu-central-1
4444

4545
# Docker metadata
4646
GIT_HASH := `git rev-parse HEAD`
47+
GIT_HASH_SHORT = `git rev-parse --short HEAD`
4748
GIT_BRANCH := `git symbolic-ref HEAD --short 2>/dev/null`
4849
GIT_DIRTY := `git status --porcelain`
4950
GIT_TAG := `git describe --tags || echo "no version info"`
5051
AUTHOR := $(USER)
5152

53+
# Docker variables
54+
DOCKER_REGISTRY = 974517877189.dkr.ecr.eu-central-1.amazonaws.com
55+
DOCKER_IMG_LOCAL_TAG = $(DOCKER_REGISTRY)/$(SERVICE_NAME):local-$(USER)-$(GIT_HASH_SHORT)
56+
5257
all: help
5358

5459
# This bit check define the build/python "target": if the system has an acceptable version of python, there will be no need to install python locally.
@@ -86,7 +91,9 @@ help:
8691
@echo -e " \033[1mLOCAL SERVER TARGETS\033[0m "
8792
@echo "- serve Run the project using the flask debug server"
8893
@echo "- gunicornserve Run the project using the gunicorn WSGI server"
94+
@echo "- dockerlogin Login to the AWS ECR registery for pulling/pushing docker images"
8995
@echo "- dockerbuild Build the project localy using the gunicorn WSGI server inside a container"
96+
@echo "- dockerpush Build and push the project localy (with tag := $(DOCKER_IMG_LOCAL_TAG))"
9097
@echo "- dockerrun Run the project using the gunicorn WSGI server inside a container. (Exposed_port: $(HTTP_PORT)"
9198
@echo "- shutdown Stop the aforementioned container"
9299
@echo -e " \033[1mCLEANING TARGETS\033[0m "
@@ -143,6 +150,10 @@ gunicornserve: $(REQUIREMENTS_TIMESTAMP)
143150
${PYTHON_CMD} wsgi.py
144151

145152
# Docker related functions.
153+
.PHONY: dockerlogin
154+
dockerlogin:
155+
aws --profile swisstopo-bgdi-builder ecr get-login-password --region $(AWS_DEFAULT_REGION) | docker login --username AWS --password-stdin $(DOCKER_REGISTRY)
156+
146157
.PHONY: dockerbuild
147158
dockerbuild:
148159
docker build \
@@ -152,6 +163,10 @@ dockerbuild:
152163
--build-arg VERSION="$(GIT_TAG)" \
153164
--build-arg AUTHOR="$(AUTHOR)" -t $(DOCKER_IMG_LOCAL_TAG) .
154165

166+
.PHONY: dockerpush
167+
dockerpush: dockerbuild
168+
docker push $(DOCKER_IMG_LOCAL_TAG)
169+
155170
export-http-port:
156171
@export HTTP_PORT=$(HTTP_PORT)
157172

README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- [Dependencies](#dependencies)
1212
- [Service API](#service-api)
1313
- [Local Development](#local-development)
14-
- [Docker](#docker)
14+
- [Docker helpers](#docker-helpers)
1515
- [Versioning](#versioning)
1616
- [Deployment](#deployment)
1717

@@ -123,17 +123,12 @@ To stop serving through containers,
123123
make shutdown
124124

125125
Is the command you're looking for.
126+
### Docker helpers
126127

127-
## Docker
128+
From each github PR that is merged into `master` or into `develop`, one Docker image is built and pushed on AWS ECR with the following tag:
128129

129-
The service is encapsulated in a Docker image. Images are pushed on the public [Dockerhub](https://hub.docker.com/r/swisstopo/service-shortlink/tags) registry. From each github PR that is merged into develop branch, one Docker image is built and pushed with the following tags:
130-
131-
- `develop.latest`
132-
- `CURRENT_VERSION-beta.INCREMENTAL_NUMBER`
133-
134-
From each github PR that is merged into master, one Docker image is built an pushed with the following tag:
135-
136-
- `VERSION`
130+
- `vX.X.X` for tags on master
131+
- `vX.X.X-beta.X` for tags on develop
137132

138133
Each image contains the following metadata:
139134

@@ -143,12 +138,16 @@ Each image contains the following metadata:
143138
- git.dirty
144139
- version
145140

146-
These metadata can be seen directly on the dockerhub registry in the image layers or can be read with the following command
141+
These metadata can be read with the following command
147142

148143
```bash
144+
# NOTE: Currently we don't have permission to do docker pull on AWS ECR
145+
make dockerlogin
146+
docker pull 974517877189.dkr.ecr.eu-central-1.amazonaws.com/service-shortcut:develop.latest
147+
149148
# NOTE: jq is only used for pretty printing the json output,
150149
# you can install it with `apt install jq` or simply enter the command without it
151-
docker image inspect --format='{{json .Config.Labels}}' swisstopo/service-shortlink:develop.latest | jq
150+
docker image inspect --format='{{json .Config.Labels}}' 974517877189.dkr.ecr.eu-central-1.amazonaws.com/service-shortcut:develop.latest | jq
152151
```
153152

154153
You can also check these metadata on a running container as follows
@@ -157,11 +156,20 @@ You can also check these metadata on a running container as follows
157156
docker ps --format="table {{.ID}}\t{{.Image}}\t{{.Labels}}"
158157
```
159158

160-
## Versioning
159+
To build a local docker image tagged as `service-shortcut:local-${USER}-${GIT_HASH_SHORT}` you can
160+
use
161+
162+
```bash
163+
make dockerbuild
164+
```
165+
166+
To push the image on the ECR repository use the following two commands
161167

162-
This service uses [SemVer](https://semver.org/) as versioning scheme. The versioning is automatically handled by `.github/workflows/main.yml` file.
168+
```bash
169+
make dockerlogin
170+
make dockerpush
171+
```
163172

164-
See also [Git Flow - Versioning](https://github.com/geoadmin/doc-guidelines/blob/master/GIT_FLOW.md#versioning) for more information on the versioning guidelines.
165173

166174
## Deployment
167175

@@ -181,4 +189,4 @@ The service is configured by Environment Variable:
181189
| ALLOWED_DOMAINS | 'admin.ch,swisstopo.ch,bgdi.ch' | A comma separated list of allowed domains names |
182190
| ALLOWED_HOSTS | 'api.geo.admin.ch,api3.geo.admin.ch' | a comma separated list of allowed hostnames |
183191
| AWS_DYNAMODB_TABLE_NAME | 'shortlinks_test' | The dynamodb table name |
184-
| AWS_DYNAMODB_TABLE_REGION | 'eu-central-1' | The AWS region in which the table is hosted. |
192+
| AWS_DYNAMODB_TABLE_REGION | 'eu-central-1' | The AWS region in which the table is hosted. |

buildspec.yml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@ version: 0.2
22

33
env:
44
variables:
5-
IMAGE_BASE_NAME: 'swisstopo/service-shortlink'
5+
IMAGE_BASE_NAME: 'service-shortlink'
6+
REGISTRY: '974517877189.dkr.ecr.eu-central-1.amazonaws.com'
67
SHELL: /bin/bash
78
AWS_DEFAULT_REGION: eu-central-1
89
USER: "aws_code_build"
910
TEST_REPORT_DIR: "./tests/report"
1011
TEST_REPORT_FILE: "nose2-junit.xml"
11-
parameter-store:
12-
CI_DOCKERHUB_USER: "/dockerhub/user"
13-
CI_DOCKERHUB_PASSWORD: "/dockerhub/password"
1412

1513
phases:
1614
install:
1715
runtime-versions:
1816
docker: 18
1917
commands:
2018
- echo "Installing necessary softwares"
21-
- docker login -u ${CI_DOCKERHUB_USER} -p ${CI_DOCKERHUB_PASSWORD}
22-
- apt-get update && apt-get install -y docker-compose python3-pip
19+
- apt-get update && apt-get install -y docker-compose python3-pip pass gnupg2
20+
- echo "Install aws cli v2 for docker login to ECR registry"
21+
- curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
22+
- unzip awscliv2.zip
23+
- ./aws/install
24+
- aws --version
25+
- echo "Login to AWS ECR docker registry"
26+
- aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${REGISTRY}
27+
2328
pre_build:
2429
commands:
2530
- echo "export of the image tag for build and push purposes"
@@ -39,11 +44,11 @@ phases:
3944
build:
4045
commands:
4146
- echo Build started on $(date)
42-
- export DOCKER_IMG_TAG=${IMAGE_BASE_NAME}:${GITHUB_TAG}
43-
- export DOCKER_IMG_TAG_LATEST=${IMAGE_BASE_NAME}:${GITHUB_BRANCH}.latest
47+
- export DOCKER_IMG_TAG=${REGISTRY}/${IMAGE_BASE_NAME}:${GITHUB_TAG}
48+
- export DOCKER_IMG_TAG_LATEST=${REGISTRY}/${IMAGE_BASE_NAME}:${GITHUB_BRANCH}.latest
4449
- |-
4550
if [ "${GITHUB_TAG}" = "" ] ; then
46-
export DOCKER_IMG_TAG=${IMAGE_BASE_NAME}:${GITHUB_BRANCH}.${GITHUB_COMMIT}
51+
export DOCKER_IMG_TAG=${REGISTRY}/${IMAGE_BASE_NAME}:${GITHUB_BRANCH}.${GITHUB_COMMIT}
4752
export GITHUB_TAG=${GITHUB_COMMIT}
4853
fi
4954
- echo "Building docker image with tags ${DOCKER_IMG_TAG} and ${DOCKER_IMG_TAG_LATEST}"
@@ -69,7 +74,7 @@ phases:
6974
docker push ${DOCKER_IMG_TAG}
7075
fi
7176
- |
72-
if [ "${GITHUB_BRANCH}" = "develop" ]; then
77+
if [ "${GITHUB_BRANCH}" = "develop" ] || [ "${GITHUB_BRANCH}" = "master" ]; then
7378
echo "Pushing ${DOCKER_IMG_TAG} and ${DOCKER_IMG_TAG_LATEST} to dockerhub"
7479
docker push ${DOCKER_IMG_TAG}
7580
docker push ${DOCKER_IMG_TAG_LATEST}

0 commit comments

Comments
 (0)