Create a for-each in .gitlab-ci to run multiple variables

Working with GitLab is awesome this tool has a lot of features and I really like it, one of the advantages is its big community and documentation also there are tons of ideas to improve this tool.

Recently working in a pipeline I realized that it could be great to have a variable like a list in order to have something like a while or a for-each sadly I couldn’t see something similar to use.

For that reason, I made this simple workaround, pass a variable like a string then create a document like a txt and finally apply a xagrs command.

Challenge.

Create a standard CI/CD for python projects those projects use different packages from many sources for that reason it is necessary to repeat many times the curl instruction.

Sample of .gitlab-ci with multiple lines to reapet the curl

  • before_script:
  • #download and package content
    • – curl sS -o paquete1.tar http://www.regner.com.mx/instalando-jenkins-en-una-vm-centos/
    • – curl sS -o paquete2.tar https://stackoverflow.com/questions/8445445/
    • – curl sS -o paquete3.tar https://stackoverflow.com/questions/

Solution.

Create a variable to set those different url’s and having a simple “while”

  1. create our variable.
  2. create a txt file with a valid format to be sorted
  3. apply the curl instruction in each line
#variable in one single line \n  is used to have new rows 
NECESARYPACKAGES: -sS -o paquete1.tar http://www.regner.com.mx/instalando-jenkins-en-una-vm-centos/ \n -sS -o paquete2.tar https://stackoverflow.com/questions/8445445/ \n -sS -o https://stackoverflow.com/questions/

Having a variable we could set multiple rows or instructions separated by \n

Now we are going to pass the content of that variable as a txt file. with a simple echo

 - echo -e "${NECESARYPACKAGES}" >> packsgen.txt

the result of this is a packsgen.txt with this content:

sS -o paquete1.tar http://www.regner.com.mx/instalando-jenkins-en-una-vm-centos
sS -o paquete2.tar https://stackoverflow.com/questions/8445445
sS -o paquete3.tar https://stackoverflow.com/questions/

Time to apply our curl sentence.

- xargs -n4 curl < packsgen.txt

-n4 is used because our lines are composed each 4 components sS -o tarfile URL if you don’t have this way to separate each line you could get an error because xargs + curl will take the file content as a single row.

Try by your self:

In a console or terminal window.

echo -e "-sS -o paquete1.tar http://www.regner.com.mx/instalando-jenkins-en-una-vm-centos/ \n-sS -o paquete2.tar https://stackoverflow.com/questions/8445445/ \n-sS -o paquete3.tar https://stackoverflow.com/questions/" >> packsgen.txt
xargs -n4 curl < packsgen.txt

This has to produce these files: 3 packages.tar and 1 txt file.

In the end, you could parametrize the content in one variable to reuse code and your Gitlab-ci will appear nice.

Before
After

Instalando Jenkins en una VM Centos.

1. Tener una VM de AWS, Azure, GCP o una vm local base centos 7.7
Necesitamos tener java instalado y revisar que cumplimos con los requerimientos que se piden en la pagina oficial. 
https://wiki.jenkins.io/display/JENKINS/Installing+Jenkins

sudo yum install java
#java -version just to check the status 

2. Configurar el home directory para agregar nuestra variable de ambiente de java.

para esto nos dirigimos al directorio por default cd */usr/lib/jvm* dentro encontraremos otra carpeta con la version de java instalada y debe hacer match con el resultado de java -version

[root@ip-172-31-40-7 jvm]# java -version
 openjdk version "11.0.6" 2020-01-14 LTS
 OpenJDK Runtime Environment Corretto-11.0.6.10.1 (build 11.0.6+10-LTS)
 OpenJDK 64-Bit Server VM Corretto-11.0.6.10.1 (build 11.0.6+10-LTS, mixed mode)
cd /usr/lib/jvm/java-11-amazon-corretto.x86_64

Abrimos el bash profile y modificamos con las siguientes lineas

vi ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
export JAVA_HOME=/usr/lib/jvm/java-11-amazon-corretto.x86_64
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin

export PATH

Revisando que todo esta ok…

#iniciamos sesión nuevamente
su -
echo $JAVA_HOME
#/usr/lib/jvm/java-11-amazon-corretto.x86_64 

Ahora vamos a instalar Jenkins con los comandos de la pagina oficial.

sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum install jenkins
service jenkins start
chkconfig jenkins on
service jenkins status

Si estas usando una maquina virtual en la nube recuerda habilitar el puerto 8080.

el pass: cat /var/lib/jenkins/secrets/initialAdminPassword
sale de ejecutar esto en la vm

Camandos utiles. >_

Kubernetes ADM

Expose a port and kubernetes endpoint i.e Kubernetes dashboard

kubectl proxy --disable-filter=true --port=8001 --address 0.0.0.0 --accept-hosts .*

kubectl patch svc <‘nombre_servicio’> -p ‘{“spec”:{“externalIPs”:[“<ip>”]}}’ -n <‘namespace’>

kubectl get deploy -n <‘namespace’> -o name | awk ‘{print $1}’ | grep -v “NAME” | xargs -I % kubectl scale % –replicas=1 -n <‘namespace’>

sed -i ‘s/<‘palabra a remplazar ‘>/<‘nueva palabra’>/g’ archivo.txt

kubectl get node

kubectl describe pods ${POD_NAME}

kubectl config rename-context kubernetes-admin@kubernetes algo-dev

kubectl taint nodes master node-role.kubernetes.io/master-

kubectl label nodes master type=standard

kubectl scale –replicas=1 deployment.extensions/algo-ingressgateway -n algo-prod

Linux

Look for Back Ground process

ps -fea | grep <“palabra_clave”>