From 2d3b76728364a496a2c8a85e68cbf60c52006f22 Mon Sep 17 00:00:00 2001 From: dannc Date: Fri, 16 Sep 2022 10:45:00 +0700 Subject: [PATCH] add test and gitlab-ci --- .gitignore | 1 + .gitlab-ci.yml | 71 ++++++++++++++++++++++++++++++++++++++++++++ test/validate_api.sh | 11 +++++++ 3 files changed, 83 insertions(+) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100755 test/validate_api.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57f1cb2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..dc03dd2 --- /dev/null +++ b/.gitlab-ci.yml @@ -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 diff --git a/test/validate_api.sh b/test/validate_api.sh new file mode 100755 index 0000000..c66d6ba --- /dev/null +++ b/test/validate_api.sh @@ -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