From 057413282315b740a97cc6cf6ea81c6e53dfd9f5 Mon Sep 17 00:00:00 2001 From: "Sun, Xuehao" Date: Thu, 15 Aug 2024 14:57:05 +0800 Subject: [PATCH] Freeze requirements workflow for release tag (#485) Signed-off-by: Sun, Xuehao Signed-off-by: NeuralChatBot Signed-off-by: BaoHuiling --- .../workflows/manual-freeze-requirements.yml | 38 ++++++++++++ .../workflows/scripts/freeze_requirements.sh | 59 +++++++++++++++++++ .gitignore | 1 + 3 files changed, 98 insertions(+) create mode 100644 .github/workflows/manual-freeze-requirements.yml create mode 100644 .github/workflows/scripts/freeze_requirements.sh diff --git a/.github/workflows/manual-freeze-requirements.yml b/.github/workflows/manual-freeze-requirements.yml new file mode 100644 index 000000000..ff958bbb2 --- /dev/null +++ b/.github/workflows/manual-freeze-requirements.yml @@ -0,0 +1,38 @@ +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +name: Freeze-requirements + +on: + workflow_dispatch: + +jobs: + freeze-requirements: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.ref }} + + - uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Set up Git + run: | + git config --global user.name "NeuralChatBot" + git config --global user.email "grp_neural_chat_bot@intel.com" + git remote set-url origin https://NeuralChatBot:"${{ secrets.ACTION_TOKEN }}"@github.com/opea-project/GenAIComps.git + + - name: Run script + run: | + bash .github/workflows/scripts/freeze_requirements.sh + + - name: Commit changes + run: | + git add . + git commit -m "Freeze requirements" + git push diff --git a/.github/workflows/scripts/freeze_requirements.sh b/.github/workflows/scripts/freeze_requirements.sh new file mode 100644 index 000000000..7c3b1d18a --- /dev/null +++ b/.github/workflows/scripts/freeze_requirements.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +function freeze() { + local file=$1 + local folder=$(dirname "$file") + local keep_origin_packages="true" + echo "::group::Check $file ..." + pip-compile \ + --no-upgrade \ + --no-annotate \ + --no-header \ + --output-file "$folder/freeze.txt" \ + "$file" + echo "::endgroup::" + if [[ -e "$folder/freeze.txt" ]]; then + if [[ "$keep_origin_packages" == "true" ]]; then + sed -i '/^\s*#/d; s/#.*//; /^\s*$/d' "$file" + sed -i '/^\s*#/d; s/#.*//; /^\s*$/d' "$folder/freeze.txt" + + packages1=$(cut -d'=' -f1 "$file" | tr '[:upper:]' '[:lower:]' | sed 's/[-_]/-/g') + packages2=$(cut -d'=' -f1 "$folder/freeze.txt" | tr '[:upper:]' '[:lower:]' | sed 's/[-_]/-/g') + common_packages=$(comm -12 <(echo "$packages2" | sort) <(echo "$packages1" | sort)) + + rm "$file" + while IFS= read -r line; do + package=$(echo "$line" | cut -d'=' -f1) + package_transformed=$(echo "$package" | tr '[:upper:]' '[:lower:]' | sed 's/[_-]/-/g') + pattern=$(echo "$package_transformed" | sed 's/\[/\\\[/g; s/\]/\\\]/g') + if echo "$common_packages" | grep -q "^$pattern$"; then + echo "$line" >>"$file" + fi + done <"$folder/freeze.txt" + rm "$folder/freeze.txt" + else + mv "$folder/freeze.txt" "$file" + fi + fi +} + +function check_branch_name() { + if [[ "$GITHUB_REF_NAME" == "main" ]]; then + echo "$GITHUB_REF_NAME is protected branch" + exit 0 + else + echo "branch name is $GITHUB_REF_NAME" + fi +} + +function main() { + check_branch_name + echo "::group::pip install pip-tools" && pip install pip-tools --upgrade && echo "::endgroup::" + export -f freeze + find . -name "requirements.txt" | xargs -n 1 -I {} bash -c 'freeze "$@"' _ {} +} + +main diff --git a/.gitignore b/.gitignore index bee8a64b7..567994ceb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +*.egg-info/