Setting GitLab environmental variable via a bash command - bash

So. I'm using GitLab and in my .gitlab-ci.yml file I have the following:
variables:
CONTAINER_NAME: $CI_BUILD_REF_NAME | tr / -
The $CI_BUILD_REF_NAME basically reads the branch name, in my case development/karl. I'm trying to set the variable name CONTAINER_NAME to development-karl though.
It doesn't seem to be working as when I echo the variable, the following shows:
$ echo $CONTAINER_NAME
development/karl | tr / -
I was expecting:
$ echo $CONTAINER_NAME
development-karl
I've also tried:
variables:
CONTAINER_NAME: $( echo "$CI_BUILD_REF_NAME" | tr / - )
CONTAINER_NAME_2: $( tr / - <<<"$CI_BUILD_REF_NAME" )
CONTAINER_NAME_3: "${CI_BUILD_REF_NAME////-}"
//.. more script stuff
script:
- echo $CONTAINER_NAME
- echo $CONTAINER_NAME_2
- echo $CONTAINER_NAME_3
And got:
Successfully extracted cache
$ echo $CONTAINER_NAME
( echo "development/karl" | tr / - )
$ echo $CONTAINER_NAME_2
( tr / - <<<"development/karl" )
$ echo $CONTAINER_NAME_3
$ npm install

Related

Unterminated quoted string error while running 'kubectl exec' using shell

