Automated build for jenkins master
949
Based on: https://technologyconversations.com/2017/06/16/automating-jenkins-docker-setup/
The image uses docker secrets for the admin user and password and so at the time of writing must be run in swarm mode.
Create the docker secrets, change as necessary: ''' echo "admin" | docker secret create jenkins-user - echo "password" | docker secret create jenkins-pass - echo "aws_key_id" | docker secret create aws_access_key_id echo "aws_secret_access_key" | docker secret create aws_secret_access_key '''
Create a directory on the node for jenkins_home, default should be called jenkins, in the directory below the jenkins.yml file.
Create a directory on each node for the agent workspace, default should be called workspace, in the directory below the jenkins.yml file. Feel free to use a network volume to share the workspace.
Create the networks (add flag --attachable=true if necessary) ''' docker network create --driver=overlay ci-network docker network create --driver=overlay proxy '''
Then run ''' docker stack deploy -c jenkins.yml jenkins '''
Jenkins should now be available on port 8080. It takes a few minutes to start up first time.
In order to attach to other integrations we will need to generate the jenkins ssh key
''' docker container exec -it $ID mkdir /var/jenkins_home/.ssh '''
Get the container id ''' ID=$(docker container ls -q -f "label=com.docker.swarm.service.name=jenkins_jenkins-main") ''' Now run the keygen command, we do it here so the key is unique on Jenkins and not shared across instances ''' docker container exec -it -d $ID ssh-keygen -f /var/jenkins_home/.ssh/id_rsa -t rsa -N '' '''
Finally print it out ''' docker container exec -it $ID cat /var/jenkins_home/.ssh/id_rsa.pub '''
or copy in to a file ''' docker container exec -it $ID cat /var/jenkins_home/.ssh/id_rsa.pub | tee jenkins_pub_key '''
Start as above, then install/uninstall any plugins not required.
Get the list of plugins, change the password as necessary:
'''
curl -s -k "http://admin:admin@localhost:8080/pluginManager/api/json?depth=1"
| jq -r '.plugins[].shortName' | tee plugins_1.txt
'''
Update the Dockerfile to copy over plugin_1.txt
Build the Dockerfile
''' docker build -t jenkins-master:latest . '''
Then tag as necessary.
The security.groovy file allows us to use Docker secrets for the admin user The executors.groovy sets the executors on the master Jenkins to 0, so no jobs are run on that service. The sonar*.groovy files adds the sonar servers for code metrics. The slack-notifier.groovy adds global slack values.
Copied from: https://technologyconversations.com/2017/06/16/automating-jenkins-docker-setup/
In order to start Jenkins automatically we need a plugin.txt so plugins can be automatically downloaded.
Start Jenkins from Docker: ''' docker service create --name jenkins -p 8080:8080 jenkins '''
Obtain the container id: ''' ID=$(docker container ls -q -f "label=com.docker.swarm.service.name=jenkins") '''
Now cat to get the password: ''' docker container exec -it $ID cat /var/jenkins_home/secrets/initialAdminPassword '''
Once initialised, login, then install any plugins that may be needed. Swarm and Multi Branch pipeline are required. The plugins.txt in this folder should be renewed if extra plugins are needed. Note that this file requires the trailing blank line.
Create the list of plugins
curl -s -k "http://admin:admin@localhost:8080/pluginManager/api/json?depth=1"
| jq -r '.plugins[].shortName' | tee plugins_1.txt
The plugins.txt file is a list of all the plugins that will be installed on start up
Build and push the master jenkins using the Docker file as per instructions in top of the Dockerfile,
'''
docker build -t jenkins-master:1.0.4 .
docker tag repo/jenkins-master:1.0.4
docker push repo/jenkins-master:1.0.4
'''
Set Docker in swarm mode docker swarm init
Label the nodes as required The master node should be the Jenkins master. This will not run any jobs, so label: ''' docker node update --label-add jenkins-master=true the-node-name ''' If this step is done, change the constraints in jenkins.yml to ''' node.labels.jenkins-master == true '''
Add the volumes for persistent data (this should be aws bucket or some kind of persistent file system somewhere). On the master node, create a directory for persistent data, check the docker volume create commands, alternatively use the mappings below in the service creation scripts. In a real-world situation, you would use a driver like REX-Ray that would mount an EFS or EBS volume (in case of AWS).
Assumes the network exists ''' docker service create --network ci-network --with-registry-auth --secret jenkins-user --secret jenkins-pass --name jenkins-master -p 8080:8080 -p 50000:50000 --reserve-memory 300m --mount "type=bind,source=$PWD/docker/jenkins,target=/var/jenkins_home" --update-failure-action=rollback mononoke/jenkins-master:1.0.4 '''
Verify everything started ok
Now create the agents to attach
''' docker service create --network ci-network --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock --mount type=bind,src=$PWD/workspace,dst=/workspace -e "COMMAND_OPTIONS=-master http://jenkins-master:8080 -labels 'docker' -executors 5" --secret jenkins-user --secret jenkins-pass -e USER_NAME_SECRET=/run/secrets/jenkins-user -e PASSWORD_SECRET=/run/secrets/jenkins-pass --name jenkins-agent-5 --with-registry-auth mononoke/jenkins-swarm-agent-docker:1.0.0 '''
To scale up the agents just run ''' docker service scale jenkins-agent=4 ''' The extra agents should start appearing in the Jenkins master
''' docker service update --image mononoke/jenkins-master:1.0.4 jenkins-master docker service update --image mononoke/jenkins-swarm-agent-docker:1.0.1 jenkins-agent-5 '''
To rollback to the previous configuration:
''' docker service update --rollback jenkins-master '''
Content type
Image
Digest
Size
375.9 MB
Last updated
almost 9 years ago
docker pull mononoke/jenkins-master