add test and gitlab-ci

This commit is contained in:
2022-09-16 10:45:00 +07:00
parent b7409dcf7d
commit 2d3b767283
3 changed files with 83 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/.idea/

71
.gitlab-ci.yml Normal file
View File

@@ -0,0 +1,71 @@
---
stages:
- build
- test
- deploy
pre-build:
image: busybox:latest
stage: build
script:
- |
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
tag="latest"
echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
else
tag="$CI_COMMIT_REF_SLUG"
echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
fi
- echo "DOCKER_IMAGE=$CI_REGISTRY_IMAGE:${tag}"" > docker.env
artifacts:
reports:
dotenv:
docker.env
# todo взять скрипт с yandex
docker-build:
# Use the official docker image.
image: docker:latest
stage: build
services:
- docker:dind
needs:
- pre-build
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# All other branches are tagged with the escaped branch name (commit ref slug)
script:
- docker build --pull -t "$DOCKER_IMAGE" -f docker/Dockerfile .
- docker push "$DOCKER_IMAGE"
# Run this job in a branch where a Dockerfile exists
rules:
- if: $CI_COMMIT_BRANCH
exists:
- docker/Dockerfile
test-api:
image: alpine/curl:latest
stage: test
variables:
PYTHON_API_HOSTNAME: python-api
services:
- name: $DOCKER_IMAGE
alias: python-api
needs:
- pre-build
- docker-build
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- ./test/validate_api.sh
# todo взять скрипт с yandex
deploy-main:
image: docker:latest
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- echo 1

11
test/validate_api.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/sh
statusCode=$(curl -s -o /dev/null -w "%{http_code}" http://$PYTHON_API_HOSTNAME:5290/get_info)
if [ $statusCode == "200" ]; then
echo "service available"
exit 0
else
echo "service unavailable"
exit 1
fi