Skip to content

Commit

Permalink
test-upload-flow - Delete the test upload flow branch once the job is…
Browse files Browse the repository at this point in the history
… completed (#32323)

test-upload-flow - Delete the test upload flow branch once the job is completed #32323
  • Loading branch information
kobymeir committed Jan 22, 2024
1 parent 6618214 commit 58948cd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
13 changes: 12 additions & 1 deletion .gitlab/ci/.gitlab-ci.on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,9 @@ test-upload-flow:

- section_start "Create Testing Branch"
- export BRANCH="${CI_COMMIT_BRANCH}-upload_test_branch-${CI_PIPELINE_ID}"
- python3 ./Utils/test_upload_flow/create_test_branch.py -tb $BRANCH -a "${ARTIFACTS_FOLDER}" -g $GITLAB_PUSH_TOKEN
- echo "${BRANCH}" > "${ARTIFACTS_FOLDER}/test_upload_flow_branch.txt"
- python3 ./Utils/test_upload_flow/create_test_branch.py -tb "${BRANCH}" -a "${ARTIFACTS_FOLDER}" -g "${GITLAB_PUSH_TOKEN}"

- echo "Created test branch:${BRANCH}"
- section_end "Create Testing Branch"

Expand All @@ -760,6 +762,15 @@ test-upload-flow:
- python3 ./Utils/test_upload_flow/verify_bucket.py -a "${ARTIFACTS_FOLDER}" -s $GCS_MARKET_KEY -sb $current_storage_base_path -b $ALL_BUCKETS
- section_end "Verify Created Testing Bucket"
- job-done
after_script:
- !reference [.default-after-script]
- section_start "Delete Testing Branch"
- |
if [ -f "${ARTIFACTS_FOLDER}/test_upload_flow_branch.txt" ]; then
BRANCH=$(cat "${ARTIFACTS_FOLDER}/test_upload_flow_branch.txt")
python3 ./Utils/test_upload_flow/delete_test_branch.py -tb "${BRANCH}" -g "${GITLAB_PUSH_TOKEN}"
fi
- section_end "Delete Testing Branch"

.server_test_playbooks_results:
stage: results
Expand Down
8 changes: 8 additions & 0 deletions Tests/scripts/stop_running_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def delete_all_instances_for_pipeline(instances_service: InstancesService,
logging.info(f"No instances to delete for pipeline:{pipeline_id}")


def delete_test_upload_flow_branch(project: Project, test_upload: str):
with contextlib.suppress(Exception):
logging.debug(f"Deleting branch:{test_upload}")
project.branches.delete(test_upload)
logging.success(f"Successfully deleted branch:{test_upload}")


def cancel_pipelines_for_branch_name(gitlab_client: Gitlab,
project: Project,
branch_name: str,
Expand Down Expand Up @@ -112,6 +119,7 @@ def cancel_pipelines_for_branch_name(gitlab_client: Gitlab,
logging.info(f"Trying to cancel pipeline for test upload flow branch:{test_upload}")
success &= cancel_pipelines_for_branch_name(gitlab_client, project, test_upload, "trigger",
instances_service, all_instances, False)
delete_test_upload_flow_branch(project, test_upload)
elif pipeline.id == pipeline_id:
logging.info(f"Pipeline {pipeline.id} was not canceled as it is the current pipeline")
else:
Expand Down
46 changes: 46 additions & 0 deletions Utils/test_upload_flow/delete_test_branch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import argparse

from git import GitCommandError, Repo

from Tests.scripts.utils import logging_wrapper as logging
from Tests.scripts.utils.log_util import install_logging
from Utils.github_workflow_scripts.utils import get_env_var

GITLAB_SERVER_HOST = get_env_var('CI_SERVER_HOST', 'gitlab.xdr.pan.local') # disable-secrets-detection
GITLAB_PROJECT_NAMESPACE = get_env_var('CI_PROJECT_NAMESPACE', 'xdr/cortex-content') # disable-secrets-detection


def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--path", nargs="?", default='.',
help="Content directory path, default is current directory.")
parser.add_argument("-tb", "--test-branch", nargs="?",
help="The content test branch name to delete.")
parser.add_argument("-g", "--gitlab-token",
help="Gitlab token for deleting the test branch.")
return parser.parse_args()


def main():
install_logging('delete_test_branch.log', logger=logging)

args = parse_arguments()
repo = Repo(args.path)
branch = args.test_branch

try:

logging.info(f"Start deleting branch: '{branch}'")
repo.git.push('--set-upstream',
f'https://GITLAB_PUSH_TOKEN:{args.gitlab_token}@' # disable-secrets-detection
f'{GITLAB_SERVER_HOST}/{GITLAB_PROJECT_NAMESPACE}/content.git', # disable-secrets-detection
f":{branch}")

logging.info("Successfully deleted branch.")

except GitCommandError as e:
logging.error(e)


if __name__ == "__main__":
main()

0 comments on commit 58948cd

Please sign in to comment.