|
| 1 | +# YAML objects named with dot is anchors, which are not recognized as jobs |
| 2 | +.run_bot: &run_bot |
| 3 | + - docker rm -f ${CONTAINER_NAME} || true |
| 4 | + - docker pull ${CONTAINER_RELEASE_IMAGE} |
| 5 | + # Add new envs here. Don't forget to add them in exmaple.env and docker-compose files. |
| 6 | + - docker run |
| 7 | + -d |
| 8 | + --name ${CONTAINER_NAME} |
| 9 | + --restart always |
| 10 | + --label traefik.http.routers.${BOT_PROJECT_NAME}.rule="Host(\`${BOT_URL}\`)" |
| 11 | + --label traefik.enable=true |
| 12 | + --label traefik.http.services.${BOT_PROJECT_NAME}.loadbalancer.server.port="8000" |
| 13 | + --log-opt max-size=10m |
| 14 | + --log-opt max-file=5 |
| 15 | + -e POSTGRES_DSN="${POSTGRES_DSN}" |
| 16 | + -e REDIS_DSN="${REDIS_DSN}" |
| 17 | + -e BOT_CREDENTIALS="${BOT_CREDENTIALS}" |
| 18 | + -e DEBUG="${DEBUG:-false}" |
| 19 | + $CONTAINER_RELEASE_IMAGE |
| 20 | + |
| 21 | +.create_db: &create_db |
| 22 | + - psql -c "create user \"${POSTGRES_USER}\"" postgres || true |
| 23 | + - psql -c "alter user \"${POSTGRES_USER}\" with password '${POSTGRES_PASSWORD}'" postgres |
| 24 | + - psql -c "create database \"${POSTGRES_DB}\" with owner \"${POSTGRES_USER}\"" postgres || true |
| 25 | + |
| 26 | +.install_dependencies: &install_dependencies |
| 27 | + - echo -e "machine ${GIT_HOST}\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc |
| 28 | + - pip install -q poetry |
| 29 | + - poetry config virtualenvs.in-project true |
| 30 | + - poetry install |
| 31 | + |
| 32 | +.cache_dependencies: &cache_dependencies |
| 33 | + key: |
| 34 | + files: |
| 35 | + - poetry.lock |
| 36 | + prefix: "venv" |
| 37 | + paths: |
| 38 | + - .cache/pip |
| 39 | + - .venv |
| 40 | + |
| 41 | +.postgres_envs: &postgres_envs |
| 42 | + - POSTGRES_USER=${CONTAINER_NAME} |
| 43 | + - POSTGRES_DB=${CONTAINER_NAME} |
| 44 | + - POSTGRES_PASSWORD=$(openssl rand -hex 16) |
| 45 | + - POSTGRES_HOST=${PROD_POSTGRES_HOST} |
| 46 | + - POSTGRES_DSN=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST/$POSTGRES_DB |
| 47 | + |
| 48 | +# Jobs |
| 49 | +variables: |
| 50 | + GIT_DEPTH: 1 # Fetch only latest commit |
| 51 | + PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip" |
| 52 | + |
| 53 | +stages: |
| 54 | + - check |
| 55 | + - build |
| 56 | + - deploy |
| 57 | + |
| 58 | +default: |
| 59 | + interruptible: true |
| 60 | + |
| 61 | +lint: |
| 62 | + image: python:3.10 |
| 63 | + stage: check |
| 64 | + tags: |
| 65 | + - docker |
| 66 | + cache: *cache_dependencies |
| 67 | + before_script: |
| 68 | + - *install_dependencies |
| 69 | + script: |
| 70 | + - poetry run ./scripts/lint |
| 71 | + |
| 72 | +test: |
| 73 | + image: python:3.10 |
| 74 | + stage: check |
| 75 | + tags: |
| 76 | + - docker |
| 77 | + services: |
| 78 | + - postgres:11.4-alpine |
| 79 | + - redis:6.0.1-alpine |
| 80 | + cache: *cache_dependencies |
| 81 | + variables: |
| 82 | + BOT_CREDENTIALS: cts.example.com@secret@123e4567-e89b-12d3-a456-426655440000 |
| 83 | + POSTGRES_DSN: postgres://postgres:postgres@postgres/postgres |
| 84 | + REDIS_DSN: redis://redis/0 |
| 85 | + before_script: |
| 86 | + - *install_dependencies |
| 87 | + script: |
| 88 | + - poetry run pytest --cov-config=setup.cfg |
| 89 | + coverage: '/Total coverage: \d\d\d.\d\d%/' |
| 90 | + |
| 91 | +build: |
| 92 | + image: docker:latest |
| 93 | + stage: build |
| 94 | + tags: |
| 95 | + - docker |
| 96 | + before_script: |
| 97 | + - docker info |
| 98 | + - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY |
| 99 | + - CONTAINER_RELEASE_IMAGE="$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG:-$CI_COMMIT_REF_SLUG}" |
| 100 | + script: |
| 101 | + - docker pull $CONTAINER_RELEASE_IMAGE || true |
| 102 | + - docker build |
| 103 | + --cache-from $CONTAINER_RELEASE_IMAGE |
| 104 | + --build-arg GIT_HOST=$GIT_HOST |
| 105 | + --build-arg CI_JOB_TOKEN=$CI_JOB_TOKEN |
| 106 | + --build-arg CI_COMMIT_SHA=$CI_COMMIT_SHA |
| 107 | + --force-rm |
| 108 | + -t $CONTAINER_RELEASE_IMAGE . |
| 109 | + - docker push $CONTAINER_RELEASE_IMAGE |
| 110 | + - docker rmi $CONTAINER_RELEASE_IMAGE |
| 111 | + |
| 112 | +deploy.botstest: |
| 113 | + image: docker:latest |
| 114 | + stage: deploy |
| 115 | + tags: |
| 116 | + - bots-test |
| 117 | + only: |
| 118 | + - branches |
| 119 | + when: manual |
| 120 | + environment: |
| 121 | + name: test |
| 122 | + on_stop: deploy.botstest.stop |
| 123 | + variables: |
| 124 | + # https://docs.gitlab.com/ee/ci/runners/configure_runners.html#git-strategy |
| 125 | + GIT_STRATEGY: none |
| 126 | + before_script: |
| 127 | + - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY |
| 128 | + - if [ -z ${BOT_PROJECT_NAME:-} ]; then BOT_PROJECT_NAME=${CI_PROJECT_PATH_SLUG#"$CI_PROJECT_NAMESPACE-"}; fi |
| 129 | + - CONTAINER_NAME=$BOT_PROJECT_NAME-`echo $CI_COMMIT_REF_NAME | sed 's/^.*\///'` |
| 130 | + - CONTAINER_RELEASE_IMAGE="$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG:-$CI_COMMIT_REF_SLUG}" |
| 131 | + - BOT_URL="${BOT_PROJECT_NAME}.${DEV_SERVER_HOST}" |
| 132 | + - BOT_CREDENTIALS=$DEV_BOT_CREDENTIALS |
| 133 | + - *postgres_envs |
| 134 | + - REDIS_DSN=redis://${PROD_REDIS_HOST}/1 |
| 135 | + script: |
| 136 | + - echo "Use URL 'https://${BOT_URL}/' in your cts admin site" |
| 137 | + - echo "Using credentials ${BOT_CREDENTIALS}" |
| 138 | + - echo "Deploing Docker container ${CONTAINER_NAME}" |
| 139 | + - *create_db |
| 140 | + - *run_bot |
| 141 | + |
| 142 | +deploy.botstest.stop: |
| 143 | + when: manual |
| 144 | + environment: |
| 145 | + name: test |
| 146 | + action: stop |
| 147 | + extends: deploy.botstest |
| 148 | + script: |
| 149 | + - docker rm -f ${CONTAINER_NAME} || true |
| 150 | + - psql -c "drop database \"${POSTGRES_DB}\"" postgres || true |
| 151 | + - psql -c "drop user \"${POSTGRES_USER}\"" postgres || true |
| 152 | + |
| 153 | +deploy.botsprod: |
| 154 | + stage: deploy |
| 155 | + image: docker:latest |
| 156 | + tags: |
| 157 | + - bots-prod |
| 158 | + only: |
| 159 | + # Note the bots-prod worker requires branch to be protected |
| 160 | + - master |
| 161 | + when: manual |
| 162 | + environment: |
| 163 | + name: production |
| 164 | + variables: |
| 165 | + # https://docs.gitlab.com/ee/ci/runners/configure_runners.html#git-strategy |
| 166 | + GIT_STRATEGY: none |
| 167 | + before_script: |
| 168 | + - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY |
| 169 | + - if [ -z ${BOT_PROJECT_NAME:-} ]; then BOT_PROJECT_NAME=${CI_PROJECT_PATH_SLUG#"$CI_PROJECT_NAMESPACE-"}; fi |
| 170 | + - CONTAINER_NAME=$BOT_PROJECT_NAME |
| 171 | + - CONTAINER_RELEASE_IMAGE="$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG:-$CI_COMMIT_REF_SLUG}" |
| 172 | + - BOT_URL="${BOT_PROJECT_NAME}.${PROD_SERVER_HOST}" |
| 173 | + - *postgres_envs |
| 174 | + - REDIS_DSN=redis://${PROD_REDIS_HOST}/1 |
| 175 | + script: |
| 176 | + - echo "Use URL 'https://${BOT_URL}/' in your cts admin site" |
| 177 | + - echo "Using credentials ${BOT_CREDENTIALS}" |
| 178 | + - echo "Deploing Docker container ${CONTAINER_NAME}" |
| 179 | + - *create_db |
| 180 | + - *run_bot |
0 commit comments