Stop unused review app environments

Problem to solve

There are a large amount of review app environments that are available, but not connected to an open MR.

One reason that pushing to www-gitlab-com is slow is because the repository advertised nearly 300k refs (7MB uncompressed). This means pushing 5kb of new data requires downloading 7MB first. This adds 5-10 seconds to every push. Approx 200k of those refs (70%) are from stopped environments.

Job to be done

Write a script to search for environments that are stopped and over 1 month old, and delete them. Stop and delete all review app environments where it is not associated with an open MR.

List of available review apps: https://gitlab.com/gitlab-com/www-gitlab-com/-/environments/folders/review

Also, ensure that we attempt to delete the associated subdirectories from the GCP bucket at the same time.

API Resources to use

We could write a quick job to delete all stopped environments on www-gitlab-com. See scripts/cleanup-* for examples of this type of job.

Step 1:

Iterate over all environments using GraphQL:

https://gitlab.com/-/graphql-explorer

Query:

query($projectFullPath: ID!, $endCursor: String!) {
  project(fullPath: $projectFullPath) {
  	environments(after: $endCursor) {
      edges {
        node {
          id
          name
          state
        }
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
}

Query Resources:

{
  "projectFullPath": "gitlab-com/www-gitlab-com",
  "endCursor": ""
}

Step 2

Use the ID to delete the environment via the REST API: https://docs.gitlab.com/ee/api/environments.html#delete-an-environment

Related

Next Steps

This should be set up as a scheduled CI job: #8928 (closed)

Edited by Chad Woolley