I am trying to run a kubectl exec command to run the command in the respective containers and transfer their outputs to a file using a shell script. I have the data in a YAML file with name of container, pod name, command and the file name where it is to be stored. I have parsed the YAML file using yq package and am trying to execute the commands. The commands having no quotes are executed successfully but the ones containing quotes result in an error. Collect execs contain name of the file and command.
I have tried running the commands normally on the command line and they seem to execute without any error. The error comes when I store them in a variable and then execute them.
Also doesn't work if I use " or ' or change ' to ".
FUNCTION
get_execs() {
mkdir ${EXECDIR}
for con in $(yq '.containers[] | .name' ${YFILE})
do
# echo $con
x=$(i=$con yq '.containers[] | select(.name == env(i)) | .collect_execs[] | .name' ${YFILE})
# printf "%s\n" "$x"
mkdir ${EXECDIR}/$con
for j in $x
do
c=$(i=$con p=$j yq '.containers[] | select(.name == env(i)) | .collect_execs[] | select(.name == env(p)) | .cmd' ${YFILE})
pod=$(i=$con yq '.containers[] | select(.name == env(i)) | .pod' ${YFILE})
# printf "%s abc\n" "$c"
kubectl exec -n ${NAMESPACE} $pod -c $con -- $c > ${EXECDIR}/$con/$j
done
done
}
YAML FILE STRUCTURE:
containers:
- name: otg-port-eth1-protocol-engine
pod: otg-port-eth1
collect_execs:
- name: resource-usage
cmd: top -c -n 2 -b -H -w 120
- name : disk-space
cmd : df -H
- name: cpu-info
cmd: cat /proc/cpuinfo
- name: interface-manager-threads-iter1
cmd : sh -c 'gdb --eval-command "set pagination 0" --eval-command "thread apply all bt" --batch --pid $(pidof InterfaceManager)'
- name: interface-manager-threads-iter2
cmd : sh -c 'gdb --eval-command "set pagination 0" --eval-command "thread apply all bt" --batch --pid $(pidof InterfaceManager)'
- name: interface-manager-shared-sos
cmd: sh -c 'cat /proc/$(pidof InterfaceManager)/maps'
- name: netstat
cmd: netstat -an
- name: dmesg
cmd : dmesg
- name : ifconfig
cmd : ifconfig
ERROR
--eval-command: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
--eval-command: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
/proc/$(pidof: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
--eval-command: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
--eval-command: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
/proc/$(pidof: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
The commands running with sh -c are causing the errors. To fix this, I changed the YAML structure and broke up the commands into 2 parts: precmd and cmd.
- name: resource-usage
cmd: top -c -n 2 -b -H -w 120
- name : disk-space
cmd : df -H
- name: cpu-info
cmd: cat /proc/cpuinfo
- name: interface-manager-threads-iter1
precmd : sh -c
cmd : 'gdb --eval-command "set pagination 0" --eval-command "thread apply all bt" --batch --pid $(pidof InterfaceManager)'
- name: interface-manager-threads-iter2
precmd : sh -c
cmd : 'gdb --eval-command "set pagination 0" --eval-command "thread apply all bt" --batch --pid $(pidof InterfaceManager)'
- name: interface-manager-shared-sos
precmd : sh -c
cmd: 'cat /proc/$(pidof InterfaceManager)/maps'
And then run the check:
if [ ! "$pc" = null ]
then
kubectl exec -n ${NAMESPACE} $pod -c $con -- $pc "$c" > ${EXECDIR}/$con/$j
else
kubectl exec -n ${NAMESPACE} $pod -c $con -- $c > ${EXECDIR}/$con/$j
fi
This seems to be working for me.

Running Script on container from host

I've made a script to perform an automate update to PHP service in a container.
At some stage i check if the new PHP version is activated with the following commands:
echo ''| update-alternatives --config php | grep $version | grep auto | head -n 1 | tr -d ' \n\r\t '
the "echo" at the beginning canceling the interactive interaction from the 'update-alternatives' command.
the $version variable is an input from the user(OPTARG) and has a default value if not set.
when im trying to save the command output to a variable , the variable is always set to null after executing the command.
i've tried the following ways:
var=$(echo ''| update-alternatives --config php | grep $version | grep auto | head -n 1 | tr -d ' \n\r\t ')
and:
var=`echo ''| update-alternatives --config php | grep $version | grep auto | head -n 1 | tr -d ' \n\r\t '`
The result of the command when im not redirecting the output to a variable is:
0/usr/bin/php8.181automode
Please help
You need to check the failed command piecemeal.
(Note: my system has only one version of php installed so substituting vi for the example below.)
The command
echo '' | update-alternatives --config vi | grep gtk3 | grep auto
reports
{*} 0 /usr/bin/vim.gtk3 50 auto mode
So ... I suggest that your coding doesn't take into account the format for reporting the choices.
Instead of the tr, use
awk '{ for( i=1 ; i < $NF ; i++){ if( index($i,"/") == 1 ){ print $i ; exit } ; } ; }'

Gitlab CI/CD create and use custom user functions

I tried create like this function in my gitlab config file:
deploy:
stage: dev
services:
- docker:dind
script:
- myFunction () { api_pl_tmp=$(curl -s --header "PRIVATE-TOKEN: $TOKEN_VAR" "https://git.example.ru/api/v4/projects/1/pipelines/latest" | jq .) }
- while myFunction; do
- if [ $(echo $api_pl_tmp | jq -r .status) = "success" ]
- then
- export PROJECT_CURRENT=$($api_pl_tmp | jq -r '{id:.id,sha:.sha[0:8]}' | base64)
- break
- fi
- if [ $(echo $api_pl_tmp | jq -r .status) = "failed" ]
- then
- echo "Error: Frontend can't be deployed!"
- exit 1
- fi
- if [ $(echo $api_pl_tmp | jq -r .status) = "running" ]
- then
- echo "Wait 5 sec... Frontend deploying!"
- sleep 5
- else
- echo Unknow status $(echo $api_pl_tmp | jq -r .status)
- exit 1
- fi
- done
But it's doesn't work and gitlab return me error with message:
This GitLab CI configuration is invalid: jobs:deploy-to-dev:script
config should be a string or a nested array of strings up to 10 levels
deep
How I can fix this problem or maybe I have an error in my custom function?
- separates commands with commands in between. Put your commands as one command, not multiple. Remember they are joined with spaces.
The problem with your script is the colon - see https://gitlab.com/gitlab-org/gitlab-foss/-/issues/30097 .
deploy:
stage: dev
services:
- docker:dind
script:
- "colon=:"
- myFunction () {
api_pl_tmp=$(curl -s --header "PRIVATE-TOKEN$colon $TOKEN_VAR" \
"https$colon//git.example.ru/api/v4/projects/1/pipelines/latest" | jq .);
}
- while myFunction; do
if [ $(echo $api_pl_tmp | jq -r .status) = "success" ]; then
export PROJECT_CURRENT=$($api_pl_tmp | jq -r '{id:.id,sha:.sha[0:8]}' | base64);
break;
fi;
if [ $(echo $api_pl_tmp | jq -r .status) = "failed" ]; then
echo "Error$colon Frontend cant be deployed";
exit 1;
fi;
if [ $(echo $api_pl_tmp | jq -r .status) = "running" ]; then
echo "Wait 5 sec... Frontend deploying!";
sleep 5;
else
echo Unknow status $(echo $api_pl_tmp | jq -r .status);
exit 1;
fi;
done
Also jq .) } is missing a ;. Check your scripts in your own shell one at a time first. Check your scripts with https://shellcheck.net .
Also $($api_pl_tmp is missing an echo and there are a lot of problem with quoting. Use consistent indentation and try to write readable code to minimize typos.

Why not working shell command with variables on envoy?

I have simple Envoy setup. A server:
#task('build_todo_list', ['on'=> $server])
echo 'Build todo list'
cd {{ $releases_dir }}
current=$(ls -td -- * | head -n 1 | cut -d'/' -f1)
d=$(echo $current | cut -c 1-8)
echo $d
since=$(date -d $d +"%Y-%m-%d")
until=$(date +"%Y-%m-%d")
echo $since
echo $until
cd {{$source_dir}}
git log staging --pretty="%an %B" --since=$since --until=$until | grep 'Fixed \| Did'
#endtask
But i have an error because i use $since and $until variables in my git log command. And this variables good work with echo command.
Problem has been not variables. If between "since" and "until" not have records - will not be task complete successfully.

bash, md5sum behaves strange

Why is this different?
text="tralala"
echo -n $text | md5sum -
result: def7d827536761c20f449f69262ff20f
echo -n "tralala" | md5sum -
result : 7e4ef92d1472fa1a2d41b2d3c1d2b77a
what am I doing wrong?
I suspect you mistakenly did not provide the -n (output no newline) flag to echo. See sample from my machine below:
$ echo tralala | md5sum
def7d827536761c20f449f69262ff20f -
$ echo -n tralala | md5sum
7e4ef92d1472fa1a2d41b2d3c1d2b77a -
$ text="tralala"
$ echo $text | md5sum
def7d827536761c20f449f69262ff20f -
$ echo -n $text | md5sum
7e4ef92d1472fa1a2d41b2d3c1d2b77a -

Resources