diff --git a/.circleci/config.yml b/.circleci/config.yml index 12a4918..2af376e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,19 +3,161 @@ version: 2.1 orbs: python: circleci/python@0.2.1 + +setup_env_variables: &setup_env_variables + - run: + name: Setup env variables + command: | + tagged_version() { + # Grabs version from either the env variable CIRCLE_TAG + # Note: we assume that all tags start with prefix 'v' + if [[ -n "${CIRCLE_TAG}" ]]; then + echo "${CIRCLE_TAG#v}" + else + eval 'cat ./version.txt' + fi + } + + BASE_BUILD_VERSION="$(tagged_version)" + echo "Setting BASE_BUILD_VERSION as $BASE_BUILD_VERSION" + echo "export BASE_BUILD_VERSION=$BASE_BUILD_VERSION" >> $BASH_ENV + + +publish_docker: &publish_docker + + - run: + name: Build Docker image + command: | + if [[ -z ${CIRCLE_TAG} ]]; then + echo "ERROR: the workflow should only be executed on the tag" + exit 1 + fi + if [[ -z ${BASE_BUILD_VERSION} ]]; then + echo "ERROR: Set up the base build version by running setup_env_variables macros" + exit 1 + fi + + echo "Buiding image $IMAGE_NAME:$BASE_BUILD_VERSION" + docker build -t $IMAGE_NAME:$BASE_BUILD_VERSION . + echo "Using docker username: $DOCKER_USERNAME" + echo "$DOCKER_PWD" | docker login -u "$DOCKER_USERNAME" --password-stdin + echo "Pushing image $IMAGE_NAME:$BASE_BUILD_VERSION to docker hub" + docker push $IMAGE_NAME:$BASE_BUILD_VERSION + jobs: build-and-test: executor: python/default + working_directory: ~/algorithms steps: - checkout - python/load-cache - python/install-deps - python/save-cache + - <<: *setup_env_variables + - run: + name: Verify git tag and version consistency + command: | + python3 setup.py verify + + build_publish_docker_version: + environment: + IMAGE_NAME: aivanou/test + + docker: + - image: circleci/buildpack-deps:stretch + + steps: + - checkout + + - setup_remote_docker + + - <<: *setup_env_variables + + - <<: *publish_docker + + + verify_correct_version: + docker: + - image: circleci/python:3.6 + + steps: + - checkout + - <<: *setup_env_variables + - run: + name: Verify git tag and version consistency + command: | + python3 setup.py verify + + + upload_to_pypi: + + docker: + - image: circleci/python:3.6 + + steps: + - checkout + - <<: *setup_env_variables + + # Download and cache dependencies + - restore_cache: + keys: + - v2-cpu-dependencies-{{ checksum "requirements.txt" }} + # fallback to using the latest cache if no exact match is found + - v2-cpu-dependencies- + - run: - command: ./manage.py test - name: Test + name: Install python deps + command: | + sudo pip install twine + + - run: + name: Setup .pypirc + command: | + echo -e "[pypi]" >> ~/.pypirc + echo -e "username = $PYPI_USERNAME" >> ~/.pypirc + echo -e "password = $PYPI_PWD" >> ~/.pypirc + - run: + name: Build packages + command: | + python3 setup.py sdist bdist_wheel + + - run: + name: Upload packages + command: | + python3 -m twine upload dist/* + workflows: main: jobs: - build-and-test + circleci-docs-workflow: + jobs: + - verify_correct_version: + context: test_ctx + filters: + branches: + ignore: /.*/ + tags: + only: /^v.*/ + + - build_publish_docker_version: + context: test_ctx + requires: + - verify_correct_version + filters: + branches: + ignore: /.*/ + tags: + only: /^v.*/ + + - upload_to_pypi: + context: test_ctx + requires: + - verify_correct_version + filters: + branches: + ignore: /.*/ + tags: + only: /^v.*/ + diff --git a/.gitignore b/.gitignore index 27da15c..393b63f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ target project/target .idea -*.txt diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..8f1458e --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,10 @@ +# CHANGELOG + +## 1.0.0 (April 29, 2020) + +### Test header + +* Test point [link] + + +[link]: https://github.com/tierex/algorithms diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..8f1458e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,10 @@ +# CHANGELOG + +## 1.0.0 (April 29, 2020) + +### Test header + +* Test point [link] + + +[link]: https://github.com/tierex/algorithms diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9396f51 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM alpine:3.8 + +ENTRYPOINT [ "echo" ] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b26e81f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +numpy + diff --git a/scripts/setup_env_variables.sh b/scripts/setup_env_variables.sh new file mode 100755 index 0000000..6a62314 --- /dev/null +++ b/scripts/setup_env_variables.sh @@ -0,0 +1,56 @@ +#!/bin/sh +tagged_version() { + # Grabs version from either the env variable CIRCLE_TAG + if [[ -n "${CIRCLE_TAG}" ]]; then + echo "${CIRCLE_TAG}" + else + eval 'cat ./version.txt' + fi +} + +BASE_BUILD_VERSION="$(tagged_version)" + +echo $BASE_BUILD_VERSION + +#BASE_BUILD_VERSION='cat ./version.txt' +# +#if tagged_version >/dev/null; then +# # Grab git tag, remove prefixed v and remove everything after - +# # Used to clean up tags that are for release candidates like v1.6.0-rc1 +# # Turns tag v1.6.0-rc1 -> v1.6.0 +# TORCHELASTIC_BUILD_VERSION="$(tagged_version)" +#fi +# +## We need to write an envfile to persist these variables to following +## steps, but the location of the envfile depends on the circleci executor +#if [[ "$(uname)" == Darwin ]]; then +# # macos executor (builds and tests) +# workdir="/Users/aivanou/code/algorithms" +#elif [[ -d "/home/circleci/algorithms" ]]; then +# # machine executor (binary tests) +# workdir="/home/circleci/algorithms" +#else +# # docker executor (binary builds) +# workdir="/" +#fi +#envfile="$workdir/env" +#touch "$envfile" +#chmod +x "$envfile" +# +#cat >>"$envfile" <> $BASH_ENV +# +#echo $envfile +# +#source $envfile diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..39ff020 --- /dev/null +++ b/setup.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + +# Copyright (c) Facebook, Inc. and its affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +import os +import re +import sys +import subprocess + +from setuptools import find_packages, setup +from setuptools.command.install import install + + +class VerifyVersionCommand(install): + """Custom command to verify that the git tag matches our version""" + description = 'verify that the git tag matches our version' + + def run(self): + if 'BASE_BUILD_VERSION' not in os.environ: + info = "Setup BASE_BUILD_VERSION variable to run verify command" + sys.exit(info) + + base_version = os.getenv('BASE_BUILD_VERSION') + if base_version[0] == 'v': + base_version = base_version[1:] + file_version = f"{get_version()}" + + if base_version != file_version: + info = "Git tag: {0} does not match the version: {1}".format( + base_version, file_version + ) + sys.exit(info) + + +def get_version(): + # get version string from version.py + version = open('version.txt', 'r').read().strip() + sha = 'Unknown' + try: + cwd = os.path.dirname(os.path.abspath(__file__)) + sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=cwd).decode('ascii').strip() + except Exception: + pass + + if 'BASE_BUILD_VERSION' not in os.environ and sha != 'Unknown': + version = f"{version}+{sha[:7]}" + + return version + + +if __name__ == "__main__": + if sys.version_info < (3, 6): + sys.exit("python >= 3.6 required") + + with open("README.md", encoding="utf8") as f: + readme = f.read() + + with open("requirements.txt") as f: + reqs = f.read() + + version = get_version() + print("-- Building version: " + version) + + setup( + # Metadata + name="aivanou", + version=version, + author="Aivanou", + author_email="csivanou@gmail.com", + description="Test project", + long_description=readme, + long_description_content_type="text/markdown", + url="https://github.com/tierex/algorithms", + license="BSD-3", + keywords=["test"], + python_requires=">=3.6", + install_requires=reqs.strip().split("\n"), + include_package_data=True, + packages=find_packages(exclude=("test", "test.*")), + test_suite="test.suites.unittests", + # PyPI package information. + classifiers=[ + ], + cmdclass={ + 'verify': VerifyVersionCommand, + } + ) diff --git a/version.txt b/version.txt new file mode 100644 index 0000000..5503126 --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +0.3.10