Skip to content

Instantly share code, notes, and snippets.

@brymck
Last active September 18, 2020 07:43
Show Gist options
  • Save brymck/49114934a48bb67ea7914a164272914b to your computer and use it in GitHub Desktop.
Save brymck/49114934a48bb67ea7914a164272914b to your computer and use it in GitHub Desktop.
Install Buildkite with Bazel on an EC2 instance
# I use the Amazon Linux 2 AMI on a t3.medium (2 vCPU, 4 GiB memory) instance attached to a 20 GB volume
#-----------------------------------------------------------------------------------------------------------------------
# Configuration
#-----------------------------------------------------------------------------------------------------------------------
# This is the token you get from going to
# https://buildkite.com/organizations/your-organization/agents
# and clicking Reveal Agent Token
export BUILDKITE_AGENT_TOKEN=foo
# If you already have a key you want to use for connecting Buildkite to GitHub, run something like
# scp -i ~/.ssh/buildkite-key-pair.pem ~/Downloads/buildkite-to-github.pem [email protected]:~/id_rsa
# A private key located in ~/id_rsa will be copied to the Buildkite agent's SSH directory as part of this process
#-----------------------------------------------------------------------------------------------------------------------
# Install Bazel
#-----------------------------------------------------------------------------------------------------------------------
# This follows the instructions on
# https://docs.bazel.build/versions/master/install-redhat.html
# Install dependencies: JDK, GCC, Python 3, patch, etc.
sudo amazon-linux-extras install -y java-openjdk11
sudo yum install -y gcc gcc-c++ patch python3
# If you have issues with the Buildkite agent detecting GCC, you may need to run
# sudo systemctl edit buildkite-agent
# and add the following configuration:
# [Service]
# Environment="CC=/usr/bin/gcc"
# Add the Bazel Repo
wget https://copr.fedorainfracloud.org/coprs/vbatts/bazel/repo/epel-7/vbatts-bazel-epel-7.repo
sudo mv vbatts-bazel-epel-7.repo /etc/yum.repos.d/
# Install Bazel
sudo yum install -y bazel3
#-----------------------------------------------------------------------------------------------------------------------
# Install Buildkite
#-----------------------------------------------------------------------------------------------------------------------
# This follows the instructions on
# https://buildkite.com/docs/agent/v3/redhat
# Add Git LFS
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.rpm.sh | sudo bash
sudo yum install -y git-lfs
git lfs install
# Add yum repository
sudo sh -c 'echo -e "[buildkite-agent]\nname = Buildkite Pty Ltd\nbaseurl = https://yum.buildkite.com/buildkite-agent/stable/x86_64/\nenabled=1\ngpgcheck=0\npriority=1" > /etc/yum.repos.d/buildkite-agent.repo'
# Install the Buildkite agent
sudo yum install -y buildkite-agent
# Configure the agent token
sudo sed -i "s/xxx/$BUILDKITE_AGENT_TOKEN/g" /etc/buildkite-agent/buildkite-agent.cfg
# Uncomment lines for getting tags from GCP
sudo sed -i 's/^# tags-from-ec2/tags-from-ec2/g' /etc/buildkite-agent/buildkite-agent.cfg
# Create SSH directories for the buildkite-agent user and ensure they have the right permissions
sudo mkdir -p /var/lib/buildkite-agent/.ssh/
sudo chmod 700 /var/lib/buildkite-agent/.ssh/
sudo chown -R buildkite-agent:buildkite-agent /var/lib/buildkite-agent/.ssh/
# Use a preexisting private key if provided, otherwise create a new SSH key
if [ -f ~/id_rsa ]; then
# Copy the private key to Buildkite's SSH directories
sudo mv ~/id_rsa /var/lib/buildkite-agent/.ssh/id_rsa
sudo chmod 600 /var/lib/buildkite-agent/.ssh/id_rsa
else
# Create an SSH key with no passphrase in the Buildkite agent SSH directory and assign it the agent's user and group
sudo ssh-keygen -t rsa -b 4096 -C buildkite -N '' -f /var/lib/buildkite-agent/.ssh/id_rsa
fi
# Ensure buildkite-agent owns its SSH directories
sudo chown -R buildkite-agent:buildkite-agent /var/lib/buildkite-agent/.ssh/
# Add the contents of /var/lib/buildkite-agent/.ssh/id_rsa.pub to your GitHub keys via
# https://github.com/settings/ssh/new
# Start the agent
sudo systemctl enable buildkite-agent && sudo systemctl start buildkite-agent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment