From 3f563d8b70fd8530a44221f6c9756de8b02799ee Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Fri, 13 Dec 2024 13:51:59 +0100 Subject: [PATCH] tentative to add ci pipeline --- .gitignore | 3 +++ .gitlab-ci.yml | 56 ++++++++++++++++++++++++++++++++++++++++++ make_openapi_client.sh | 52 ++++++++++++++++++++++++++++----------- 3 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 .gitlab-ci.yml diff --git a/.gitignore b/.gitignore index efcff6d..b85b124 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /frontend/.env.local /backend/ssl/ /backend/app/test.db +/backend/python-client/ +/backend/openapi.json +/backend/openapi-generator-cli.jar diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..e845b36 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,56 @@ +# GitLab CI/CD Configuration for FastAPI +stages: + - test + - deploy + - release + +variables: + PIP_CACHE_DIR: "$CI_PROJECT_DIR/.pip_cache" + VIRTUAL_ENV: ".venv" + +# Cache for dependencies +cache: + paths: + - .pip_cache/ + +before_script: # common setup for every job + - python3 -m venv $VIRTUAL_ENV + - source $VIRTUAL_ENV/bin/activate + - pip install --upgrade pip + +test: + stage: test + script: + - source $VIRTUAL_ENV/bin/activate + - pip install -r requirements.txt + - pytest --cov=app --cov-report=xml # Run tests and generate coverage report + +lint: + stage: test + script: + - source $VIRTUAL_ENV/bin/activate + - pip install flake8 + - flake8 app/ # Run linting for code quality + +deploy: + stage: deploy + only: + - main + script: + - echo "Updating repository..." + - git pull origin main # Update the repository with the latest code + - echo "Setting up Python dependencies..." + - source $VIRTUAL_ENV/bin/activate + - pip install -r requirements.txt # Install the required Python dependencies + - bash ./make_openapi_client.sh # Re-generate OpenAPI client library + - echo "Running the application..." + - python3.8 -m main # Replace with the exact command to start your app + +release: + stage: release + when: manual + variables: + TWINE_USERNAME: gitlab-ci-token + TWINE_PASSWORD: $CI_JOB_TOKEN + script: + - bash make_openapi_client.sh \ No newline at end of file diff --git a/make_openapi_client.sh b/make_openapi_client.sh index 124c233..7aa8d43 100755 --- a/make_openapi_client.sh +++ b/make_openapi_client.sh @@ -1,23 +1,45 @@ #!/bin/bash -VERSION={0.1.0} +VERSION=0.1.0 -# Run whataver you need to generate FastAPI .json file -pushd backend -python -m main generate-openapi -popd +# Navigate to project root +cd "$(dirname "$0")/backend" || exit -# Download OpenAPI generator +# Generate OpenAPI JSON file +python3 main.py generate-openapi +if [[ ! -f openapi.json ]]; then + echo "Error: openapi.json file not generated!" + exit 1 +fi + +# Download OpenAPI generator CLI if not present OPENAPI_VERSION=7.8.0 -wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${OPENAPI_VERSION}/openapi-generator-cli-${OPENAPI_VERSION}.jar -O openapi-generator-cli.jar +if [[ ! -f openapi-generator-cli.jar ]]; then + wget "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${OPENAPI_VERSION}/openapi-generator-cli-${OPENAPI_VERSION}.jar" -O openapi-generator-cli.jar +fi -# Run OpenAPI generator (requires java to be installed - this is already configured on mx-aare-test) -java -jar openapi-generator-cli.jar generate -i /backend/openapi.json -o python-client/ -g python --additional-properties=packageName=aare-test,packageVersion=${VERSION} -# build python package -cd python-client -python3.11 -m venv .venv +# Run OpenAPI generator +java -jar openapi-generator-cli.jar generate \ + -i "$(pwd)/openapi.json" \ + -o python-client/ \ + -g python \ + --additional-properties=packageName=aareDBclient,projectName=aareDB,packageVersion=${VERSION} + +# Check if the generator succeeded +if [[ ! -d python-client ]]; then + echo "OpenAPI generator failed. Exiting." + exit 1 +fi + +# Build Python package +cd python-client || exit +python3 -m venv .venv source .venv/bin/activate -pip install twine build +pip install -U pip twine build python -m build -# upload the package into the package repository - this uses variables from CI pipeline, so it won't work offline -twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/* \ No newline at end of file +# Upload package (only if in CI environment) +if [[ -z "${CI_API_V4_URL}" || -z "${CI_PROJECT_ID}" ]]; then + echo "Skipping package upload—CI variables not set." +else + twine upload --repository-url "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi" dist/* +fi \ No newline at end of file