From 7a176c0cdd391d1d3de92434fd4c4d12414edf2b Mon Sep 17 00:00:00 2001 From: hoangvv Date: Fri, 10 Jan 2025 09:47:19 +0700 Subject: [PATCH] update --- .github/workflows/setup_docker.yml | 2 +- setup/clean_up_virtualenvs.sh | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/.github/workflows/setup_docker.yml b/.github/workflows/setup_docker.yml index 90b431c77..fd342883e 100644 --- a/.github/workflows/setup_docker.yml +++ b/.github/workflows/setup_docker.yml @@ -24,7 +24,7 @@ jobs: with: ref: ${{ github.ref_name }} - name: Create Virtual Environment - run: /home/nextzen/.pyenv/bin/pyenv virtualenv "${{ github.ref_name }}-$(git rev-parse --short "$GITHUB_SHA")" + run: pyenv virtualenv "${{ github.ref_name }}-$(git rev-parse --short "$GITHUB_SHA")" - name: Install dotenv run: pip install python-dotenv diff --git a/setup/clean_up_virtualenvs.sh b/setup/clean_up_virtualenvs.sh index 86613e6d8..0224b8465 100755 --- a/setup/clean_up_virtualenvs.sh +++ b/setup/clean_up_virtualenvs.sh @@ -3,15 +3,25 @@ # Get the current branch name branch_name=$(git rev-parse --abbrev-ref HEAD) -# Get a list of all virtual environments, filtering out duplicates -virtualenvs=$(pyenv virtualenvs | awk '{print $1}' | sort -u) +# Get a list of all virtual environments, filtering out duplicates and those not containing the branch name +virtualenvs=$(pyenv virtualenvs | awk '{print $1}' | sort -u | grep "$branch_name") -# Loop through each virtual environment and delete it if it contains the branch name -for venv in $virtualenvs; do - if [[ "$venv" == *"$branch_name"* ]]; then +# Count the number of virtual environments +count=$(echo "$virtualenvs" | wc -l) + +# Calculate how many virtual environments to keep +keep_count=$((count - $1)) + +# If there are more than 3 virtual environments, delete the oldest ones +if (( keep_count > 0 )); then + # Get the oldest virtual environments (assuming they are listed first) + oldest_venvs=$(echo "$virtualenvs" | head -n "$keep_count") + + # Loop through the oldest virtual environments and delete them + for venv in $oldest_venvs; do echo "Deleting virtual environment: $venv" pyenv virtualenv-delete "$venv" -f - fi -done + done +fi -echo "Virtual environments containing '$branch_name' deleted." \ No newline at end of file +echo "Old virtual environments containing '$branch_name' deleted." \ No newline at end of file