-
-
Save stoe/2eae33bd3d097a59b4a75e997a14c54d to your computer and use it in GitHub Desktop.
Script to install a GitHub App by ID on all organizations on a GHES instance
This file contains 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
#!/usr/bin/env bash | |
# Script to install a GitHub App by ID on all organizations on a GHES instance | |
# Hostname or IP of the GitHub Enterprise Server to connect to | |
GHES_HOST=github.example.com | |
# SSH key to be used for connecting to the GitHub Enterprise Server | |
# GHES_SSH_KEY=~/.ssh/your-key | |
############################################################################### | |
# CODE - PLEASE DON'T EDIT BELOW THIS LINE | |
############################################################################### | |
set -e | |
function set_github_env() { | |
# Prepare arguments that are passed to the Ruby script | |
for arg in "$@"; do | |
# If argument matches a file path, read it and pass its content on | |
if [[ -f "$arg" ]]; then | |
# shellcheck disable=SC2002 | |
prepared_arguments+=" $(cat "$arg" | xargs | sed -e 's/ /,/g') " | |
else | |
# github-env is splitting argument values with space into separate values, hence we're encoding spaces here | |
# decoding is happening in the Ruby scripts for arguments that expect values with spaces | |
# https://github.com/koalaman/shellcheck/wiki/SC2001 | |
# shellcheck disable=SC2001 | |
arg=$(echo "$arg" | sed 's/ /:space:/g') | |
prepared_arguments+=" $arg " | |
fi | |
done | |
notice="Loading GitHub environment on $GHES_HOST, this may take a moment ..." | |
separator="--------------------------------------------------------------------------------" | |
# shellcheck disable=SC2034 | |
github_env="echo $notice 1>&2; echo $separator 1>&2; github-env bin/safe-ruby -r ./config/environment - $prepared_arguments" | |
} | |
function error_exit() { | |
echo -e "\n$(tput setaf 1)###\n### ERROR\n###\n> $(tput sgr 0)$1\n" >&2 | |
# shellcheck disable=SC2154 | |
echo -e "$(tput setaf 1)$error_message$(tput sgr 0)\n" >&2 | |
exit 1 | |
} | |
if [[ -n "$GHES_SSH_KEY" ]]; then | |
ghes_ssh_key_option="-i $GHES_SSH_KEY" | |
fi | |
set_github_env $@ | |
ssh -p 122 $ghes_ssh_key_option admin@$GHES_HOST "$github_env" << END_OF_RUBY | |
options = OpenStruct.new | |
option_parser = OptionParser.new do |opts| | |
opts.banner = "Usage: ghes-install-app [options]" | |
opts.separator "" | |
opts.on("-i", "--id GitHub App ID", "ID of the GitHub App to install") do |app_id| | |
options.id = app_id.to_i | |
end | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
end | |
option_parser.parse!(ARGV) | |
if options.id.nil? | |
raise ArgumentError, "GitHub App ID required" | |
end | |
STDERR.puts "Total number of organizations: #{Organization.count - 1}" | |
app = Integration.find_by(id: options.id) | |
if app.nil? | |
STDERR.puts "GitHub App with ID #{options.id} not found!" | |
else | |
Organization.find_each do |organization| | |
next if organization.to_s == "github-enterprise" | |
result = app.install_on( | |
organization, | |
repositories: [], # all current and future repositories | |
installer: organization.admins.first | |
) | |
if result.success? | |
STDERR.puts "#{organization.to_s} install succeeded\r" | |
else | |
STDERR.puts "#{organization.to_s} install failed\r" | |
end | |
end | |
end | |
STDERR.puts "Done!" | |
END_OF_RUBY |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment