Browse Source

Cleanup CI scripts (#12205)

* Delete unused scripts

- gen_tags.sh: not the right file, produce garbage even if path is fixed
- premoderator.sh: not used since ef6d24a49 (CI require a 'lgtm' or
  'ok-to-test' labels to pass (#11251), 2024-05-31)
- gitlab-branch-cleanup: unused AFAICT

* CI: inline molecule logs

Single use site -> less indirection makes it easier to read.
pull/12213/head
Max Gautier 4 months ago
committed by GitHub
parent
commit
373b952a0c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
8 changed files with 4 additions and 138 deletions
  1. 5
      .gitlab-ci/molecule.yml
  2. 12
      scripts/gen_tags.sh
  3. 2
      scripts/gitlab-branch-cleanup/.gitignore
  4. 24
      scripts/gitlab-branch-cleanup/README.md
  5. 38
      scripts/gitlab-branch-cleanup/main.py
  6. 1
      scripts/gitlab-branch-cleanup/requirements.txt
  7. 51
      scripts/premoderator.sh
  8. 9
      tests/scripts/molecule_logs.sh

5
.gitlab-ci/molecule.yml

@ -16,7 +16,10 @@
script: script:
- ./tests/scripts/molecule_run.sh - ./tests/scripts/molecule_run.sh
after_script: after_script:
- ./tests/scripts/molecule_logs.sh
- rm -fr molecule_logs
- mkdir -p molecule_logs
- find ~/.cache/molecule/ \( -name '*.out' -o -name '*.err' \) -type f | xargs tar -uf molecule_logs/molecule.tar
- gzip molecule_logs/molecule.tar
artifacts: artifacts:
when: always when: always
paths: paths:

12
scripts/gen_tags.sh

@ -1,12 +0,0 @@
#!/bin/sh
set -eo pipefail
#Generate MD formatted tags from roles and cluster yaml files
printf "|%25s |%9s\n" "Tag name" "Used for"
echo "|--------------------------|---------"
tags=$(grep -r tags: . | perl -ne '/tags:\s\[?(([\w\-_]+,?\s?)+)/ && printf "%s ", "$1"'|\
perl -ne 'print join "\n", split /\s|,/' | sort -u)
for tag in $tags; do
match=$(cat docs/ansible.md | perl -ne "/^\|\s+${tag}\s\|\s+((\S+\s?)+)/ && printf \$1")
printf "|%25s |%s\n" "${tag}" " ${match}"
done

2
scripts/gitlab-branch-cleanup/.gitignore

@ -1,2 +0,0 @@
openrc
venv

24
scripts/gitlab-branch-cleanup/README.md

@ -1,24 +0,0 @@
# gitlab-branch-cleanup
Cleanup old branches in a GitLab project
## Installation
```shell
pip install -r requirements.txt
python main.py --help
```
## Usage
```console
$ export GITLAB_API_TOKEN=foobar
$ python main.py kargo-ci/kubernetes-sigs-kubespray
Deleting branch pr-5220-containerd-systemd from 2020-02-17 ...
Deleting branch pr-5561-feature/cinder_csi_fixes from 2020-02-17 ...
Deleting branch pr-5607-add-flatcar from 2020-02-17 ...
Deleting branch pr-5616-fix-typo from 2020-02-17 ...
Deleting branch pr-5634-helm_310 from 2020-02-18 ...
Deleting branch pr-5644-patch-1 from 2020-02-15 ...
Deleting branch pr-5647-master from 2020-02-17 ...
```

38
scripts/gitlab-branch-cleanup/main.py

@ -1,38 +0,0 @@
import gitlab
import argparse
import os
import sys
from datetime import timedelta, datetime, timezone
parser = argparse.ArgumentParser(
description='Cleanup old branches in a GitLab project')
parser.add_argument('--api', default='https://gitlab.com/',
help='URL of GitLab API, defaults to gitlab.com')
parser.add_argument('--age', type=int, default=30,
help='Delete branches older than this many days')
parser.add_argument('--prefix', default='pr-',
help='Cleanup only branches with names matching this prefix')
parser.add_argument('--dry-run', action='store_true',
help='Do not delete anything')
parser.add_argument('project',
help='Path of the GitLab project')
args = parser.parse_args()
limit = datetime.now(timezone.utc) - timedelta(days=args.age)
if os.getenv('GITLAB_API_TOKEN', '') == '':
print("Environment variable GITLAB_API_TOKEN is required.")
sys.exit(2)
gl = gitlab.Gitlab(args.api, private_token=os.getenv('GITLAB_API_TOKEN'))
gl.auth()
p = gl.projects.get(args.project)
for b in p.branches.list(all=True):
date = datetime.fromisoformat(b.commit['created_at'])
if date < limit and not b.protected and not b.default and b.name.startswith(args.prefix):
print("Deleting branch %s from %s ..." %
(b.name, date.date().isoformat()))
if not args.dry_run:
b.delete()

1
scripts/gitlab-branch-cleanup/requirements.txt

@ -1 +0,0 @@
python-gitlab

51
scripts/premoderator.sh

@ -1,51 +0,0 @@
#!/bin/bash
# A naive premoderation script to allow Gitlab CI pipeline on a specific PRs' comment
# Exits with 0, if the pipeline is good to go
# Exits with 1, if the user is not allowed to start pipeline
# Exits with 2, if script is unable to get issue id from CI_COMMIT_REF_NAME variable
# Exits with 3, if missing the magic comment in the pipeline to start the pipeline
CURL_ARGS="-fs --retry 4 --retry-delay 5"
MAGIC="${MAGIC:-ci check this}"
exit_code=0
# Get PR number from CI_COMMIT_REF_NAME
issue=$(echo ${CI_COMMIT_REF_NAME} | perl -ne '/^pr-(\d+)-\S+$/ && print $1')
if [ "$issue" = "" ]; then
echo "Unable to get issue id from: $CI_COMMIT_REF_NAME"
exit 2
fi
echo "Fetching labels from PR $issue"
labels=$(curl ${CURL_ARGS} "https://api.github.com/repos/kubernetes-sigs/kubespray/issues/${issue}?access_token=${GITHUB_TOKEN}" | jq '{labels: .labels}' | jq '.labels[].name' | jq -s '')
labels_to_patch=$(echo -n $labels | jq '. + ["needs-ci-auth"]' | tr -d "\n")
echo "Checking for '$MAGIC' comment in PR $issue"
# Get the user name from the PR comments with the wanted magic incantation casted
user=$(curl ${CURL_ARGS} "https://api.github.com/repos/kubernetes-sigs/kubespray/issues/${issue}/comments" | jq -M "map(select(.body | contains (\"$MAGIC\"))) | .[0] .user.login" | tr -d '"')
# Check for the required user group membership to allow (exit 0) or decline (exit >0) the pipeline
if [ "$user" = "" ] || [ "$user" = "null" ]; then
echo "Missing '$MAGIC' comment from one of the OWNERS"
exit_code=3
else
echo "Found comment from user: $user"
curl ${CURL_ARGS} "https://api.github.com/orgs/kubernetes-sigs/members/${user}"
if [ $? -ne 0 ]; then
echo "User does not have permissions to start CI run"
exit_code=1
else
labels_to_patch=$(echo -n $labels | jq '. - ["needs-ci-auth"]' | tr -d "\n")
exit_code=0
echo "$user has allowed CI to start"
fi
fi
# Patch labels on PR
curl ${CURL_ARGS} --request PATCH "https://api.github.com/repos/kubernetes-sigs/kubespray/issues/${issue}?access_token=${GITHUB_TOKEN}" -H "Content-Type: application/json" -d "{\"labels\": ${labels_to_patch}}"
exit $exit_code

9
tests/scripts/molecule_logs.sh

@ -1,9 +0,0 @@
#!/bin/bash
# Ensure a clean environent
rm -fr molecule_logs
mkdir -p molecule_logs
# Collect and archive the logs
find ~/.cache/molecule/ -name \*.out -o -name \*.err -type f | xargs tar -uf molecule_logs/molecule.tar
gzip molecule_logs/molecule.tar
Loading…
Cancel
Save