Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fzipi/bce2fb096985803f057b to your computer and use it in GitHub Desktop.
Save fzipi/bce2fb096985803f057b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# The MIT License (MIT)
#
# Copyright (c) 2014 Arun Babu Neelicattu
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
GITLAB_USER=${GITLAB_USER:-gitlab_ci_runner}
GITLAB_HOME=${GITLAB_HOME:-/opt/${GITLAB_USER}}
GITLAB_CODE=${GITLAB_CODE:-${GITLAB_HOME}/gitlab-ci-runner}
GITLAB_DATABASE=${GITLAB_DATABASE:-gitlab_ci_production}
GITLAB_REPO=${GITLAB_REPO:-"https://gitlab.com/gitlab-org/gitlab-ci-runner.git"}
GITLAB_BRANCH=${GITLAB_BRANCH:-"master"}
GITLAB_CI_URL=${GITLAB_CI_URL:-"http://localhost:8080"}
RUNAS_CMD="runuser --login ${GITLAB_USER} --command"
# GITLAB_CI_TOKEN is required from http://gitlab-ci-domain.com/admin/runners
if [ -z ${GITLAB_CI_TOKEN} ]; then
echo "GITLAB_CI_TOKEN not set, fetch from ${GITLAB_CI_URL}/admin/runners"
exit 1
fi
# install deps
yum -y install ruby rubygem-bundler git tar
adduser \
--create-home \
--home-dir ${GITLAB_HOME} \
--system \
--comment 'GitLab CI Runner' ${GITLAB_USER}
# fetch source
${RUNAS_CMD} "git clone ${GITLAB_REPO} ${GITLAB_CODE} \
&& cd ${GITLAB_CODE} \
&& git checkout ${GITLAB_BRANCH}"
# install gems
yum -y install libicu-devel ruby-devel gcc make automake patch gcc-c++
${RUNAS_CMD} "cd ${GITLAB_CODE} \
&& bundle install --deployment"
# tmp files
${RUNAS_CMD} "cd ${GITLAB_CODE} \
&& mkdir -p tmp/pids \
&& find tmp/ -maxdepth 1 -type d -exec chmod -R u+rwX {} \;"
# non-interactive setup
${RUNAS_CMD} "cd ${GITLAB_CODE} \
&& CI_SERVER_URL=${GITLAB_CI_URL} \
REGISTRATION_TOKEN=${GITLAB_CI_TOKEN} \
bundle exec ./bin/setup"
# setup service
GITLAB_CI_RUNNER_INIT=/etc/init.d/gitlab-ci-runner
GITLAB_CI_RUNNER_SERVICE=$(basename ${GITLAB_CI_RUNNER_INIT})
cp ${GITLAB_CODE}/lib/support/init.d/gitlab_ci_runner ${GITLAB_CI_RUNNER_INIT}
chmod +x ${GITLAB_CI_RUNNER_INIT}
sed -i s='^APP_ROOT\=.*'="APP_ROOT\=\"${GITLAB_CODE}\""= ${GITLAB_CI_RUNNER_INIT}
sed -i s='^APP_USER\=.*'="APP_USER\=\"${GITLAB_USER}\""= ${GITLAB_CI_RUNNER_INIT}
systemctl enable ${GITLAB_CI_RUNNER_SERVICE}
# service ${GITLAB_CI_RUNNER_SERVICE} start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment