From c6fb8df994ae21d43210278eb734e93e3f79c58e Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Thu, 6 Aug 2026 20:47:40 +0300 Subject: [PATCH 01/16] feat: add review envnironment clean up workflow --- .github/workflows/clean-up-on-pr-merged.yml | 57 +++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 .github/workflows/clean-up-on-pr-merged.yml diff --git a/.github/workflows/clean-up-on-pr-merged.yml b/.github/workflows/clean-up-on-pr-merged.yml new file mode 100644 index 00000000..86cb9984 --- /dev/null +++ b/.github/workflows/clean-up-on-pr-merged.yml @@ -0,0 +1,57 @@ +name: Destroy Review Environment on PR Merge + +on: + pull_request: + types: + - closed + inputs: + runs-on: + type: string + description: Overrides jobs runs-on settings (json-encoded list) + default: '["ubuntu-24.04"]' + +jobs: + destroy-gitlab-environment: + name: Destroy GitLab Review Environment + # Only run when PR is actually merged, not just closed + if: github.event.pull_request.merged == false ### temporary use for not merging + runs-on: ${{ fromJSON(inputs.runs-on) }} + + steps: + - name: Destroy review environment on PR merge + env: + GITHUB_PR_ID: ${{ github.event.pull_request.number }} + GITHUB_REPO: ${{ github.repository }} + GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN_TEST_DESTROY }} + shell: bash + run: | + echo "$GITHUB_PR_ID" + # remove "epam/" prefix from the repository name to match the GitLab environment name + $GITHUB_REPO=${GITHUB_REPO:5} + echo "$GITHUB_REPO" + + # check if the review environment is already stopped + CURRENT_ENV_STATE=$(curl --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ + --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ + | jq -r '.[0].state') + + if [ "$CURRENT_ENV_STATE" == "stopped" ]; then + echo "Review environment for repository epam/$GITHUB_REPO pr-$GITHUB_PR_ID is already stopped." + else + #get the environment id base on the environment name + GITLAB_ENV_ID=$(curl --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ + --url "https://gitlab.deltixhub.com/api/v4/projects//environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ + | jq -r '.[0].id') + + echo "$GITLAB_ENV_ID" + + echo "Stopping review environment... " + + #stop the environment based on the environment id + STATE=$(curl --request POST \ + --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ + --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments/${GITLAB_ENV_ID}/stop" \ + | jq -r '.state') + + echo "Review environment for repostory epam/$GITHUB_REPO pr-$GITHUB_PR_ID has been $STATE" + fi From c0b9d4cd5c4f3c995cfab24a93ade4140541c837 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 7 Aug 2026 12:13:05 +0300 Subject: [PATCH 02/16] feat: add review envnironment clean up workflow --- .github/workflows/clean-up-on-pr-merged.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/clean-up-on-pr-merged.yml b/.github/workflows/clean-up-on-pr-merged.yml index 86cb9984..57d3496d 100644 --- a/.github/workflows/clean-up-on-pr-merged.yml +++ b/.github/workflows/clean-up-on-pr-merged.yml @@ -1,9 +1,7 @@ name: Destroy Review Environment on PR Merge on: - pull_request: - types: - - closed + workflow_call: inputs: runs-on: type: string From d826b7dd8d22f49e85503066cb74f2b1fa0a3714 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 7 Aug 2026 13:10:16 +0300 Subject: [PATCH 03/16] fix: rename file and fix typo --- ...clean-up-on-pr-merged.yml => cleanup-review-environment.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{clean-up-on-pr-merged.yml => cleanup-review-environment.yml} (95%) diff --git a/.github/workflows/clean-up-on-pr-merged.yml b/.github/workflows/cleanup-review-environment.yml similarity index 95% rename from .github/workflows/clean-up-on-pr-merged.yml rename to .github/workflows/cleanup-review-environment.yml index 57d3496d..d4b0e1fe 100644 --- a/.github/workflows/clean-up-on-pr-merged.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -25,7 +25,7 @@ jobs: run: | echo "$GITHUB_PR_ID" # remove "epam/" prefix from the repository name to match the GitLab environment name - $GITHUB_REPO=${GITHUB_REPO:5} + GITHUB_REPO=${GITHUB_REPO:5} echo "$GITHUB_REPO" # check if the review environment is already stopped From 26410fb483af70af05603f28027cd28c44de5928 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 7 Aug 2026 14:20:01 +0300 Subject: [PATCH 04/16] Enhance logging in cleanup workflow for review environment Added logging for current review environment status and GitLab environment ID. --- .github/workflows/cleanup-review-environment.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index d4b0e1fe..75f5c8c5 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -23,16 +23,16 @@ jobs: GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN_TEST_DESTROY }} shell: bash run: | - echo "$GITHUB_PR_ID" # remove "epam/" prefix from the repository name to match the GitLab environment name GITHUB_REPO=${GITHUB_REPO:5} - echo "$GITHUB_REPO" # check if the review environment is already stopped CURRENT_ENV_STATE=$(curl --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ | jq -r '.[0].state') - + + echo "Current review environment status is ${CURRENT_ENV_STATE} + if [ "$CURRENT_ENV_STATE" == "stopped" ]; then echo "Review environment for repository epam/$GITHUB_REPO pr-$GITHUB_PR_ID is already stopped." else @@ -41,7 +41,7 @@ jobs: --url "https://gitlab.deltixhub.com/api/v4/projects//environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ | jq -r '.[0].id') - echo "$GITLAB_ENV_ID" + echo "Gitlab review environment id: ${GITLAB_ENV_ID}" echo "Stopping review environment... " From 51c8e810c186d025eb1fd7accb135765ce836d8d Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 7 Aug 2026 14:25:35 +0300 Subject: [PATCH 05/16] Refactor cleanup workflow for review environments --- .github/workflows/cleanup-review-environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index 75f5c8c5..f015340b 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -31,7 +31,7 @@ jobs: --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ | jq -r '.[0].state') - echo "Current review environment status is ${CURRENT_ENV_STATE} + echo "Current review environment status is ${CURRENT_ENV_STATE}" if [ "$CURRENT_ENV_STATE" == "stopped" ]; then echo "Review environment for repository epam/$GITHUB_REPO pr-$GITHUB_PR_ID is already stopped." From 05c62eebfdac38cbd978dda565956e52d44897f2 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 7 Aug 2026 14:34:15 +0300 Subject: [PATCH 06/16] Refactor cleanup workflow for review environments --- .github/workflows/cleanup-review-environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index f015340b..751fabe1 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -38,7 +38,7 @@ jobs: else #get the environment id base on the environment name GITLAB_ENV_ID=$(curl --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ - --url "https://gitlab.deltixhub.com/api/v4/projects//environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ + --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ | jq -r '.[0].id') echo "Gitlab review environment id: ${GITLAB_ENV_ID}" From 9f70660d4b9df836a0e090ee49dbd3429236166f Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 7 Aug 2026 16:30:19 +0300 Subject: [PATCH 07/16] Refactor cleanup workflow for review environment Updated the cleanup workflow to improve efficiency and clarity in stopping the review environment. --- .github/workflows/cleanup-review-environment.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index 751fabe1..f17650a3 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -11,8 +11,6 @@ on: jobs: destroy-gitlab-environment: name: Destroy GitLab Review Environment - # Only run when PR is actually merged, not just closed - if: github.event.pull_request.merged == false ### temporary use for not merging runs-on: ${{ fromJSON(inputs.runs-on) }} steps: @@ -27,17 +25,17 @@ jobs: GITHUB_REPO=${GITHUB_REPO:5} # check if the review environment is already stopped - CURRENT_ENV_STATE=$(curl --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ + CURRENT_ENV_STATE=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ | jq -r '.[0].state') echo "Current review environment status is ${CURRENT_ENV_STATE}" if [ "$CURRENT_ENV_STATE" == "stopped" ]; then - echo "Review environment for repository epam/$GITHUB_REPO pr-$GITHUB_PR_ID is already stopped." + exit 0 else #get the environment id base on the environment name - GITLAB_ENV_ID=$(curl --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ + GITLAB_ENV_ID=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ | jq -r '.[0].id') @@ -46,7 +44,7 @@ jobs: echo "Stopping review environment... " #stop the environment based on the environment id - STATE=$(curl --request POST \ + STATE=$(curl -s --request POST \ --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments/${GITLAB_ENV_ID}/stop" \ | jq -r '.state') From ec1fe0598ed2a2e03e5fd92ec28e79ab3a738cb6 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 7 Aug 2026 17:05:35 +0300 Subject: [PATCH 08/16] Refactor cleanup workflow for review environments Updated the cleanup workflow to include environment data retrieval and confirmation of environment stop status. --- .../workflows/cleanup-review-environment.yml | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index f17650a3..f750689e 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -21,33 +21,39 @@ jobs: GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN_TEST_DESTROY }} shell: bash run: | - # remove "epam/" prefix from the repository name to match the GitLab environment name + # Remove "epam/" prefix from the repository name to match the GitLab environment name GITHUB_REPO=${GITHUB_REPO:5} - # check if the review environment is already stopped - CURRENT_ENV_STATE=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ - --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ - | jq -r '.[0].state') + # Get environment data + ENV_DATA=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ + --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID") + # Get environment current state + CURRENT_ENV_STATE=$(echo "$ENV_DATA" | jq -r '.[0].state') echo "Current review environment status is ${CURRENT_ENV_STATE}" - if [ "$CURRENT_ENV_STATE" == "stopped" ]; then + if [ "$CURRENT_ENV_STATE" = "stopped" ]; then exit 0 else - #get the environment id base on the environment name - GITLAB_ENV_ID=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ - --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ - | jq -r '.[0].id') - + # Get the environment id base on the environment name + GITLAB_ENV_ID=$(echo "$ENV_DATA" | jq -r '.[0].id') echo "Gitlab review environment id: ${GITLAB_ENV_ID}" echo "Stopping review environment... " #stop the environment based on the environment id - STATE=$(curl -s --request POST \ + curl -s --request POST \ --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ - --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments/${GITLAB_ENV_ID}/stop" \ - | jq -r '.state') - - echo "Review environment for repostory epam/$GITHUB_REPO pr-$GITHUB_PR_ID has been $STATE" + --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments/${GITLAB_ENV_ID}/stop" + + #Confirm is review environment is stopped + while [ "$CURRENT_ENV_STATE" != "stopped" ] + do + echo "Waiting for environment to stop..." + sleep 60 + CURRENT_ENV_STATE=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ + --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments/${GITLAB_ENV_ID}" \ + | jq -r '.[0].state') + done + echo "Environment stopped successfully." fi From fdd940f559468729a203d8a5cd5ac0ffe63a2acb Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Mon, 10 Aug 2026 11:46:02 +0300 Subject: [PATCH 09/16] Enhance cleanup workflow with environment existence check Added a check for existing review environments before attempting to stop them. --- .github/workflows/cleanup-review-environment.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index f750689e..97746a0e 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -27,14 +27,20 @@ jobs: # Get environment data ENV_DATA=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID") - + + # Check if review environment exists + if [ -z "$ENV_DATA" ]; then + echo "Review environment hasn't been created" + exit 0 + fi + # Get environment current state CURRENT_ENV_STATE=$(echo "$ENV_DATA" | jq -r '.[0].state') echo "Current review environment status is ${CURRENT_ENV_STATE}" if [ "$CURRENT_ENV_STATE" = "stopped" ]; then exit 0 - else + elif [ "$CURRENT_ENV_STATE" = "available" ]; then # Get the environment id base on the environment name GITLAB_ENV_ID=$(echo "$ENV_DATA" | jq -r '.[0].id') echo "Gitlab review environment id: ${GITLAB_ENV_ID}" From c4ba20ca15e25bc1df8072e14f74ce25c144384f Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Mon, 10 Aug 2026 12:20:28 +0300 Subject: [PATCH 10/16] Refactor cleanup workflow for review environments --- .github/workflows/cleanup-review-environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index 97746a0e..89019c0e 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -58,7 +58,7 @@ jobs: echo "Waiting for environment to stop..." sleep 60 CURRENT_ENV_STATE=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ - --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments/${GITLAB_ENV_ID}" \ + --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ | jq -r '.[0].state') done echo "Environment stopped successfully." From 7e5809f6b8afc2ea1af52a15578517c28ce12a73 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 28 Aug 2026 12:53:28 +0300 Subject: [PATCH 11/16] change trigger --- .github/workflows/cleanup-review-environment.yml | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index 89019c0e..51458f1f 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -1,17 +1,18 @@ name: Destroy Review Environment on PR Merge on: - workflow_call: - inputs: - runs-on: - type: string - description: Overrides jobs runs-on settings (json-encoded list) - default: '["ubuntu-24.04"]' + repository_dispatch: + types: [cleanup-review-environment] jobs: destroy-gitlab-environment: name: Destroy GitLab Review Environment - runs-on: ${{ fromJSON(inputs.runs-on) }} + runs-on: ubuntu-latest + environment: + name: e2e-test + outputs: + github-app: ${{ github.event.client_payload.github-app }} + github-pr: ${{ github.event.client_payload.github-pr }} steps: - name: Destroy review environment on PR merge From c38ecd8882b996d923a4b2c0ccc7e742114780a9 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 28 Aug 2026 13:13:43 +0300 Subject: [PATCH 12/16] update environment name --- .github/workflows/cleanup-review-environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index 51458f1f..15ebbc77 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -9,7 +9,7 @@ jobs: name: Destroy GitLab Review Environment runs-on: ubuntu-latest environment: - name: e2e-test + name: dial-review outputs: github-app: ${{ github.event.client_payload.github-app }} github-pr: ${{ github.event.client_payload.github-pr }} From 7dec968cda2de73372d4387d847f0bd074eefd84 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Mon, 31 Aug 2026 14:04:24 +0300 Subject: [PATCH 13/16] change env names form payload --- .github/workflows/cleanup-review-environment.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index 15ebbc77..a876896d 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -17,9 +17,9 @@ jobs: steps: - name: Destroy review environment on PR merge env: - GITHUB_PR_ID: ${{ github.event.pull_request.number }} - GITHUB_REPO: ${{ github.repository }} - GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN_TEST_DESTROY }} + APP: ${{ github.event.client_payload.github-app }} + PR_ID: ${{ github.event.pull_request.number }} + DESTROY_TOKEN: ${{ secrets.DESTROY_ACCESS_TOKEN }} shell: bash run: | # Remove "epam/" prefix from the repository name to match the GitLab environment name From f5e321f30f7d7f117904dc0c8557848c8917bc4b Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Thu, 3 Sep 2026 13:12:25 +0300 Subject: [PATCH 14/16] update script --- .../workflows/cleanup-review-environment.yml | 57 +++++++------------ 1 file changed, 19 insertions(+), 38 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index a876896d..aad1bcc1 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -19,48 +19,29 @@ jobs: env: APP: ${{ github.event.client_payload.github-app }} PR_ID: ${{ github.event.pull_request.number }} + GITLAB_HOST: ${{ vars.DEPLOY_HOST }} DESTROY_TOKEN: ${{ secrets.DESTROY_ACCESS_TOKEN }} + GL_PROJECT_ID: ${{ vars.DEPLOY_PROJECT_ID }} shell: bash run: | - # Remove "epam/" prefix from the repository name to match the GitLab environment name - GITHUB_REPO=${GITHUB_REPO:5} + # Get environment name to get gitlab environment ID + ENV_NAME="$APP/pr-$PR_ID" - # Get environment data - ENV_DATA=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ - --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID") + GL_API_URL="${GITLAB_HOST}/api/v4/projects/${GITLAB_PROJECT_ID}/environments" + curl_gl_api() { curl -sS --fail-with-body --retry 5 --retry-all-errors --retry-delay 3 \ + -H "PRIVATE-TOKEN: $DESTROY_TOKEN" "$@"; } - # Check if review environment exists - if [ -z "$ENV_DATA" ]; then - echo "Review environment hasn't been created" - exit 0 - fi - - # Get environment current state - CURRENT_ENV_STATE=$(echo "$ENV_DATA" | jq -r '.[0].state') - echo "Current review environment status is ${CURRENT_ENV_STATE}" - - if [ "$CURRENT_ENV_STATE" = "stopped" ]; then - exit 0 - elif [ "$CURRENT_ENV_STATE" = "available" ]; then - # Get the environment id base on the environment name - GITLAB_ENV_ID=$(echo "$ENV_DATA" | jq -r '.[0].id') - echo "Gitlab review environment id: ${GITLAB_ENV_ID}" + ENV_ID=$(curl_gl_api --get --url-query "name=$ENV_NAME" "GL_API_URL" | jq -r '.[0].id // empty') + [ -n "$ENV_ID" ] || { echo "::notice::No environment '$ENV_NAME'"; exit 0; } - echo "Stopping review environment... " + # Get current environment state + STATE=$(curl_gl_api -X POST "$GL_API_URL/$ENV_ID/stop" | jq -r '.state') - #stop the environment based on the environment id - curl -s --request POST \ - --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ - --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments/${GITLAB_ENV_ID}/stop" - - #Confirm is review environment is stopped - while [ "$CURRENT_ENV_STATE" != "stopped" ] - do - echo "Waiting for environment to stop..." - sleep 60 - CURRENT_ENV_STATE=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_ACCESS_TOKEN" \ - --url "https://gitlab.deltixhub.com/api/v4/projects/1779/environments?name=$GITHUB_REPO/pr-$GITHUB_PR_ID" \ - | jq -r '.[0].state') - done - echo "Environment stopped successfully." - fi + # Check environment state + DEADLINE=$((SECONDS + 600)) + while [ "$STATE" = "stopping" ]; do + [ "$SECONDS" -lt "$DEADLINE" ] || { echo "::error::timed out in 'stopping'"; exit 1; } + sleep 15 + STATE=$(curl_gl_api "$GL_API_URL/$ENV_ID" | jq -r '.state') + done + [ "$STATE" = "stopped" ] || { echo "::error::unexpected state '$STATE'"; exit 1; } \ No newline at end of file From c09f05b20fbc69ba71bd741dbde3920ba90859bc Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Thu, 3 Sep 2026 13:30:47 +0300 Subject: [PATCH 15/16] fix issues --- .github/workflows/cleanup-review-environment.yml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index aad1bcc1..8d716136 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -10,16 +10,12 @@ jobs: runs-on: ubuntu-latest environment: name: dial-review - outputs: - github-app: ${{ github.event.client_payload.github-app }} - github-pr: ${{ github.event.client_payload.github-pr }} - steps: - name: Destroy review environment on PR merge env: APP: ${{ github.event.client_payload.github-app }} - PR_ID: ${{ github.event.pull_request.number }} - GITLAB_HOST: ${{ vars.DEPLOY_HOST }} + PR_ID: ${{ github.event.client_payload.github-pr }} + GL_HOST: ${{ vars.DEPLOY_HOST }} DESTROY_TOKEN: ${{ secrets.DESTROY_ACCESS_TOKEN }} GL_PROJECT_ID: ${{ vars.DEPLOY_PROJECT_ID }} shell: bash @@ -27,11 +23,11 @@ jobs: # Get environment name to get gitlab environment ID ENV_NAME="$APP/pr-$PR_ID" - GL_API_URL="${GITLAB_HOST}/api/v4/projects/${GITLAB_PROJECT_ID}/environments" + GL_API_URL="${GL_HOST}/api/v4/projects/${GL_PROJECT_ID}/environments" curl_gl_api() { curl -sS --fail-with-body --retry 5 --retry-all-errors --retry-delay 3 \ -H "PRIVATE-TOKEN: $DESTROY_TOKEN" "$@"; } - ENV_ID=$(curl_gl_api --get --url-query "name=$ENV_NAME" "GL_API_URL" | jq -r '.[0].id // empty') + ENV_ID=$(curl_gl_api --get --url-query "name=$ENV_NAME" "$GL_API_URL" | jq -r '.[0].id // empty') [ -n "$ENV_ID" ] || { echo "::notice::No environment '$ENV_NAME'"; exit 0; } # Get current environment state From fe829723d9f5dee4e27f1da4fdb0b817bdd4e826 Mon Sep 17 00:00:00 2001 From: Dmytro Pashynskyi <61313167+dspashynskyi@users.noreply.github.com> Date: Fri, 4 Sep 2026 12:15:17 +0300 Subject: [PATCH 16/16] add final script state --- .github/workflows/cleanup-review-environment.yml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml index 8d716136..f9dc7e87 100644 --- a/.github/workflows/cleanup-review-environment.yml +++ b/.github/workflows/cleanup-review-environment.yml @@ -1,9 +1,13 @@ -name: Destroy Review Environment on PR Merge +name: Destroy Review Environment on PR closure on: repository_dispatch: types: [cleanup-review-environment] +concurrency: + group: cleanup-${{ github.event.client_payload.github-app }}-${{ github.event.client_payload.github-pr }} + cancel-in-progress: true + jobs: destroy-gitlab-environment: name: Destroy GitLab Review Environment @@ -15,11 +19,14 @@ jobs: env: APP: ${{ github.event.client_payload.github-app }} PR_ID: ${{ github.event.client_payload.github-pr }} - GL_HOST: ${{ vars.DEPLOY_HOST }} + GL_HOST: https://${{ vars.DEPLOY_HOST }} DESTROY_TOKEN: ${{ secrets.DESTROY_ACCESS_TOKEN }} GL_PROJECT_ID: ${{ vars.DEPLOY_PROJECT_ID }} shell: bash run: | + # Hack, pipeline to return the exit status of the last command to fail (instead of the last command). Exit immediately if any command returns a non-zero exit status. + set -euo pipefail + # Get environment name to get gitlab environment ID ENV_NAME="$APP/pr-$PR_ID" @@ -30,14 +37,15 @@ jobs: ENV_ID=$(curl_gl_api --get --url-query "name=$ENV_NAME" "$GL_API_URL" | jq -r '.[0].id // empty') [ -n "$ENV_ID" ] || { echo "::notice::No environment '$ENV_NAME'"; exit 0; } - # Get current environment state + # Stop environment state and get current state STATE=$(curl_gl_api -X POST "$GL_API_URL/$ENV_ID/stop" | jq -r '.state') # Check environment state DEADLINE=$((SECONDS + 600)) while [ "$STATE" = "stopping" ]; do [ "$SECONDS" -lt "$DEADLINE" ] || { echo "::error::timed out in 'stopping'"; exit 1; } + echo "The environment is still stopping, checking again..." sleep 15 STATE=$(curl_gl_api "$GL_API_URL/$ENV_ID" | jq -r '.state') done - [ "$STATE" = "stopped" ] || { echo "::error::unexpected state '$STATE'"; exit 1; } \ No newline at end of file + [ "$STATE" = "stopped" ] && { echo "Environment ${ENV_NAME} is stopped"; exit 0; } || { echo "::error::unexpected state '$STATE'"; exit 1; } \ No newline at end of file