How to get job variables injected into the docker execution? - docker

I wonder if this is already part of the system...
I need to use the current gitlab user id and email ($GITLAB_USER_ID, $GITLAB_USER_EMAIL) injected into the execution of the docker image (to later configure the git repository).
Is there a magic way to do this ? or should I explicitly write the export commands into my .gitlab-ci.yml file (as a before_script for example) ?
Thanks.

I got my response by trying the env command on a build.
So yes every job variables are available into the docker execution env.

Related

passing envvars to docker containers in GoCD

We have a docker container and the program that runs inside expects a number of envvars to be set. There are several ways to do this and I was curious as to what the correct way is:
1- put envvars in a config repo, add it as a material, use docker --env-file flag to pass the file. Cons: seems like overkill for passing 10 envvars.
2- define envvars in the job configuration tab. Export them during docker build. Cons: Every new job would have to manually set the envvars, adding an envvar would require changing all jobs in one place.
3- define envvars in the job configuration tab. Pass them during docker run using -e flag. Cons: Every new job would have to manually set the envvars, adding an envvar would require changing each job in two places.
Is there any other way?
There's another option: Write a small shell script that passes all environment variables to docker that start with a certain prefix.
For example, you could make it turn DOCKER_A=a and DOCKER_B=b into -e A=a -e B=b, and call it as
docker $(./munge_env_vars) ...
Then you can put all your environment variables in one place (either in the GoCD config, or in a shell script under version control that you can source), and no need to modify two places when you add another env variable.

Creating Docker image and running as service in Jenkins

I have a JSP website. I am building DevOps pipeline. I am looking for help to integrate Jenkins with the Docker.
I already have docker file which does task of Deploying war file to the tomcat server.
(Command1)
Through the command line I can run the docker file and create an image.
I can run created image as a service and able to browse the website.
(Command2)
I want to do these two steps in Jenkins. I need your help to integrate these two commands in Jenkins, so that I need not to run these two commands manually one after other.
I think that you can use the "Docker Pipeline Plugin" for that.
For the first command, you can have a stage that runs:
myImage = docker.build("my-image:my-tag")
If you need you can have another stage where you can run some tests inside the image with:
myImage.inside {
sh './run-test.sh'
}
Finally, you can push the image to the repository to your repository with:
docker.withRegistry('https://your-registry.com', 'credentials_id') { //use a second parameter if you repository requires authentication
myImage.push('new_tag') //You can push it with a new tag
}
Please note that if you wanna use the docker.* methods in a declarative pipeline you must do it inside a script step or in a function.
(More info in the plugin's user guide)
For the second command, you only have to update the running image in the server. For doing that you have a lot of options (docker service update if you're using Docker Swarm, for example) and I think that part is outside of the scope of this post.

Extract user information from the build

Jenkins ver. 2.73.3
I have a sample build task that is triggered by a commit to a Github repository. This is how the build information looks:
We need to write this username to a separate file and store it in a particular location. How can I achieve it?
**********Edit-1**********
Added a build step that executes a shell command to write the variable GIT_COMMITTER_NAME to a file. This fails(empty file) but if I write, say JENKINS_URL, it is written to the file:
I guess the github plugin doesn't set, by default, the variables like GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL etc.
Taking a cue from this answer, I proceeded with using the placeholders of the 'pretty option' of git show command. I added the following command in the 'Execute Shell' build step of Jenkins job:
git show -s --pretty='GIT_AUTHOR_NAME='%aN%n'GIT_AUTHOR_EMAIL='%aE%n'GIT_COMMITTER_NAME='%cN%n'GIT_COMMITTER_EMAIL='%cE >> github.properties
The output:
GIT_AUTHOR_NAME=LastName FirstName
GIT_AUTHOR_EMAIL=FirstName.LastName#company.com
GIT_COMMITTER_NAME=GitHub Enterprise
GIT_COMMITTER_EMAIL=noreply#github.company.com
Instead of echo $variable name execute env in shell, it will give you all environment variables at the time of execution and then you can pick the correct variable. (From Gitlab to Jenkins its $gitlabUserName)

jenkins pipeline DOCKER_HOST

I need to run a docker-container inside my pipeline.
My problem is, there is no docker.sock available inside the Jenkins-container. And actual no chance to get it.
But I found some jobs using docker with this Option:
"Inject environment variables to the build process" -> "Properties
Content"
And following configured:
DOCKER_HOST=tcp://<ip>:<port>
DOCKER_CERT_PATH=/var/jenkins_home/certs
In my understanding, this is equivalent to the docker.sock and useable as plugin, isnt it?
But how can i use it inside a (multi-)pipeline project?
I've tried using this Block inside my Note:
environment {
DOCKER_HOST = 'tcp://<ip>:<port>'
DOCKER_CERT_PATH = '/var/jenkins_home/certs'
}
But got same issue: "docker: not found"
I might have a logical fallacy. Hope someone could help.
Otherwise is it possible to create a jenkins-slave including a docker.sock?
But got same issue: "docker: not found"
This indicates that your Jenkins slave, the one running the pipeline script, does not have the docker command line tools. This depends on your distribution, but in my case I fixed it by changing my build-slave/pipeline-runner creation steps to include:
yum install -y docker-client
Note that you'll still need that for the Cloudbees docker plugin (the thing which provides stuff like docker.build() and docker.image()) because it translates those nice pipeline directives down into shell commands.

Jenkins: Access bitbucket payload in shell

According to the documentation of the bitbucket plugin for Jenkins (https://wiki.jenkins-ci.org/display/JENKINS/BitBucket+Plugin) it should be possible to access the payload infos through the environment varaible $BITBUCKET_PAYLOAD.
However when in my build I run the command printenv, there is no environment variable called $BITBUCKET_PAYLOAD, and nothing related to it.
So it's impossible for me to access informations I need to configure my build.
You can trigger Jenkins with Generic Webhook Trigger instead.
Then you can create a variable everything having the JSONPath $. Then everything will resolve to the entire JSON payload. So you can have a shell script build step like:
echo $everything

Resources