Skip to content

Commit 6ea4ec1

Browse files
committed
- Moving to better .env for docker-compose.yml
- houskeeping for registrated gitlab-runner
1 parent 87aaf01 commit 6ea4ec1

File tree

6 files changed

+58
-55
lines changed

6 files changed

+58
-55
lines changed

.env.template

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This is the Environment for the docker-compos-file:
2+
# autogenerated ...do NOT edit

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
docker-compose.yml
21
docker-compose.yml-*
32
*.jpi
43
*.war
4+
.env

README.md

+10-12
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
##### Tired of endless installation and configuration .... ?!
44

5-
My personal solution is a local Build Environment with Jenkins, Gitlab, (Sonar) and Nexus; ready in a few minutes.
5+
My personal solution is a local Build Environment with Jenkins, Gitlab / Gitlabrunner, (Sonar) and Nexus; ready in a few minutes.
66
Your own lokal, personal, continous build enviroment (maybe in future releases I just call it lpcbe).
77

8+
# This is NOT for any cluster (Swarm / Kubernetes)
9+
810
### System requirements
911
* At least 8GB Memory with 3GB Swap and 10GB Disk-Space
1012
* docker version >= 17.06.0
@@ -15,7 +17,7 @@ Bring up your own build environment ... just do a
1517
```
1618
git clone https://github.com/Springjunky/docker-local-build-environment.git
1719
cd docker-local-build-environment
18-
sudo ./prepareCompose.sh
20+
sudo ./setupEnvironment.sh
1921
docker-compose up --build -d
2022
docker-compose logs
2123
```
@@ -39,7 +41,7 @@ Now you are ready to go with a little CI/CD Environment:
3941
... not really, its all http .. don't worry about it! It's only local communication
4042

4143
##### security paranoia
42-
All the exposed ports are reachable from outer world because docker creates and deletes dynamically FORWARD rules with default policy ACCEPT on startup / shutdown containers wich have exported ports.
44+
All the exposed ports are reachable from outer world because docker creates and deletes dynamically iptables FORWARD rules with default policy ACCEPT on startup / shutdown containers wich have exported ports.
4345

4446
To deny acccess from outer world the DOCKER-USER Chain (since docker 17.06) ist the medium of choice for your own rules (this is the first target in the FORWARD-Chain and never touched by docker).
4547

@@ -97,23 +99,19 @@ echo "See logs with prefix DCKR-EXT-ACCESS-DENY:"
9799
* MAVEN_HOME is /opt/maven
98100
* JAVA_HOME is /usr/lib/jvm/java-8-openjdk-amd64
99101
* Blue Ocean is installed and works perfect with a GitHUB Account, not GitLab ... sorry, this is Jenkins.
100-
You need to be logged in to use Blue Ocean
102+
You need to be logged as a jenkins-user in to use Blue Ocean
101103

102104
### Giltab
103105

104-
* the docker-registry is at port 5555 (and secured with an openssl certificate ..thats part of
105-
prepareCompose.sh), just create a project in gitlab and click at the registry tab to show
106+
* the docker-registry from GitLab is at port 5555 (and secured with an openssl certificate ..thats part of
107+
prepareEnvironment.sh), just create a project in gitlab and click at the registry tab to show
106108
how to login to the project registry and how to tag your images
107109
* ssh cloning and pushing is at port 2222
108110

109111
#### gitlab-runner
110-
The runner is a gitlab-multirunner image with a docker-runner (concurrent=1) , based on [gitlab/gitlab-runner][2] The docker-compose section has an environment called
111-
REGISTER_MODE, it can set to KEEP or REFRESH
112-
* KEEP register at one time a runner and keep it during startups
113-
* REFRESH at every startup remove all old runners and register one new runner (the pipeline-history ist lost.)
114-
115-
It takes a long time until gitlab is ready to accept a runner registration, if it fails, increase the REGISTER_TRYS
112+
The runner is a gitlab-multirunner image with a docker-runner (concurrent=1) , based on [gitlab/gitlab-runner][2] at every startup any runner is removed and only ONE new runner ist registrated to avoid multiple runners (the pipeline-history maybe lost.) docker-in-docker works :-)
116113

