Skip to content

Instantly share code, notes, and snippets.

@atd-schubert
Last active August 30, 2017 09:32
Show Gist options
  • Save atd-schubert/aba495fa500472d04113a50dc78a6f07 to your computer and use it in GitHub Desktop.
Save atd-schubert/aba495fa500472d04113a50dc78a6f07 to your computer and use it in GitHub Desktop.
Create your own GitLab instance on ubuntu with docker
#!/bin/bash
# GITLAB_HOSTNAME="gitlab.acme.org" # <- Change this to your actual domain...
# DOCKER_NAMESPACE="acme" # <- Change this to your actual company...
# ALLOWED_IMAGES="$DOCKER_NAMESPACE/*"
# DEFAULT_IMAGE="$DOCKER_NAMESPACE/build"
# ADMIN_IMAGE="$DOCKER_NAMESPACE-dind"
DOCKER_COMPOSE_VERSION=1.15.0
################## Install pre-requirements ##################
if [ `command -v docker-compose | wc -l` == "0" ]
then
set -x \
&& echo "Install pre-requirements..." \
&& apt-get update \
&& apt-get install -y \
linux-image-extra-virtual \
apt-transport-https \
ca-certificates \
curl \
software-properties-common \
&& echo "Register docker software-repository..." \
&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - \
&& add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable" \
&& echo "Install docker..." \
&& apt-get update \
&& apt-get install -y docker-ce \
&& echo "Install docker-compose..." \
&& curl -L https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose \
&& chmod +x /usr/local/bin/docker-compose
else
echo "Docker and docker-compose seems to be already installed..."
fi
################## Create a docker-compose file if there is none... ##################
read -p "Enter the hostname of your GitLab instance: " GITLAB_HOSTNAME
if [ ! -f docker-compose.yml ]
then
set +x
echo "Create docker files in folder '$PWD'"
cat > docker-compose.yml <<EOF
version: '3'
services:
gitlab:
image: gitlab/gitlab-ce:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./gitlab-ce:/etc/gitlab
- gitlab-data:/var/opt/gitlab
- gitlab-logs:/var/log/gitlab
environment:
GITLAB_OMNIBUS_CONFIG: "external_url 'http://$GITLAB_HOSTNAME'; gitlab_rails['lfs_enabled'] = true;"
expose:
- 80
- 443
- 22
ports:
- 80:80
- 443:443
- 44:22
depends_on:
- admin-runner
- shared-runner
- priority-runner
shared-runner:
image: gitlab/gitlab-runner:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./shared-runner/config:/etc/gitlab-runner
priority-runner:
image: gitlab/gitlab-runner:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./priority-runner/config:/etc/gitlab-runner
admin-runner:
image: gitlab/gitlab-runner:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./admin-runner/config:/etc/gitlab-runner
volumes:
gitlab-data:
gitlab-logs:
EOF
else
echo "There is already a docker-compose.yml in '$PWD'. The provisioning will use this one..."
fi
################## Start the containers and make the provisioning for the runners ##################
set -x \
&& mkdir -p gitlab-ce admin-runner priority-runner shared-runner \
&& docker-compose up -d \
&& docker-compose exec gitlab update-permissions
################## Run interactive provisioning for the runners ##################
set +x
echo "Additional instructions:"
echo "Please go to http://$GITLAB_HOSTNAME/ and register the credentials for the root user..."
echo "After that got to: http://$GITLAB_HOSTNAME/admin/runners and copy the registration token."
read -p "Paste the GitLab registration-token for runners: " TOKEN
read -e -p "Paste the GitLab registration-token for the admin runner: " -i "$TOKEN" ADMIN_TOKEN
read -e -p "Enter your docker-hub namespace: " -i "$DOCKER_NAMESPACE" DOCKER_NAMESPACE
read -e -p "Enter the allowed images for your runners: " -i "$DOCKER_NAMESPACE/*" ALLOWED_IMAGES
read -e -p "Enter the default image for your runners: " -i "$DOCKER_NAMESPACE/build" DEFAULT_IMAGE
read -e -p "Enter the name of the admin (dind) image: " -i "$DOCKER_NAMESPACE-dind" ADMIN_IMAGE
set -x \
&& docker-compose exec shared-runner gitlab-runner register -n --name "Shared CI runner" -r $TOKEN -u "http://gitlab" --executor "docker" --docker-allowed-images $ALLOWED_IMAGES --docker-pull-policy "if-not-present" --run-untagged --docker-image $DEFAULT_IMAGE \
&& docker-compose exec priority-runner gitlab-runner register -n --name "Priority CI runner" -r $TOKEN -u "http://gitlab" --executor "docker" --docker-allowed-images $ALLOWED_IMAGES --tag-list "priority" --docker-pull-policy "if-not-present" --docker-image $DEFAULT_IMAGE --locked \
&& docker-compose exec admin-runner gitlab-runner register -n --name "Admin CI runner" -r $ADMIN_TOKEN -u "http://gitlab" --executor "docker" --docker-allowed-images '*' --tag-list "admin" --docker-pull-policy "if-not-present" --docker-volumes "/var/run/docker.sock:/var/run/docker.sock" --docker-image $ADMIN_IMAGE \
################## Create docker image for admin purpose ##################
set +x
if [ `docker images | grep dind | wc -l` == 0 ]
then
echo "Create dind image for admins" \
set -x \
&& mkdir -p dind \
&& cat > dind/Dockerfile <<EOF
FROM alpine:latest
MAINTAINER Arne Schubert <atd.schubert@gmail.com>
RUN set -x \
&& apk --no-cache add docker
VOLUME ["/var/run/docker.sock"]
EOF
set -x \
&& docker build -t $ADMIN_IMAGE dind
else
set +x
echo "dind image already available to docker..."
fi
## update with docker-compose pull
## maybe restart after that with docker-compose restart
## create other build images directly in gitlab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment