Last active
March 15, 2022 14:59
-
-
Save wattry/f94b61766f25f6ead16a663bedab40ee to your computer and use it in GitHub Desktop.
Gitlab bash script to destroy a feature branch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
script: | |
# Try clone the branch, if it does not exist then try clone dev. | |
- | | |
REPO_URL="https://gitlab-ci-token:${CI_JOB_TOKEN}@code.vt.edu/${CI_PROJECT_PATH}.git" | |
RED="\033[31m" | |
BOLD="\033[1m" | |
GREEN="\033[32m" | |
echo $TF_ROOT | |
# Try clone the branch that was stopped if we can't then clone dev and do the destory. | |
if $(git clone $REPO_URL -b $CI_COMMIT_REF_NAME); then | |
echo -e "$GREEN $BOLD Using unmerged branch" | |
echo ls -la | |
cd $TF_ROOT | |
else | |
echo -e "$GREEN $BOLD FB merge or DELETED unmerged Branch" | |
git clone $REPO_URL -b $CI_DEFAULT_BRANCH | |
cd $TF_ROOT | |
# If we can checkout the commit from the merge then we have merged into dev, else we will let the script attempt the destroy | |
if $(git checkout -b $CI_COMMIT_REF_NAME $CI_COMMIT_SHA); then | |
echo -e "$GREEN $BOLD Checked out branch (${CI_COMMIT_REF_NAME}) from last commit (${CI_COMMIT_SHA})" | |
else | |
echo -e "$RED $BOLD ***Attempting to delete branch without original pipeline and TF code***" | |
echo -e "$RED $BOLD ***If this fails push the deleted branch and run destroy step manually***" | |
echo -e "$RED $BOLD ***Lastly delete the branch***" | |
fi | |
fi | |
# If the plan succeeds then run the destroy else create an issue on the project and tag the user. | |
if $(gitlab-terraform plan -destroy); then | |
gitlab-terraform apply -destroy | |
curl -H "Private-Token: ${TERRAFORM_STATE_CI_JOB_TOKEN}" -X DELETE "${TF_ADDRESS}" | |
else | |
echo "Unable to perform delete notifying developer" | |
# Merge the branch into dev to update the pipeline. | |
CREATE_REQUEST="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/repository/branches?branch=${CI_COMMIT_REF_NAME}&ref=${CI_DEFAULT_BRANCH}" | |
curl -X POST -H "Private-Token: ${TERRAFORM_STATE_CI_JOB_TOKEN}" "${CREATE_REQUEST}" | |
# Notify the developer to manually remove the branch again. | |
TITLE="title=TF%20Destroy%20Failed%3A%20Branch%20-%20${CI_COMMIT_REF_NAME}%20for%20User%20-%20${GITLAB_USER_NAME}" | |
DESCRIPTION="description=%40${GITLAB_USER_NAME}%20Pipeline%20failed%20${CI_PIPELINE_URL}%20please%20delete%20the%20branch%20again" | |
ISSUE_TYPE="issue_type=incident&labels=failure,${GITLAB_USER_NAME},manual%20deletion" | |
USER="assignee_id=${GITLAB_USER_ID}" | |
ISSUE_REQUEST="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/issues?${TITLE}&${DESCRIPTION}&${ISSUE_TYPE}&${USER}" | |
curl -X POST -H "Private-Token: ${TERRAFORM_STATE_CI_JOB_TOKEN}" "${ISSUE_REQUEST}" | |
exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment