Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Cleanup old GH env via Actions #20513

Merged
merged 4 commits into from
Jan 29, 2025
Merged

feat: Cleanup old GH env via Actions #20513

merged 4 commits into from
Jan 29, 2025

Conversation

ericboehs
Copy link
Contributor

@ericboehs ericboehs commented Jan 28, 2025

department-of-veterans-affairs/va.gov-team#100722

This workflow deletes old, non-protected environments from the Vets API repo on GitHub. It also deletes environments attached to PRs on PR close/merge.

I initially wrote this to use bash but decided to convert to ruby for readability. Speaking of, GitHub doesn't syntax highlight bash (or ruby) embedded in YAML, so here's the ruby script:

require 'bundler/inline'
gemfile { source 'https://rubygems.org'; gem 'octokit' }

CURRENT_TIME = Time.now.to_i
THRESHOLD_SECONDS = 90 * 24 * 60 * 60 # 90 days in seconds
REPO = ENV['GITHUB_REPOSITORY']
CLIENT = Octokit::Client.new access_token: ENV['VA_VSP_BOT_GITHUB_TOKEN']

def old_and_unprotected? env
  env[:protection_rules].empty? && CURRENT_TIME - env[:created_at].to_i > THRESHOLD_SECONDS
end

def delete_environment env
  puts "\nDeleting: #{env[:name]}"
  CLIENT.delete_environment REPO, URI.encode_www_form_component(env[:name])
end

print 'Fetching and deleting old environments'
100.times do |page|
  envs = CLIENT.environments(REPO, per_page: 100, page: page + 1)&.dig(:environments) || []
  print '.'
  break if envs.empty?
  exit 2 if CLIENT.rate_limit.remaining < 1000

  envs
    .select { |env| old_and_unprotected? env }
    .each { |env| delete_environment env }
end

puts "\nDone."

I extracted the methods for readability.

@va-vfs-bot va-vfs-bot temporarily deployed to eb-clean-gh-env-job-test/main/main January 29, 2025 01:37 Inactive
@va-vfs-bot va-vfs-bot temporarily deployed to eb-clean-gh-env-job/main/main January 29, 2025 01:38 Inactive
@ericboehs ericboehs marked this pull request as ready for review January 29, 2025 18:33
@ericboehs ericboehs requested a review from a team as a code owner January 29, 2025 18:33
@ericboehs ericboehs enabled auto-merge (squash) January 29, 2025 20:44
end

print 'Fetching and deleting old environments'
100.times do |page|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically we'll only have 1 page worth of environments open so I probably don't need to loop but if this ever needs to run on thousands of envs again, this should run through them in 1-2 runs (rate limiting aside).

@ericboehs ericboehs merged commit 764ec08 into master Jan 29, 2025
79 of 81 checks passed
@ericboehs ericboehs deleted the eb-clean-gh-env-job branch January 29, 2025 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants