How to Make Jenkins Request User Data Available as an Environment Variable

Jenkins jobs serves many purposes. One of the important ones is to use them as self service type of jobs. Usually, you make them available to internal customers. When there are issues, the first thing you want to know may be who requested the job.

I know there is a plugin that makes BUILD_USER_ID available as a environment variable, but I will show you a little trick that doesn’t require another plugin.

First go to Configure section the job you are working on. Click Inject environment variables to the build process under Build Environment section.

Copy and paste the following code in Groovy Script section.

def map = ["REQUEST_USER": currentBuild.getCauses()[0]['userId']]
return map

When you run a build, the REQUEST_USER environment variable is available throughout your build. This is very convenient for logging purposes.  If you check the Environment Variables page for the build the key and the value are there.

Describing it in Job DSL, it looks like the following.

job('foo') {
    environmentVariables {
		loadFilesFromMaster(false)
		keepSystemVariables(true)
		keepBuildVariables(true)
		overrideBuildParameters(false)
        groovy('''def map = ["REQUEST_USER": currentBuild.getCauses()[0]['userId']]
return map''')
	}
}

 

Building Bash Muscle (9)

Let’s replace a text with some other text. I’m going to replace ‘hello’ in ‘hello world’ with ‘hi’. The result will be ‘hi world’. I’m going to have ‘hello world’ in a variable.

var1='hello world'
echo $var1

The following code replaces hello with hi and print the result out in the terminal.

echo ${var1//hello/hi}

You can alternatively use regular expression.

var2='food moon red green monster'
echo ${var2//?oo?/whatever}

The result looks like the following.

whatever whatever red green monster

To recap, the syntax is ${variable//replacefrom/replaceto}

Automating CentOS 7 Joining Windows Domain in bash

I previously wrote an article about CentOS 7 joining a Windows domain. I have come up with a way to automate it in bash script. It’s time for me to utilize my bash muscle I built in the past. 🙂 Here is the script. Make sure to execute it as root.

#!/bin/bash
set -e
set -u

while getopts "u:p:d:g:" arg;
do
	case $arg in
		u) username=$OPTARG;;
		p) password=$OPTARG;;
		d) domain=$OPTARG;;
		g) sudoergroup=$OPTARG;;
		\?) echo "Invalid option -$OPTARG" >&2;;
	esac
done

yum install sssd realmd oddjob oddjob-mkhomedir adcli samba-common samba-common-tools krb5-workstation openldap-clients policycoreutils-python -y

echo "Joining $domain"
echo "$password" | realm join --user=$username $domain

sssd_conf_path=/etc/sssd/sssd.conf

echo "Modifying $sssd_conf_path"
sed -i 's/use_fully_qualified_names = True/use_fully_qualified_names = False/g' $sssd_conf_path
sed -i 's/fallback_homedir = \/home\/\%u@\%d/fallback_homedir = \/home\/\%u/g' $sssd_conf_path

echo "Restarting sssd"
systemctl restart sssd

echo "Granting $sudoergroup to be sudoers"
sudoergroup=${sudoergroup// /\\ } #replace space with \space
echo "%$sudoergroup@$domain ALL=(ALL) ALL" >> /etc/sudoers.d/sudoers

echo "Completed joining $domain"

Make sure that -u indicates a domain user that has a permission to have computers join the domain. Execute the script like the following example.

./join-windows-domain.sh -u 'domainadminuser' -p 'yourpassword' -d 'homenet.iriumi.ad' -g 'domain admin'

This bash script really eliminates all the manual steps to join a CentOS 7 to Windows domain. This script can be used on pretty much all the Red Hat based Linux distributions.