114+
It takes a long time until gitlab is ready to accept a runner registration, if it fails, increase the REGISTER_TRYS in docker-compse.yml
117115

118116

119117
#### Jenkins and Gitlab
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#####################################
22
# Don't touch this file is generated
3-
# expecilly for: HOSTNAME
3+
# expecilly for: ${DC_HOSTNAME}
44
###################################
55
#
6-
# Consider to Backup BASE_DATA_DIR
6+
# Consider to Backup ${DC_BASE_DATA_DIR}
77
#
88
version: "3"
99

@@ -18,7 +18,7 @@ services:
1818
ports:
1919
- "80:80" #http://
2020
# SSH Bypassing into gitlab, if you want to change this edit nginx.conf also
21-
- "2222:2222" #ssh port of gitlab (ssh://git@myhostname:2222/scott/foo.git)
21+
- "2222:2222" #ssh port of gitlab (ssh://git@myHOST:2222/scott/foo.git)
2222
- "5555:5555" #Gitlab Docker Registry do NOT use 5000, this is an internal PORT of the gitlab-ce Image
2323
networks:
2424
- devstacknetwork
@@ -27,64 +27,65 @@ services:
2727
jenkins-fat:
2828
build: jenkins-fat
2929
extra_hosts:
30-
- HOSTNAME:HOSTIP
30+
- ${DC_HOSTNAME}:${DC_HOSTIP}
3131
networks:
3232
- devstacknetwork
3333
volumes:
34-
- BASE_DATA_DIR/jenkins:/var/jenkins_home
34+
- ${DC_BASE_DATA_DIR}/jenkins:/var/jenkins_home
3535
- /var/run/docker.sock:/var/run/docker.sock
3636

3737
# ------------------------------------------------------------------------------
3838
gitlab:
3939
image: 'gitlab/gitlab-ce:latest'
4040
extra_hosts:
41-
- HOSTNAME:HOSTIP
41+
- ${DC_HOSTNAME}:${DC_HOSTIP}
4242
networks:
4343
- devstacknetwork
4444
environment:
4545
GITLAB_OMNIBUS_CONFIG: |
46-
external_url 'http://HOSTNAME/gitlab'
46+
external_url 'http://${DC_HOSTNAME}/gitlab'
4747
gitlab_rails['initial_root_password'] = "gitlab4me"
4848
gitlab_rails['initial_shared_runners_registration_token'] = "s3cretToken4Runner"
4949
gitlab_rails['gitlab_shell_ssh_port'] = 2222
5050
# docker-registry config
51-
registry_external_url 'https://HOSTNAME:5555'
51+
registry_external_url 'https://${DC_HOSTNAME}:5555'
5252
registry_nginx['listen_port'] = 5555
5353
registry_nginx['listen_https'] = true
5454
# SSL config just for the docker-registry need
55-
nginx['ssl_certificate'] = "/etc/gitlab/ssl/HOSTNAME.crt"
56-
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/HOSTNAME.key"
55+
nginx['ssl_certificate'] = "/etc/gitlab/ssl/${DC_HOSTNAME}.crt"
56+
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/${DC_HOSTNAME}.key"
5757
registry_nginx['proxy_set_headers'] = {
5858
"X-Forwarded-Proto" => "https",
5959
"X-Forwarded-Ssl" => "on"
6060
}
6161
volumes:
62-
- BASE_DATA_DIR/gitlab/config:/etc/gitlab
63-
- BASE_DATA_DIR/gitlab/logs:/var/log/gitlab
64-
- BASE_DATA_DIR/gitlab/data:/var/opt/gitlab
62+
- ${DC_BASE_DATA_DIR}/gitlab/config:/etc/gitlab
63+
- ${DC_BASE_DATA_DIR}/gitlab/logs:/var/log/gitlab
64+
- ${DC_BASE_DATA_DIR}/gitlab/data:/var/opt/gitlab
6565
# ------------------------------------------------------------------------------
6666
nexus:
6767
image: sonatype/nexus3
6868
extra_hosts:
69-
- HOSTNAME:HOSTIP
69+
- ${DC_HOSTNAME}:${DC_HOSTIP}
7070
networks:
7171
- devstacknetwork
7272
environment:
7373
- NEXUS_CONTEXT=nexus
7474
volumes:
75-
- BASE_DATA_DIR/nexus:/nexus-data
75+
- ${DC_BASE_DATA_DIR}/nexus:/nexus-data
7676

