Skip to content

Commit

Permalink
ci: Use IMAGE_PREFIX for docker images
Browse files Browse the repository at this point in the history
This will allow specifying where to push the images right from semaphore

#122: Deploy to separate GCP project
  • Loading branch information
MichaelAkvo committed Apr 12, 2022
1 parent 1f4742f commit 11632ef
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 33 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IMAGE_PREFIX=docker.io
2 changes: 2 additions & 0 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ global_job_config:
- name: coveralls
prologue:
commands:
# Export all default variables
- set -a && source .env && set +a
- echo "${DOCKER_PASSWORD}" | docker login --username
"${DOCKER_USERNAME}" --password-stdin
- export CI_COMMIT="${SEMAPHORE_GIT_SHA:0:7}"
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ docker volume rm isco-docker-sync
## Production

```bash
export CI_COMMIT='local'
set -a
source .env
CI_COMMIT='local'
set +a
./ci/build.sh
```

Above command will generate two docker images with prefix `eu.gcr.io/akvo-lumen/isco` for backend and frontend
Above command will generate two docker images for the backend and frontend

```bash
docker-compose -f docker-compose.yml -f docker-compose.ci.yml up -d
Expand Down
2 changes: 2 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.coverage
.pytest_cache
63 changes: 35 additions & 28 deletions ci/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
# Required env vars:
# CI_COMMIT - commit hash
#shellcheck disable=SC2039

set -exuo pipefail
Expand All @@ -8,33 +10,36 @@ set -exuo pipefail
BACKEND_CHANGES=0
FRONTEND_CHANGES=0

COMMIT_CONTENT=$(git diff --name-only "${CI_COMMIT_RANGE}")

if grep -q "backend" <<< "${COMMIT_CONTENT}"
then
BACKEND_CHANGES=1
fi

if grep -q "frontend" <<< "${COMMIT_CONTENT}"
then
FRONTEND_CHANGES=1
fi

if grep -q "ci" <<< "${COMMIT_CONTENT}"
then
BACKEND_CHANGES=1
FRONTEND_CHANGES=1
fi

if [[ "${CI_BRANCH}" == "main" && "${CI_PULL_REQUEST}" != "true" ]];
then
BACKEND_CHANGES=1
FRONTEND_CHANGES=1
if [[ -n "${FORCE:-}" ]] ; then
BACKEND_CHANGES=1
FRONTEND_CHANGES=1
else
COMMIT_CONTENT=$(git diff --name-only "${CI_COMMIT_RANGE}")

if grep -q "backend" <<< "${COMMIT_CONTENT}"
then
BACKEND_CHANGES=1
fi

if grep -q "frontend" <<< "${COMMIT_CONTENT}"
then
FRONTEND_CHANGES=1
fi

if grep -q "ci" <<< "${COMMIT_CONTENT}"
then
BACKEND_CHANGES=1
FRONTEND_CHANGES=1
fi

if [[ "${CI_BRANCH}" == "main" && "${CI_PULL_REQUEST}" != "true" ]];
then
BACKEND_CHANGES=1
FRONTEND_CHANGES=1
fi
fi


image_prefix="eu.gcr.io/akvo-lumen/isco"

# Normal Docker Compose
dc () {
docker-compose \
Expand All @@ -53,24 +58,26 @@ frontend_build () {
echo "PUBLIC_URL=/" > frontend/.env
sed 's/"warn"/"error"/g' < frontend/.eslintrc.json > frontend/.eslintrc.prod.json

dc down
dc run \
--rm \
--no-deps \
frontend \
bash release.sh

docker build \
--tag "${image_prefix}/frontend:latest" \
--tag "${image_prefix}/frontend:${CI_COMMIT}" frontend
--tag "${IMAGE_PREFIX}/frontend:latest" \
--tag "${IMAGE_PREFIX}/frontend:${CI_COMMIT}" frontend
}

backend_build () {

docker build \
--tag "${image_prefix}/backend:latest" \
--tag "${image_prefix}/backend:${CI_COMMIT}" backend
--tag "${IMAGE_PREFIX}/backend:latest" \
--tag "${IMAGE_PREFIX}/backend:${CI_COMMIT}" backend

# Test and Code Quality
dc down
dc -f docker-compose.test.yml \
-p backend-test \
run --rm -T backend ./test.sh
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ services:
command: "postgres -c 'shared_buffers=128MB' -c 'fsync=off' -c 'synchronous_commit=off' -c 'full_page_writes=off' -c 'max_connections=100' -c 'bgwriter_lru_maxpages=0' -c 'client_min_messages=warning'"
restart: always
frontend:
image: eu.gcr.io/akvo-lumen/isco/frontend:latest
image: ${IMAGE_PREFIX}/frontend:latest
command: ["nginx", "-g", "daemon off;"]
depends_on:
- backend
- db
backend:
image: eu.gcr.io/akvo-lumen/isco/backend:latest
image: ${IMAGE_PREFIX}/backend:latest
network_mode: service:mainnetwork
command: ["sh", "run.sh"]
environment:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
- ./db/script:/script:ro
restart: always
backend:
image: eu.gcr.io/akvo-lumen/isco/backend:latest
image: ${IMAGE_PREFIX}/backend:latest
volumes:
- ./:/app:delegated
working_dir: /app/backend
Expand Down

0 comments on commit 11632ef

Please sign in to comment.