Commit ed5c8d7b authored by Luis Leon's avatar Luis Leon
Browse files

Add first prototype

parents 65d2f4af f7020414
Copyright (c) 2020 RidgeRun LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Local CI Hooks
This repo contains the instructions to perform local CI before a push
\ No newline at end of file
This script adds Git hooks to your repository to run the tests automatically
while pushing commits to your remotes. The idea is to avoid pushing broken
code to your remotes, automatising the process of verification.
## Installation
```bash
pip install local_ci_hooks
```
## Usage
Run the script in a repository:
```bash
local_ci_hooks --install
```
This will install the pre-push hook for you. Also, it will create a file
named `.local_ci.sh` with a dummy test. You can set your test commands
in this file.
To uninstall the hook:
```bash
local_ci_hooks --uninstall
```
## Example scripts
For modifying the CI, please, take into consideration the following
`.local_ci.sh` example files.
NodeJS:
```bash
#!/bin/bash
npm install || exit 1
npm run test || exit 1
exit 0
```
Python:
```bash
#!/bin/bash
pip install --prefix ./build . || exit 1
export PATH="./build/bin:$PATH"
export LD_LIBARY_PATH="./build/lib:$LD_LIBRARY_PATH"
pytest . || exit 1
pip uninstall <your project>
exit 0
```
Version: 0.1.0
#!/bin/bash
# Copyright 2020 - RidgeRun LLC
# Author: Luis G. Leon Vega <luis.leon@ridgerun.com>
# Licenced under MIT
# Support: only Linux/MacOS with BASH
touch file_ok.txt
cat file_ok.txt
if [ "$?" -ne "0" ]; then
exit 1
fi
exit 0
#!/bin/bash
# Copyright 2020 - RidgeRun LLC
# Author: Luis G. Leon Vega <luis.leon@ridgerun.com>
# Licenced under MIT
# Support: only Linux/MacOS with BASH
OPTION=$1
EXECUTABLE=$(python3 -c "import local_ci_hooks as _; print(_.__file__)")
LIB_PATH="$(dirname ${EXECUTABLE})"
function check_repo() {
echo "Checking git repository"
git status > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "> Error: the current directory does not have a repo"
exit 1
else
echo "> OK"
fi
GIT_TOP_DIR=$(git rev-parse --show-toplevel)
}
function help() {
echo "Usage:"
echo " local_ci_hooks <option>"
echo "Options:"
echo " -i, --install: installs Local CI onto the current repo"
echo " -u, --uninstall: uninstalls Local CI from the current repo"
}
case $OPTION in
-i|--install)
check_repo
echo "Installing Local CI onto the current repo"
cp ${LIB_PATH}/local_ci.sh ${GIT_TOP_DIR}/.local_ci.sh
cp ${LIB_PATH}/pre-push.hook ${GIT_TOP_DIR}/.git/hooks/pre-push
if [ "$?" -eq "0" ]; then
echo "> Installed"
fi
;;
-u|--uninstall)
echo "Uninstalling Local CI from the current repo"
rm ${GIT_TOP_DIR}/.git/hooks/pre-push.hook > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
echo "> Uninstalled"
else
echo "> Not uninstalled. Probably it's never been there"
fi
;;
-h|--help)
help
exit 0
;;
*)
echo "Invalid option"
help
exit 1
;;
esac;
#!/bin/bash
# Copyright 2020 - RidgeRun LLC
# Author: Luis G. Leon Vega <luis.leon@ridgerun.com>
# Licenced under MIT
# Support: only Linux/MacOS with BASH
SCRIPT=.local_ci.sh
# Checking test script
echo "- Checking test script $GIT_DIR/${SCRIPT}"
cat $(pwd)/${SCRIPT} > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "Test script not found. Skipping tests"
exit 0
fi
# Creating temporary dir with the repo
echo "- Cloning the repo with the last commit in a temporary folder"
ORIGIN_DIR=$(pwd)
TEST_DIR="$(mktemp -d /tmp/.local-ci.XXXXXX)"
CURRENT_COMMIT=$(git rev-parse HEAD)
git clone $(pwd) ${TEST_DIR} > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "> Could not clone the repo"
rm -r ${TEST_DIR}
exit 1
fi
cd ${TEST_DIR}
# Executing tests
echo "- Executing tests"
$(pwd)/${SCRIPT} > ${ORIGIN_DIR}/.local-ci.log
if [ "$?" -ne "0" ]; then
echo "> Tests did not pass. Please, check the failing tests in .local-ci.log"
rm -r ${TEST_DIR}
exit 1
fi
# Clean up
echo "> Tests passed. Continuing with pushing the repo"
cd -
rm ${ORIGIN_DIR}/.local-ci.log
rm -r ${TEST_DIR}
exit 0
# Copyright 2020 - RidgeRun LLC
# Author: Luis G. Leon Vega <luis.leon@ridgerun.com>
# Licenced under MIT
# Support: only Linux/MacOS with BASH
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="local_ci_hooks",
version="0.1.0",
author="Luis G. Leon-Vega",
author_email="luis@luisleon.me",
description="Install a Local CI engine",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://gitlab.com/lleon95/local-ci-hooks.git",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: Linux",
],
scripts=['local_ci_hooks/local_ci_hooks'],
package_data={'': ['*.hook', '*.sh']},
include_package_data=True,
install_requires=[
],
python_requires='>=3.8',
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment