Pre-configured Environment Variable in Jenkins - jenkins

I am running Jenkins though the docker image.
I want my Jenkins Installation to come up with few pre configured enviroment variables.
Is there any way I can copy some of my environment variables, so that when I bring my jenkins up, it should come with these pre-configured environment variables ?
TIA.

A list of available environment variables is available here: https://hub.docker.com/_/jenkins/
Update
If you want to add your own environment variables you need to build your own jenkins image. You can do this by extending an existing image or modify the official docker build project provided by jenkins on GitHub.
Docker has a nice tutorial on how to build images here.
In this specific case you need to modify the docker entrypoint file to retrieve and replace the environment variables where needed. For jenkins, the docker entrypoint file is located in /usr/local/bin/jenkins.sh.

Related

Pass binary from Jenkins host to agent

Can you pass a binary from a Jenkins host to an agent?
I've got Jenkins running in Kubernetes, and the terraform plugin installed on my Jenkins master with the binary located at /var/jenkins_home/tools/org.jenkinsci.plugins.terraform.TerraformInstallation/terraform/terraform
I would like to pass this to my Jenkins agent by configuring my pod template and mounting the host volume path /var/jenkins_home/tools/org.jenkinsci.plugins.terraform.TerraformInstallation/terraform/terraform to the agent's path /usr/bin/terraform
But this doesn't seem to work as expected
When I exec into the agent and run a terraform version I get the error bash: terraform: command not found indicating that it doesn't have the binary.
I can see a terraform directory mounted in /usr/bin but without the binary. What I expect is for terraform to be installed on the agent. But my thinking might be incorrect here.
Is it possible to do this, has anyone has any experience with this?
As a #David Maze mentioned binary from Jenkins needs to be manually installed on every node, which can be a difficult to manage. However you can set Jenkins to run pipeline steps inside a container where the image contains the tools you need, which simplifies such case.
Read more: execution-env-jenkins.
One alternative is to use the slaves setup plugin. We use it to install and configure internal tools (and end) on nodes bases on labels. A log less hassle than #Malgorata's (and our previous) manually copy approach
Not sure how well it works with Kubernetes as not in our configuration.

Using jenkins and docker to deploy to server

Hey I am currently learn Jenkins pipeline for CI and CD
I was successfully deploy my express js by Jenkins
On locally machine my server
It was for server and my ENV was show off on my public repository
I am here trying to understand more how to hide that ENV on my Jenkins? That use variable
And is that possible to use variable on Dockerfile also to hide my ENV ?
On my Jenkins Pipeline
I run my ENV on docker run -p -e myEnV=key
I do love to hide my ENV so people didn't know my keys inside on my Jenkinsfile and Dockerfile
I am using multi branches in jenkins because I follow the article on hackernoon for deploy react and node js app with Jenkins
And anyway, what advantages to push our container or image to Docker Hub?
If we push it to there and if we want to move our server to another server
We just need to pull our repo Docker Hub to use that to new server because what we have been build everytime it push to our repo Docker Hub , right ?
For your first question, you should use EnvInject Plugin. or If you are running Docker from the pipeline, then set Environment variable in Jenkins, then access these environment variables in Docker run command.
in the pipeline, you can access environment variable like this
${env.DEVOPS_KEY}
So your Docker run command will be
docker run -p -e myEnV=${env.DEVOPS_KEY}
But make sure you have set DEVOPS_KEY in the Jenkins server.
Using EnvInject it pretty much simple.
You can also inject from the file.
For your Second Question, Yes just the pull the image from docker-hub and use it.
Anyone from your Team can pull and run so only the Jenkins server will build and push the image. So it will save time for others and the image will be up to date and will also available remotely.
Never push or keep sensitive data in your Docker image.
Using Docker Hub or any kind of registry like Sonatype Nexus, Registry, JFrog Artifactory helps you to keep your images with their tags and share it with anyone. It also means that the images are safe there. If your local environment goes down, the images will stay there. It also helps for version control. If you are using multibranch pipelines, that means that you probably will generate different images with different tags.
Running Jenkins, working the jobs, doing the deployment is not a good practice. In my experience from previous work, the best exaples are: The server starts being bloated after some time, Jenkins doesn't work the most important times that you need it, The application you have deployed does not work because Jenkins has too many jobs that takes all the resources.
Currently, I am running different servers for Jenkins Master and Slave. Master instance does not run any jobs, only the master instances do. This keeps Jenkins alive all the time. If slaves goes down, you can simply set another slave.
For deployment, I am using Ansible which can simultaneously deploy the same docker image to multiple servers. It is easy to use and in my opinion quite safe as well.
For the sensitive data such as keys, password, api keys, you are right about using -e flag. You can also use --env-file. This way, you can keep it outside of docker image and keep the file. For passwords, I prefer to have a shell script that generates the passwords in environment files.
If you are planning to use the environment as it is, you can keep the value that you are going to set as environment variable inside Jenkins safely. then you can get that value as a variable. You can see it in Jenkins website

