From 10e11d66dcc53a8775a573fb67862dc412bcaf3f Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 22 Sep 2021 00:04:11 +0200 Subject: [PATCH] Moved CI automation from Platypush hooks to Gitlab CI --- .gitlab-ci.yml | 62 +++++++++++++++++++++++++++++++++++++++++ .gitlab/rebuild_docs.sh | 33 ++++++++++++++++++++++ .gitlab/run_ci_tests.sh | 60 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100755 .gitlab/rebuild_docs.sh create mode 100755 .gitlab/run_ci_tests.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 000000000..a3dcee088 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,62 @@ +sync-to-github: + stage: build + script: + - echo "Synchronizing repo state to Github" + - export REPO_DIR="$(mktemp -d /tmp/platypush-XXXXX)" + - git clone git@git.platypush.tech:platypush/platypush.git "$REPO_DIR" + - cd "$REPO_DIR" + - git remote add github git@github.com:/BlackLight/platypush.git + - git checkout $CI_COMMIT_BRANCH + - git pull + - git push --mirror -v github + +run-tests: + stage: test + script: + - ./.gitlab/run_ci_tests.sh + +rebuild-docs: + stage: deploy + only: + - master + script: + - ./.gitlab/rebuild_docs.sh + +update-aur-packages: + stage: deploy + only: + - master + script: + - echo "Updating AUR packages" + - export REPO_DIR="$(mktemp -d /tmp/platypush-distutils-XXXXX)" + - git clone git@fabiomanganiello.com:/home/git/platypush-distutils.git "$REPO_DIR" + - cd "$REPO_DIR" + - git submodule init + - git submodule update + - cd distro/arch/git + - git checkout master + - git pull --rebase + - cd ../../../ + - cd distro/arch/stable + - git checkout master + - git pull --rebase + - cd ../../../ + - ./update.sh + - cd distro/arch/git + - changes="$(git status --porcelain --untracked-files=no)" + - "[[ -n \"$changes\" ]] && git commit -a -m '[Automatic] Package updated' && git push || echo 'No changes'" + - cd ../../../ + - cd distro/arch/stable + - changes="$(git status --porcelain --untracked-files=no)" + - "[[ -n \"$changes\" ]] && git commit -a -m '[Automatic] Package updated' && git push || echo 'No changes'" + +upload-pip-package: + stage: deploy + only: + - tags + script: + - rm -rf build dist *.egg-info + - export VERSION=$(grep -e '^\s*__version__\s*=' platypush/__init__.py | sed -r -e 's/^\s*__version__\s*=\s*.(.+?).\s*$/\1/') + - source ~/.credentials/pypi.env + - python setup.py sdist bdist_wheel + - twine upload ./dist/platypush-${VERSION}.tar.gz diff --git a/.gitlab/rebuild_docs.sh b/.gitlab/rebuild_docs.sh new file mode 100755 index 000000000..4e843a8bc --- /dev/null +++ b/.gitlab/rebuild_docs.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +LOGFILE="./docs.log" +STATUS_IMG_PATH="./docs-status.svg" + +build_docs() { + cd ./docs || exit 1 + make html 2>&1 | tee "../$LOGFILE" + ret=$? + cd .. || exit 1 + return $? +} + +######## +# MAIN # +######## + +build_docs +ret=$? + +log_base_path="$(date +/opt/tests/platypush/logs/docs/%Y-%m-%dT%H:%M:%S.%m)" +if [[ $ret == 0 ]]; then + wget -O "$STATUS_IMG_PATH" https://ci.platypush.tech/docs/passed.svg + cp "$LOGFILE" "${log_base_path}_PASSED.log" +else + wget -O "$STATUS_IMG_PATH" https://ci.platypush.tech/docs/failed.svg + cp "$LOGFILE" "${log_base_path}_FAILED.log" +fi + +mv "$STATUS_IMG_PATH" /opt/tests/platypush/logs/docs/ +mv "$LOGFILE" /opt/tests/platypush/logs/latest.log +cp -r docs/build/html /opt/repos/platypush/docs/build/ +exit $ret diff --git a/.gitlab/run_ci_tests.sh b/.gitlab/run_ci_tests.sh new file mode 100755 index 000000000..c4aee4434 --- /dev/null +++ b/.gitlab/run_ci_tests.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +BASE_DIR="$(mktemp -d '/tmp/platypush-ci-tests-XXXXX')" +VENV_DIR="$BASE_DIR/venv" +TEST_LOG="./test.log" +STATUS_IMG_PATH="./status.svg" + +cleanup() { + echo "Cleaning up environment" + rm -rf "$BASE_DIR" +} + +prepare_venv() { + echo "Preparing virtual environment" + python -m venv "$VENV_DIR" + cd "$VENV_DIR" || exit 1 + source ./bin/activate + cd - || exit 1 +} + +install_repo() { + echo "Installing latest version of the repository" + pip install '.[http]' +} + +run_tests() { + echo "Running tests" + pytest 2>&1 | tee "$TEST_LOG" + deactivate + + if grep -e '^FAILED ' "$TEST_LOG"; then + return 2 + fi + + return 0 # PASSED +} + +######## +# MAIN # +######## + +cleanup +prepare_venv +install_repo +run_tests +ret=$? +cleanup + +log_base_path="$(date +/opt/tests/platypush/logs/%Y-%m-%dT%H:%M:%S.%m)" +if [[ $ret == 0 ]]; then + wget -O "$STATUS_IMG_PATH" https://ci.platypush.tech/passed.svg + cp "$TEST_LOG" "${log_base_path}_PASSED.log" +else + wget -O "$STATUS_IMG_PATH" https://ci.platypush.tech/failed.svg + cp "$TEST_LOG" "${log_base_path}_FAILED.log" +fi + +mv "$STATUS_IMG_PATH" /opt/tests/platypush/logs/status.svg +mv "$TEST_LOG" /opt/tests/platypush/logs/latest.log +exit $ret