From d4a4f4f7c1765116106b873528f532052cb35815 Mon Sep 17 00:00:00 2001 From: sdesnoe <simon.desnoe@beebryte.com> Date: Tue, 5 Sep 2023 12:03:00 +0200 Subject: [PATCH] feat: add depandabot template --- r2_metadata/depandabot.r2.yml | 15 ++++++ .../depandabot/CHANGELOG.md | 5 ++ .../depandabot/README.md | 38 +++++++++++++++ .../depandabot/depandabot.yml | 47 +++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 r2_metadata/depandabot.r2.yml create mode 100644 templates/python/dependancy_management/depandabot/CHANGELOG.md create mode 100644 templates/python/dependancy_management/depandabot/README.md create mode 100644 templates/python/dependancy_management/depandabot/depandabot.yml diff --git a/r2_metadata/depandabot.r2.yml b/r2_metadata/depandabot.r2.yml new file mode 100644 index 0000000..021b5c5 --- /dev/null +++ b/r2_metadata/depandabot.r2.yml @@ -0,0 +1,15 @@ +files: + template: ../templates/python/dependency_management/depandabot/depandabot.yml + documentation: ../templates/python/dependency_management/depandabot/README.md + changelog: ../templates/python/dependency_management/depandabot/CHANGELOG.md +data: + description: "Run pip-compile to generate a requirements.txt and create a merge request on your repository with the updated requirements.txt" + icon: âš« + public: true + labels: + - Utilities + - Dependency management + - Gitlab + - Python + license: MIT + deprecated: false diff --git a/templates/python/dependancy_management/depandabot/CHANGELOG.md b/templates/python/dependancy_management/depandabot/CHANGELOG.md new file mode 100644 index 0000000..b54d613 --- /dev/null +++ b/templates/python/dependancy_management/depandabot/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog +All notable changes to this job will be documented in this file + +## [0.1.0] - 2023-09-05 +* Initial version \ No newline at end of file diff --git a/templates/python/dependancy_management/depandabot/README.md b/templates/python/dependancy_management/depandabot/README.md new file mode 100644 index 0000000..1dec917 --- /dev/null +++ b/templates/python/dependancy_management/depandabot/README.md @@ -0,0 +1,38 @@ +## Objective + +The objective of the `depandabot` job is to provide a way to update the requirements.txt file and create a merge request on a Gitlab instance. This reusable job can help speed up other jobs creation and ensure consistent configuration across CI jobs. + +## How to use it + +1. Include the depandabot template in your CI/CD configuration (see quick use above). +2. Create a job that extend the `depandabot` job template. +3. If you need to customize the job, check + the [jobs customization](https://docs.r2devops.io/get-started/use-templates/#job-templates-customization). + +## Variables + +| Name | Description | Default | +| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | +| `PROJECT_PATH` | The path to the project root directory. | `"."` | +| `REQUIREMENTS_FILE_PATH` | the path to the requirements file. | `"${PROJECT_PATH}/requirements.txt"` | +| `IMAGE_TAG` | The default tag for the docker image. See [Python Docker Official Image](https://hub.docker.com/_/python) for supported tags and respective Dockerfile links. | `"latest"` | +| `PYTHON_SETUP` | Bash commands to setup your python environment. Default rely on `requirements.txt` to install all dependencies. | `"pip install -r ${REQUIREMENTS_FILE_PATH}"` | + +## Example of use + +```yaml +include: + # Include the python_install job template (don't forget to replace the version tag) + - remote: 'https://api.r2devops.io/job/r/gitlab/dorianturba/r2devops_catalog/r2_metadata/depandabot@0.1.0.yaml' + +requirements311: + extends: + - .depandabot # extends the python_install job template + stage: dependency_management + variables: + REQUIREMENTS_FILE_PATH: "${PROJECT_PATH}/requirements-py311.txt" # override the default REQUIREMENTS_FILE_PATH variable + IMAGE_TAG: "3.11" # override the default IMAGE_TAG variable + GITLAB_API_URL: "gitlab.example.com" # override the default GITLAB_API_URL + script: + - !reference [script] # reuse the script from the python_install job template +``` diff --git a/templates/python/dependancy_management/depandabot/depandabot.yml b/templates/python/dependancy_management/depandabot/depandabot.yml new file mode 100644 index 0000000..ea6f75b --- /dev/null +++ b/templates/python/dependancy_management/depandabot/depandabot.yml @@ -0,0 +1,47 @@ +depandabot: + stage: dependency_management + image: python:${IMAGE_TAG} + + variables: + PROJECT_PATH: "." + REQUIREMENTS_FILE_PATH: "${PROJECT_PATH}/requirements.txt" + IMAGE_TAG: "latest" + GITLAB_API_URL: "${CI_SERVER_HOST}" + + before_script: + - python --version ; pip --version # debugging + - python -m venv venv --upgrade-deps || python -m venv venv + - source venv/bin/activate + - pip install pip-tools + + script: + - DEPS_BRANCH="depandabot/requirements-txt/$(date +%s)" + - | + COMMIT_MESSAGE="build(deps): bump new versions" + - $([ -f ${REQUIREMENTS_FILE_PATH} ]) && ACTION="update" || ACTION="create" + - pip-compile --quiet -o ${REQUIREMENTS_FILE_PATH} + - if [ -n "$(git status --porcelain ${REQUIREMENTS_FILE_PATH})" ]; then + - | + curl --header "Authorization: Bearer ${DEPANDABOT_TOKEN}" \ + --form "branch=$DEPS_BRANCH" \ + --form "ref=${CI_DEFAULT_BRANCH}" \ + "https://${GITLAB_API_URL}/api/v4/projects/${CI_PROJECT_ID}/repository/branches" + - | + curl --header "Authorization: Bearer ${DEPANDABOT_TOKEN}" \ + --form "branch=$DEPS_BRANCH" \ + --form "commit_message=$COMMIT_MESSAGE" \ + --form "actions[][action]=$ACTION" \ + --form "actions[][file_path]=${REQUIREMENTS_FILE_PATH}" \ + --form "actions[][content]=<${REQUIREMENTS_FILE_PATH}" \ + "https://${GITLAB_API_URL}/api/v4/projects/${CI_PROJECT_ID}/repository/commits" + - | + curl --header "Authorization: Bearer ${DEPANDABOT_TOKEN}" \ + --form "source_branch=$DEPS_BRANCH" \ + --form "target_branch=${CI_DEFAULT_BRANCH}" \ + --form "title=$COMMIT_MESSAGE" \ + "https://${GITLAB_API_URL}/api/v4/projects/${CI_PROJECT_ID}/merge_requests" + - fi + + + + -- GitLab