grep command with regex not working on gitlab ci - grep

I am running script in my gitlab ci:
pre_build:
stage: get_info
script:
- printenv
- cd ./scripts
- ./pre_build.sh
this script has the following line:
todays_date=$(date +"%d-%b-%Y")
latest=$(curl -i ${artifactory_url} | grep develop | grep -i ${todays_date} | grep ${last_ok_build_number} | grep -Eoi '<a [^>]+>' | grep -Eo 'href="[^\"]+"')
I believe this is failing due to regex in grep, any idea how to run this in GitLab ci?
Above script works perfectly well on my local machine
EDIT: Above codes works it was timezone issue

Related

how to trigger and monitor Jenkins job from CI and getting results after is finished

I am trying to implement a script in gitlab CI to trigger a smoke test via Jenkins and then get the results.
So far I am able to trigger the job successfully and I am trying to follow this to implement a monitoring stage and then get the result once the job finishes.
my issue is that I implemented a while loop to monitor if the Jenkins job has finished so far the script is giving either syntax errors (when copied in gitlab) or if run in the terminal I get:
job is building? true
waiting...
parse error: Invalid numeric literal at line 2, column 0
job is still building?
job is building?
This is what I am using so far:
#!/bin/bash
running="true"
while [ "$running" != "false" ]
do
echo "job is building? ${running}"
echo "waiting...";
sleep 2;
running=$(curl -s --user ${EMAIL}:${TOKEN} ${URL}/${var}/lastBuild/api/json | jq .'building')
echo "job is still building? ${running}"
done
echo "Done!"
buildNumber=$(curl -s --user $EMAIL:$TOKEN ${URL}/$ENV-${var}/lastBuild/api/json | jq ".url" | awk -F "/" '{print $(NF-1)}')
echo "getting results for build ${buildNumber}"
curl -s --user ${EMAIL}:${TOKEN} ${URL}/${ENV}-${var}/lastBuild/api/json | jq ".url" | awk -F "/" '{print $(NF-1)}'
curl -v --silent --user ${EMAIL}:${TOKEN} ${URL}/${ENV}-${var}/lastBuild/consoleText 2>&1 | grep -i "finished:"
UPDATE
the script is running now ok in my local terminal
the change was
running="true"
while [ "$running" != "false" ]
do
echo "job is building? ${running}"
echo "waiting..."
sleep 2
curl -s --user $EMAIL:$TOKEN $URL/$ENV-${var}/lastBuild/api/json --output now.txt
running=$(jq .'building' now.txt)
echo "job is still building? ${running}"
done
The problem is still on Gitlab CI as after copy pasting this script I get in the pipeline
/bin/sh: eval: line 149: syntax error: unexpected "done"
I'm guessing that the error
parse error: Invalid numeric literal at line 2, column 0
comes from the line that does the curl
running=$(curl -s --user ${EMAIL}:${TOKEN} ${URL}/${var}/lastBuild/api/json | jq .'building')`
I'm assuming that the error is thrown by jq when it tries to parse the json and fails. Since this line has an error, the variable running never gets properly updated, meaning that the rest of the script doesn't work as intended.
If you fix this line, the rest of your pipeline should work. Consider looking into this question which has a similar problem and some solutions.

Gitlab runner fails executin a valid shell command

I have a following GitLab CI/CD job defined in my gitlab-ci.yml:
image: docker:stable
stages:
- ...
...
build-dependencies-init:
stage: checks
tags:
- ...
needs:
- check-registry
script:
#- wget --spider --server-response http://$DOCKER_REGISTRY_URL/v2/repo/image/manifests/latest 2>&1 | grep -m 1 "HTTP/" | awk '{print $2}'
- STATUS=$(wget --spider --server-response http://$DOCKER_REGISTRY_URL/v2/repo/image/manifests/latest 2>&1 | grep -m 1 "HTTP/" | awk '{print $2}')
- if [ $STATUS -eq 200 ]; then
- echo "Image is available"
- else
- echo "Building dependencies"
- docker build -t $DOCKER_REGISTRY_URL/repo/image:latest .
- echo "Pushing dependencies"
- docker push $DOCKER_REGISTRY_URL/repo/image:latest
- fi
I would expect, that it would store the status code into the variable STATUS which I can then use to decide if building new image is necessary. I dont want to build new image every time.
The problem is that when this thing fails (STATUS stores 404 for example) the runner decides to stop execution and starts cleaning up... it does not evaluate the if/then/else code that follows.
Why this happens? How do I prevent GitlabRunner from stopping early? The commented out code prints out 404 as it should (if uncommented), but i do not expect it to terminate at this point in execution.
I am using containerised GitlabRunner in Docker, with a mounted docker.sock.

Docker cli: docker run command with quoted arguments

I'm trying to run execute jest -t variant with the following Docker cli command:
docker run -it node-jest npx jest -t "This string matches exactly one test"
Which does not do the same thing if I were to run npx jest -t "This string matches exactly one test" locally.
It appears that double quotes are being stripped/ignored and only This is getting passed to jest -t. It appears that This string matches exactly one test is getting split up on spaces and treated as individual arguments. Can someone explain why that is happening, and how to get "This string matches exactly one test" passed in to docker run correctly (hopefully in a readable/sane way)?
You did not mention the error, and the quotes seems fine and it should work or run the container with shell, but my assumption is you did not set the WORKING directory in your Dockerfile or there is something wrong with Dockerfile
Here is working example taking from jest docker image with some testing code.
docker run -ti adiii717/jest sh -c 'npx jest -t "it should filter by a search term (link)"'
output:
Ran all test suites with tests matching "it should filter by a search term (link)".
-----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------------|----------|----------|----------|----------|-------------------|
All files | 12.5 | 0 | 0 | 16.67 | |
filterByTerm.js | 12.5 | 0 | 0 | 16.67 | 2,3,4,5,6 |
-----------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 skipped, 0 of 1 total
Tests: 3 skipped, 3 total
Snapshots: 0 total
Time: 1.109s
Ran all test suites with tests matching "it should filter by a search term (link)".
Here is the Dockerfile
FROM node:alpine
RUN apk add --no-cache git
RUN npm install jest npx -g
WORKDIR /app
RUN git clone https://github.com/valentinogagliardi/getting-started-with-jest.git /app
RUN npm install

Extract App Version from versions.sbt through Command line

Is there a way / command - I can extract an App version from a versions.sbt file ?
You have two ways to do this:
ask sbt for the app version, this will work with any sbt project:
sbt 'show version' | tail -n1 | cut -d ' ' -f2
parse the version.sbt file:
cat version.sbt | cut -d '"' -f2

Rebuild a docker image depending on the output of a command

Say I want to rebuild my docker image when a new compiler version is available in some repository.
I can gather the version inside the container:
FROM centos:7
RUN yum info gcc | grep Version | sort | tail -1 | cut -d: -f2 | tr -d ' '
If I build this container and tag it as base, I can use that information and setup a second container:
FROM base
RUN yum install gcc-4.8.5
Docker will be able to cache the second container and not rebuild it, when the compiler version has not changed. But creating that requires some shell scripting and that might be brittle in, e.g., a continuous integration scenario.
What I would like to do is to introduce a unified source for these two containers. Is there a way to write something like this:
FROM centos:7
$GCC_VERSION=RUN yum info gcc | grep Version | sort | tail -1 | cut -d: -f2 | tr -d ' '
RUN yum install gcc-$GCC_VERSION
and have the variables expanded (and the commands still cached) during docker build ?
You can use the ARG instruction. Build args affect the cache in the way you want to: Impact on build caching
With a Dockerfile like this:
FROM centos:7
ARG GCC_VERSION
RUN yum install -y gcc-$GCC_VERSION
The image gets only rebuilt if the GCC_VERSION changes.
docker build --build-arg GCC_VERSION=$(docker run centos:7 yum info gcc | grep Version | sort | tail -1 | cut -d: -f2 | tr -d ' ') .

Resources