This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/.gitlab-ci.yml

91 lines
2.5 KiB
YAML
Raw Normal View History

2020-12-26 16:49:47 +01:00
stages:
- build
- test
- publish
- deploy
variables:
# Can't compose variables with user-defined ones, so we repeat ourselves :'(
# See https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1809
IMAGE_REGISTRY: rg.fr-par.scw.cloud/crocmagnon
IMAGE_TESTS: $CI_REGISTRY_IMAGE:tests-latest
IMAGE_DEPS_TESTS: $CI_REGISTRY_IMAGE:tests-deps
IMAGE_LATEST: rg.fr-par.scw.cloud/crocmagnon/blog:latest
IMAGE_DEPS_LATEST: $CI_REGISTRY_IMAGE:deps
PRE_COMMIT_IMAGE: rg.fr-par.scw.cloud/crocmagnon/pre-commit:latest
.build: &build
2020-12-26 16:56:15 +01:00
image: docker:20.10.1
2020-12-26 16:58:16 +01:00
services:
- docker:20.10.1-dind
2020-12-26 16:49:47 +01:00
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker login -u $REGISTRY_USER -p $REGISTRY_PASSWORD $IMAGE_REGISTRY
- docker pull $IMAGE || true
- docker pull $DEPS || true
# Build this first stage separately so that we can cache it
- docker build --pull --build-arg POETRY_OPTIONS --target venv --cache-from $DEPS -t $DEPS .
# Build the final image. The "cache from" deps is necessary to take advantage of the intermediate image cache.
# See https://stackoverflow.com/a/52649913/2758732 and https://github.com/moby/moby/issues/34715
- docker build --pull --build-arg POETRY_OPTIONS --cache-from $IMAGE --cache-from $DEPS -t $IMAGE .
- docker push $IMAGE
- docker push $DEPS
build-tests:
<<: *build
stage: build
variables:
POETRY_OPTIONS: ""
before_script:
- export IMAGE=$IMAGE_TESTS
- export DEPS=$IMAGE_DEPS_TESTS
.tests: &tests
stage: test
image: $IMAGE_TESTS
unit_tests:
<<: *tests
script:
- cd /app
- python -m pytest
pre_commit:
stage: test
image: $PRE_COMMIT_IMAGE
script:
- pre-commit run --all-files --color always --show-diff-on-failure
2020-12-26 17:24:37 +01:00
needs: []
2020-12-26 16:49:47 +01:00
missing_migrations:
<<: *tests
script:
- cd /app
- python manage.py makemigrations --check
publish:
<<: *build
stage: publish
variables:
POETRY_OPTIONS: "--no-dev"
before_script:
- export IMAGE=$IMAGE_LATEST
- export DEPS=$IMAGE_DEPS_LATEST
only:
- master
2020-12-26 18:05:50 +01:00
deploy:
stage: deploy
image: ubuntu
before_script:
- 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y --no-install-recommends )'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
script:
- ssh blog -o StrictHostKeyChecking=no "/home/gaugendre/blog/update"
only:
- master