Skip to content

Commit

Permalink
update script and wf
Browse files Browse the repository at this point in the history
  • Loading branch information
gurevichdmitry committed Jun 17, 2024
1 parent e4b25ce commit c8553c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
44 changes: 30 additions & 14 deletions .ci/scripts/get_expired_envs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,39 @@

# Function to check if a file exists in a folder
file_exists() {
local bucket_name="$1"
local bucket_key="$2"
local bucket_path="$1"
local bucket_exists
# Check if the file exists in the bucket
if aws s3api head-object --bucket "$bucket_name" --key "$bucket_key" >/dev/null 2>&1; then
return 0
bucket_exists=$(aws s3 ls "$bucket_path")
if [ -z "$bucket_exists" ]; then
return 1
fi
return 1
return 0
}

# Get all folders from the specific S3 bucket
s3_bucket="tf-state-bucket-test-infra"
folders=$(aws s3 ls "s3://$s3_bucket" | awk '{print $2}')
folders=$(aws s3 ls "s3://${s3_bucket}" | awk '{print $2}')

# JSON array to store deployment names
deployment_json='[]'
deployments_json='[]'

# Boolean variable to track if an expired environment was found
expired_env_found=false

# Iterate over each folder
for folder in $folders; do
# Check if env_config.json file exists
if ! file_exists "$s3_bucket" "$folder/env_config.json"; then
echo "env_config.json file does not exist in $folder"
if ! file_exists "s3://${s3_bucket}/${folder}env_config.json"; then
# TODO: To consider if we want to log this message
# echo "env_config.json file does not exist in $folder"
continue
fi

file_content=$(aws s3 cp s3://"$s3_bucket"/"$folder"/env_config.json -)
# TODO: To consider if we want to log this message
# echo "Reading env_config.json file from $folder"

file_content=$(aws s3 cp "s3://${s3_bucket}/${folder}env_config.json" -)

# Read expiration date from env_config.json
expiration=$(echo "$file_content" | jq -r '.expiration')
Expand All @@ -36,11 +44,19 @@ for folder in $folders; do
if [[ ! "$expiration" > "$current_date" ]]; then
# Extract the deployment_name field using jq
deployment_name=$(echo "$file_content" | jq -r '.deployment_name')

# TODO: To consider if we want to log this message
echo "Environment $deployment_name is expired."
# Add deployment name to JSON object
deployment_json=$(echo "$deployment_json" | jq --arg value "$deployment_name" '. += [{"deployment_name": $value}]')
deployments_json=$(echo "$deployments_json" | jq --arg value "$deployment_name" '. += [{"deployment_name": $value}]')

# Set found to true
expired_env_found=true
else
# TODO: To consider if we want to log this message
echo "Environment $(echo "$file_content" | jq -r '.deployment_name') is not expired yet."
fi
done

# Print the deployment names to GITHUB_OUTPUT
echo "deployments=$(echo "$deployment_json" | jq .)" >>"$GITHUB_OUTPUT"
# Print the deployment names and found status to GITHUB_OUTPUT
echo "deployments=$(echo "$deployments_json" | jq .)" >>"$GITHUB_OUTPUT"
echo "expired_env_found=$expired_env_found" >>"$GITHUB_OUTPUT"
4 changes: 3 additions & 1 deletion .github/workflows/destroy-expired-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.get_expired_envs.outputs.deployments }}
env_found: ${{ steps.get_expired_envs.outputs.expired_env_found }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down Expand Up @@ -54,7 +55,8 @@ jobs:
# TODO: Remove after debugging env_to_destroy action
runs-on: ubuntu-latest
needs: ["envs_to_destroy"]
if: ${{ success() && needs.envs_to_destroy.outputs.matrix != '' }}
# Only run if there are environments to destroy
if: ${{ needs.envs_to_destroy.outputs.env_found == 'true' }}
strategy:
fail-fast: false
max-parallel: 1 # Destroy environments one by one
Expand Down

0 comments on commit c8553c6

Please sign in to comment.