In this example we will create an installation of Gitlab and Rancher from strach on Digitalocean.
docker-machine create --driver digitalocean --digitalocean-access-token $DOTOKEN --digitalocean-image ubuntu-16-04-x64 --digitalocean-size 4gb --digitalocean-tags gitlab-rancher-example gitlab-host
eval $(docker-machine env gitlab-host)
cd gitlab-ui
cat docker-compose.yml
docker-compose up -d
cd gitlab-runner
cat docker-compose.yml
docker-compose up -d
register gitlab-runner to the gitlab instance:
docker exec -it gitlabrunner_web_1 bash
gitlab-runner register
- coordinator URL: http://DOCKER_MACHINE_IP
- CI token: login as root in Gitlab UI >> admin area >> runners
- executor: docker
- default docker image: java:8-jdk
check registered runner on Gitlab UI: login as root in Gitlab UI >> admin area >> runners
- Generate Gitlab Personal access token: Github >> Settings Personal Access tokens >> create
- Import from https://github.com/mariodavid/kubanische-kaninchenzuechterei
Set up CI >> Gradle template
.gitlab-ci.yml:
image: java:8
variables:
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
CONTAINER_TEST_IMAGE: mariodavid/kubanische-kaninchenzuechterei:$CI_BUILD_REF_NAME
CONTAINER_RELEASE_IMAGE: mariodavid/kubanische-kaninchenzuechterei:latest
before_script:
- chmod +x gradlew
stages:
- build
- test
- release-jar
- release-container
build:
stage: build
script:
- ./gradlew -g /cache/.gradle clean assemble
allow_failure: false
test:
stage: test
script:
- ./gradlew -g /cache/.gradle check
buildJar:
stage: release-jar
script:
- ./gradlew -g /cache/.gradle buildUberJar
artifacts:
untracked: true
buildContainer:
image: docker:git
services:
- docker:dind
stage: release-container
dependencies:
- buildJar
before_script:
- docker login -u $DOCKER_REGISTRY_USERNAME -p $DOCKER_REGISTRY_PASSWORD
script:
- mkdir -p ./deployment/container-files
- cp ./build/distributions/uberJar/app.jar ./deployment/container-files/app.jar
- cd ./deployment
- docker build -t $CONTAINER_TEST_IMAGE .
- docker push $CONTAINER_TEST_IMAGE
only:
- master
docker exec -it gitlabrunner_web_1 bash
nano /etc/gitlab-runner/config.toml
gitlab-runner restart
set the following attributes:
privileged = true
volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
docker-machine create --driver digitalocean --digitalocean-access-token $DOTOKEN --digitalocean-image ubuntu-16-04-x64 --digitalocean-size 2gb --digitalocean-tags gitlab-rancher-example rancher-ui-host
eval $(docker-machine env rancher-ui-host)
cd rancher
cat docker-compose.yml
docker-compose up -d
Rancher UI > Default environment > Hosts > Add Host > Digitalocean. Add the Access token of Digitalocean ($DOTOKEN) and create a machine.
Create a Rancher CLI access token through the UI of rancher (API > Keys) and put the following environment variables into your .bashrc file:
export RANCHER_URL=http://<<RANCHER_IP>>:8080
export RANCHER_ACCESS_KEY=<accessKey_of_account_api_key>
export RANCHER_SECRET_KEY=<secretKey_of_account_api_key>
git clone https://github.com/mariodavid/kubanische-kaninchenzuechterei
cd kubanische-kaninchenzuechterei/deployment
cat docker-compose.yml
cat rancher-compose.yml
rancher up
Add environment variables to the Gitlab project:
RANCHER_URL=http://RANCHER_HOST_IP:8080/
RANCHER_ACCESS_KEY=...
RANCHER_SECRET_KEY=...
Add the following step to your .gitlab-ci.yml:
stages:
- ...
- deploy
deploy:
stage: deploy
image: cdrx/rancher-gitlab-deploy
script:
- upgrade --stack deployment --service web --new-image $CONTAINER_TEST_IMAGE