7777
# ------------------------------------------------------------------------------
7878
gitlabrunner:
7979
build: gitlabrunner
8080
network_mode: "host"
8181
extra_hosts:
82-
- HOSTNAME:HOSTIP
82+
- ${DC_HOSTNAME}:${DC_HOSTIP}
8383
environment:
84-
- GITLAB_URL=http://HOSTNAME/gitlab
84+
- GITLAB_URL=http://${DC_HOSTNAME}/gitlab
85+
- HOSTNAME=${DC_HOSTNAME}
86+
- HOSTIP=${DC_HOSTIP}
8587
- REGISTER_TOKEN=s3cretToken4Runner
86-
- REGISTER_MODE=FRESH #KEEP register a new runner an keep it. FRESH unregister all runners and always install a new (pipeline-history lost!)
8788
- REGISTER_TRYS=60 # every 10 seconds a try to register the runner..gitlab takes a long time to startup
8889
volumes:
89-
- BASE_DATA_DIR/gitlab-runner/config:/etc/gitlab-runner
90+
- ${DC_BASE_DATA_DIR}/gitlab-runner/config:/etc/gitlab-runner
9091
- /var/run/docker.sock:/var/run/docker.sock

gitlabrunner/entrypointAutoregister

+3-9
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,21 @@ fi
2020

2121

2222
###########################################################################################################
23-
echo "Try to autoregister runner at ${GITLAB_URL} with ${REGISTER_TOKEN} with $REGISTER_TRYS trys"
23+
echo "Try to autoregister runner at ${GITLAB_URL} with host ${HOSTNAME}:${HOSTIP} and token ${REGISTER_TOKEN} for $REGISTER_TRYS trys"
2424

2525
grep docker-runner /etc/gitlab-runner/config.toml 2>/dev/null 1>/dev/null
2626
RUNNER_IS_PRESENT=$?
2727

28-
if test $REGISTER_MODE = "KEEP" -a $RUNNER_IS_PRESENT -eq 0 ; then
29-
echo "Keeping old runner"
30-
fi
31-
3228
TRYS=$REGISTER_TRYS
3329
REGISTERED=1
3430

35-
if test $REGISTER_MODE = "FRESH" -o $RUNNER_IS_PRESENT -ne 0 ; then
3631
while test $TRYS -gt 0 -a $REGISTERED -gt 0 ; do
3732
TRYS=$(expr $TRYS - 1)
3833
gitlab-runner register -n --url=${GITLAB_URL} --registration-token=${REGISTER_TOKEN} \
3934
--executor=docker --name=docker-multi-runner \
4035
--run-untagged=true --locked=false --docker-image=docker:latest \
4136
--docker-privileged \
42-
--docker-extra-hosts="HOSTNAME:HOSTIP"
37+
--docker-extra-hosts="${HOSTNAME}:${HOSTIP}"
4338
REGISTERED=$?
4439
if [ $REGISTERED -ne 0 ] ; then
4540
echo "${GITLAB_URL} not ready $TRYS trys left, next try in 10 second for ${GITLAB_URL}"
@@ -56,10 +51,9 @@ if test $REGISTER_MODE = "FRESH" -o $RUNNER_IS_PRESENT -ne 0 ; then
5651
--executor=docker --name=docker-multi-runner \
5752
--run-untagged=true --locked=false --docker-image=docker:latest \
5853
--docker-privileged \
59-
--docker-extra-hosts="HOSTNAME:HOSTIP"
54+
--docker-extra-hosts="${HOSTNAME}:${HOSTIP}"
6055
fi
6156
done
62-
fi
6357