Adding Jenkins system property doesn't survive a restart

Trying to add new system properties to Jenkins through its GUI through script console like :
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
and some other properties but they're removed whenever I restart Jenkins.
Is there a way to save the properties and survive the restart of Jenkins or is there any other way other than script console to add those properties?
PS: I'm running Jenkins on AWS ECS container and I don't have /etc/sysconfig/jenkins or /etc/default/jenkins path to add the setting to the java args.
To set system properties permanently in a official Jenkins docker image, you need to pass it as JVM arguments to the container through the variable environment JAVA_OPTS.
For example :
docker run --name=docker-jenkins --env JAVA_OPTS="-Dhudson.model.DirectoryBrowserSupport.CSP=\"\""
So, on AWS ECS, you need to find a way to pass environment variable to the container. Maybe, this topic can help you.

Pre Configured Environment variables in Jenkins

I am running Jenkins though the docker image.
I want my Jenkins Installation to come up with few pre configured enviroment variables.
Is there any way I can copy some of my environment variables, so that when I bring my jenkins up, it should come with these pre-configured environment variables ?
Or
Where are the environment variables stored in the Jenkins installation ?
TIA.

Jenkins Swarm plugin - Evironmental variables

I have latest Jenkins and using it's latest Swarm Plugin.
I have written Ansible modules/roles/playbooks to setup install various tools/configuration on a given target node (which I would like to use as a Swarm slave node).
After Ansible playbook run is complete, I now see a new Slave is created and attached to my Jenkins master but Swarm Plugin's docs (Available Options) doesn't mention how to create ENVIRONMENT variables in the slave. https://wiki.jenkins-ci.org/display/JENKINS/Swarm+Plugin
My question is:
How can I have multiple slaves created on a same target machine and they all have their own individual settings for setting various tools like JAVA_HOME, M2_HOME, GRADLE_HOME, PATH etc.
How can I set ENVIRONMENT variables for a slave using Swarm plugin?
This is required as if I created a slave whose default JAVA is jdk1.7.0_67, then I would like to create another slave whose default JAVA_HOME is jdk1.8.0_45. Similarly, the end goal is to have various flavors of such slaves with various tools if possible, where each slave's tools are slightly different. I'll assign the LABEL(s) accordingly and use it in a Jenkins job's configuration so that a job runs only using / on these slave if the associated label is assigned/tied to the job.
I tried using https://github.com/MovingBlocks/GroovyJenkins/blob/master/src/main/groovy/AddNodeToJenkins.groovy but not sure how I can automatically define/set ENVIRONMENT variables in the slave's configuration.
I'm assuming you're running on Linux here.
You can have a shell script to export the new environment before calling the swarm-client. These variables will be inherited by the new swarm slave
https://unix.stackexchange.com/questions/130985/if-processes-inherit-the-parents-environment-why-do-we-need-export
Alternatively you could run docker and have a separate swarm slave containers https://hub.docker.com/r/csanchez/jenkins-swarm-slave/ and put your specific install into the Dockerfile and add a new ENTRYPOINT in the bottom of the Dockerfile
ENTRYPOINT ["/usr/local/bin/jenkins-slave.sh" \
"-labels", "label1", "-labels", "label2"]

Resources