6458
# launch gitlab-ci-multi-runner passing all arguments
6559
exec gitlab-ci-multi-runner "$@"

prepareCompose.sh renamed to setupEnvironment.sh

+22-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if [ $(id -u) -gt 0 ] ;then
55
exit 1
66
fi
77

8-
echo "Prepare compose file and directorys"
8+
echo "Prepare .env for compose file and directorys"
99

1010
USER_DATA_DIR=$HOME/devstack-data
1111
HOSTNAME=$(hostname)
@@ -75,10 +75,10 @@ else
7575
exit 1
7676
fi
7777

78-
if [ -f docker-compose.yml ]; then
79-
FILE_NAME=docker-compose.yml-$(date +"%F-%H-%M-%S-%N")
80-
cp docker-compose.yml $FILE_NAME
81-
echo "previous docker-compose.yml saved as $FILE_NAME"
78+
if [ -f .env ]; then
79+
FILE_NAME=.env-$(date +"%F-%H-%M-%S-%N")
80+
cp .env $FILE_NAME
81+
echo "previous .env saved as $FILE_NAME"
8282
fi
8383
# Copy preconfigs to host-volumes
8484
# sonar.properties
@@ -92,19 +92,27 @@ fi
9292
#Copy predefined Jobs and Configs
9393
cp -r preconfig/jenkins/* $USER_DATA_DIR/jenkins/
9494

95-
# Set the right volume-names, hostname and host_ip in docker-compose.yml
96-
sed s#BASE_DATA_DIR#${USER_DATA_DIR}#g docker-compose.yml.template > docker-compose.yml
97-
sed -i s#HOSTIP#${HOSTIP}#g docker-compose.yml
98-
sed -i s#HOSTNAME#${HOSTNAME}#g docker-compose.yml
95+
# Set the right volume-names, hostname and host_ip in .env for docker-compose.yml
96+
echo "---------- generating .env file for docker-compose.yml "
97+
cat .env.template > .env
98+
echo "DC_HOSTNAME=${HOSTNAME}" >> .env
99+
echo "DC_HOSTIP=${HOSTIP}" >> .env
100+
echo "DC_BASE_DATA_DIR=${USER_DATA_DIR}" >> .env
101+
echo "---------- genarated file ---------------------------- "
102+
cat .env
103+
echo "-------------------------------------------------------------------------------------------"
104+
105+
#sed s#BASE_DATA_DIR#${USER_DATA_DIR}#g docker-compose.yml.template > docker-compose.yml
106+
#sed -i s#HOSTIP#${HOSTIP}#g docker-compose.yml
107+
#sed -i s#HOSTNAME#${HOSTNAME}#g docker-compose.yml
99108

100-
# Gitlabrunner needs extra_hosts to clone stuff via hostname
101-
sed -i s#HOSTNAME#${HOSTNAME}#g gitlabrunner/entrypointAutoregister
102-
sed -i s#HOSTIP#${HOSTIP}#g gitlabrunner/entrypointAutoregister
109+
# Gitlabrunner needs extra_hosts to clone stuff via (outside) hostname
110+
# sed -i s#HOSTNAME#${HOSTNAME}#g gitlabrunner/entrypointAutoregister
111+
# sed -i s#HOSTIP#${HOSTIP}#g gitlabrunner/entrypointAutoregister
103112

104-
chmod a+rw docker-compose.yml
105113
echo "-------------------------------------------------------------------------------------------"
106114
echo "-------------------------------------------------------------------------------------------"
107-
echo "docker-compose.yml created"
115+
echo "Evironment for docker-compose.yml created"
108116
echo "run "
109117
echo "docker-compose up --build -d "
110118
echo "docker-compose logs -f"

0 commit comments

Comments
 (0)