Use multiline string in Gitlab release-cli description - gitlab

I want to create a release in the gitlab-cli with a multiline description but when add one by yaml specs I get the following error:
Executing "step_release" stage of the job script
/bin/sh: eval: line 150: syntax error: unexpected newline
$ release-cli create --name "Release $CI_COMMIT_TAG" --description "Release created using the release-cli. # collapsed multi-line command
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 2
Job:
create release:
...
release:
tag_name: $CI_COMMIT_TAG
name: 'Release $CI_COMMIT_TAG'
description: |-
Release created using the release-cli.
Maven dependency
```xml
<dependency>
<groupId>group</groupId>
<artifactId>artifact-id</artifactId>
<version>$CI_COMMIT_TAG</version>
</dependency>
```
(also tried with > and |)
I tried it manually with \n but that is literally added in the release as
Release created using the release-cli.\n\n ...
How do I create a release via the release-cli with a multiline (markdown) description?

I found a dirty unreadable workaround but it works.
(Note that multiline echo doesn't work either)
create release:
...
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
script:
- echo "Running the release job for version '$CI_COMMIT_TAG'."
- set -e
- |
echo "Release created using the release-cli." > release_notes.md
echo "" >> release_notes.md
echo "Maven dependency:" >> release_notes.md
echo "\`\`\`xml" >> release_notes.md
echo "<dependency>" >> release_notes.md
echo " <groupId>group</groupId>" >> release_notes.md
echo " <artifactId>artifact-id</artifactId>" >> release_notes.md
echo " <version>$CI_COMMIT_TAG</version>" >> release_notes.md
echo "</dependency>" >> release_notes.md
echo "\`\`\`" >> release_notes.md
release:
tag_name: $CI_COMMIT_TAG
name: 'Release $CI_COMMIT_TAG'
description: release_notes.md
Still searching for a better solution.

Related

If condition is failing in the GitLab CI

following is my Gitlab CI code:-
stages:
- check
variables:
JIRA_HEADER: "Accept: application/json"
jira:
stage: check
before_script:
#- apk add jq curl
- apk add --no-cache bash jq curl
image: python:3.7.4-alpine3.9
script:
- export MERGE_REQUEST_JIRA_ID=$(echo ${CI_MERGE_REQUEST_TITLE} | sed -r "s/^([A-Za-z][A-Za-z0-9]+-[0-9]+).*/\1/")
- echo $CI_MERGE_REQUEST_TITLE
- export JIRA_DETAIL=$(curl -u ${JIRA_USERNAME}:${JIRA_PASSWORD} -H "${JIRA_HEADER}" -X GET https://${JIRA_SERVER}/rest/api/2/issue/${MERGE_REQUEST_JIRA_ID}?fields=status)
- echo $JIRA_DETAIL
# extract the JIRA key id, this also validates JIRA issue referenced is valid
- export JIRA_KEY_ID=$(echo ${JIRA_DETAIL} | jq -e '.key')
- echo $JIRA_KEY_ID
# extract the JIRA status
- export JIRA_STATUS=$(echo ${JIRA_DETAIL} | jq '.fields.status.name')
- echo $JIRA_STATUS
- |
if [[ "$JIRA_STATUS" == "^(Done|Completed|Closed)$" ]]
then
echo "Invalid JIRA (Done/Completed/Closed) found!"
exit 1
else echo "Valid JIRA Id found!"
fi
only:
- merge_requests
I'm trying to validate the JIRA status by calling its API after retrieving Jira id from title of Merge Request. There is a problem in the If condition below if [[ "$JIRA_STATUS" == "^(Done|Completed|Closed)$" ]] as it is not validating it properly. Every time, the else condition is getting executed and printing the message as Valid JIRA Id found!
I would really appreciate if someone can help me to fix this minor issue. I want to gracefully exit the job with this message in the if block as Invalid JIRA (Done/Completed/Closed) found! whenever the Jira status found to be in any of the given values as Done, Completed or Closed.
I'm finally able to resolve this issue by modifying the code like below:-
- |
if test -z "$(echo ${JIRA_STATUS} | sed -r "s/\"(Done|Completed|Closed)\"//")"
then
echo "Not a valid Jira (Done/Completed/Closed)"; exit 1
else
echo "Valid Jira found!"; echo $?
fi
I had used the test command along with if-else condition in Linux to make it work

Gitlab CI CD Pipeline Variable Concat Two Variables Not Working

The Problem: I am trying to concat two variables for a copy cmd in a before script for a gitlab ci/cd pipeline job.
What I expect: myfile_filesuffix
What I get: _filesuffix
Can anyone see what I am doing wrong? When I run this for loop on my local CLI I have no problems. Thank you!
before_script:
- rm -rf .terraform
- terraform --version
- mkdir ~/.aws
- echo "[default]" > ~/.aws/credentials
- echo "aws_access_key_id=$AWS_ACCESS_KEY_ID" >> ~/.aws/credentials
- echo "aws_secret_access_key=$AWS_SECRET_ACCESS_KEY" >> ~/.aws/credentials
- mkdir ./deployments
- ls common
- common_files=$(find common -type f)
- echo $common_files
- prefix_common=$(echo $common_files | cut -d"/" -f 1)
- echo $prefix_common
- for f in $common_files;
do
common_file="$(basename $f)"
cp $f ./deployments/""${common_file}"_"${prefix_common}"";
done
you can used GitLab repo settings -> CI/CD -> Variables to add FILE type variable and use mv command move to your folder.
ex: File Type variable is ANSIBLE_VARIABLES_FILE
script:
- mv $ANSIBLE_VARIABLES_FILE ./deployments/variables_common.tf

Gitlab Yamls does not works properly Job needs

I'm facing some issues and would be glad if someone can help me.
My main goal here it's being able to save at the same pipeline, more than one job. The error it’s because the jobs does not exist for the specific branch, since I have 6 branches I don't want to modified one by one. I would like to have only one Yaml version.
When I've tried to save, GitLab show me the following errors.
Found errors in your .gitlab-ci.yml:
jobs:deploy_dc_manual:needs:need job should be a string
You can test your .gitlab-ci.yml in CI Lint.
This, it is a piece of the code. Example if I am in the branch feature the job needs it´s "feature_package_build" If I am at integration branch will be expected "int_package_build".
feature_package_build:
extends: .build
only:
- /^feature\/.*/
script:
# GitLab API query
- LAST=$(curl -s "sensitive data" | jq '.[0] | .sha' | sed '1q;d' | sed 's:^.\(.*\).$:\1:')
- >
if [ "$OLDER_COMMIT" == "none" ]; then
node_modules/sfdx-cli/bin/run sfpowerkit:project:diff -d package -r ${LAST} -x --loglevel debug
elif [ "$OLDER_COMMIT" != "none" ]; then
node_modules/sfdx-cli/bin/run sfpowerkit:project:diff -d package -r $OLDER_COMMIT -x --loglevel debug
fi
int_package_build:
extends: .build
only:
- integration
script:
# GitLab API query
- LAST=$(curl -s "sensitive data" | jq '.[0] | .sha' | sed '1q;d' | sed 's:^.\(.*\).$:\1:')
- >
if [ "$OLDER_COMMIT" == "none" ]; then
node_modules/sfdx-cli/bin/run sfpowerkit:project:diff -d package -r ${LAST} -x --loglevel debug
elif [ "$OLDER_COMMIT" != "none" ]; then
node_modules/sfdx-cli/bin/run sfpowerkit:project:diff -d package -r $OLDER_COMMIT -x --loglevel debug
fi
uat_package_build:
extends: .build
only:
- uat
script:
# GitLab API query
- LAST=$(curl -s "sensitive data" | jq '.[0] | .sha' | sed '1q;d' | sed 's:^.\(.*\).$:\1:')
deploy_DC_Manual:
extends:
- .deployDC_Manual
needs:
- job:
if [uat_package_build]; then
fi
if [feature_package_build]; then
fi
only:
- /^feature\/.*/
- integration
- uat
- release
- master
script:
- nomDeployedDC=${NONDEPLOYEDDC}
- >
if [ -f package/destructiveChanges.xml ] && [ "$VALIDATE" == "no" ]; then
I assume what you are looking for is the optional-flag for the needs keyword like
deploy_DC_Manual:
extends:
- .deployDC_Manual
needs:
- job: uat_package_build
optional: true
- job: feature_package_build
optional: true
As this makes the needs conditional - but please be aware, that this also means if you mess up your rules, and none of those two is available, your job might be still executed.

Gitlab CI: Facing error "Syntax error near unexpected token `else' " in gitlab ci job

Here is my yml file:
stages:
- stage1
job1:
stage: stage1
script:
- >
#!/bin/sh
cd .
echo "checking whether the ctl file exists"
if [ -s something.ctl ]
then
echo "exists"
else
echo "does not exist, exiting gitlab pipeline"
exit -1
fi
When the pipeline runs it fails with exit code 2 and I see this message in the log:
Syntax error near unexpected token `else'
What do I need to change here?
This script section work (using bash shell) :
script:
- cd .
- echo "checking whether the ctl file exists"
- >
if [ -s something.ctl ]; then
echo "exists";
else
echo "does not exist, exiting gitlab pipeline";
exit 1;
fi
Also be aware that to exit a job properly, code must be 0 (succeed) or 1 (fail)
The issue resolved after I replaced "- >" with "- |-"
so now my file looks like
script:
- |-
echo "..."
echo....

GitLab CI syntax to write FOR loop statement?

Below is the script mentioned in the gitlab-ci.yml file. This GitLab CI configuration is valid. But, when the CI/CD build is run, the job fails. Is it something to do with the FOR loop syntax?
deploy_dv:
stage: deploy_dv
variables:
GIT_STRATEGY: none
script:
- echo "Deploying Artifacts..."
- echo "Configure JFrog CLI with parameters of your Artifactory instance"
- 'c:\build-tools\JFROG-CLI\jfrog rt config --url %ARTIFACTORY_WEBSITE% --user %ARTIFACTORY_USER% --apikey %APIKEY%'
- 'cd ..\artifacts'
- 'SETLOCAL ENABLEDELAYEDEXPANSION'
- FOR %%i in (*) do (
'c:\build-tools\curl\bin\curl.exe --header "PRIVATE-TOKEN:%HCA_ACCESS_TOKEN%" --insecure https://code.example.com/api/repository/tags/%CI_COMMIT_TAG% | c:\build-tools\jq\jq-win64.exe ".release.description" > temp.txt'
'set /p releasenote=<temp.txt'
'rem del temp.txt'
'set mydate=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%'
'c:\build-tools\JFROG-CLI\jfrog rt u "%%i" %ARTIFACTORY_ROOT_PATH%/%PROJECT_NAME%/%%i --build-name=%%i --build-number=%BUILDVERSION% --props releasenote=%releasenote%;releaseversion=%BUILDVERSION%;releasedate=%mydate% --flat=false'
)
- '%CURL% -X POST -F token=%REPOSITORY_TOKEN% -F ref=master -F "variables[RELEASE]=false" -F "variables[PROGRAM]=test" --insecure https://code.example.com/api/repository/trigger'
only:
- /^(dv-)(\d+\.)(\d+\.)(\d+)$/
I get this below error:
$ echo "Deploying Artifacts..."
"Deploying Artifacts..."
$ echo "Configure JFrog CLI with parameters of your Artifactory instance"
"Configure JFrog CLI with parameters of your Artifactory instance"
$ c:\build-tools\JFROG-CLI\jfrog rt config --url %ARTIFACTORY_WEBSITE% --user %ARTIFACTORY_USER% --apikey %APIKEY%
Artifactory server ID [Default-Server]: $ cd ..\artifacts
$ SETLOCAL ENABLEDELAYEDEXPANSION
$ FOR %%i in (*) do ( 'c:\build-tools\curl\bin\curl.exe --header "PRIVATE-TOKEN:%HCA_ACCESS_TOKEN%" --insecure https://code.example.com/api/repository/tags/%CI_COMMIT_TAG% | c:\build-tools\jq\jq-win64.exe ".release.description" > temp.txt' 'set /p releasenote=<temp.txt' 'rem del temp.txt' 'set mydate=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%' 'c:\build-tools\JFROG-CLI\jfrog rt u "%%i" %ARTIFACTORY_ROOT_PATH%/%PROJECT_NAME%/%%i --build-name=%%i --build-number=%BUILDVERSION% --props releasenote=%releasenote%;releaseversion=%BUILDVERSION%;releasedate=%mydate% --flat=false' )
The filename, directory name, or volume label syntax is incorrect.
ERROR: Job failed: exit status 255
Since there is still no good answer to this question, I will give it a try. I used this snippet to start multiple Docker builds for every directory in my repository. Notice the |+ and the > characters, which lets you put multi-line commands in YAML and are part of GitLab syntax.
Linux example:
build:
stage: loop
script:
- |+
for i in $(seq 1 3)
do
echo "Hello $i"
done
Windows example:
build:
stage: loop
script:
- >
setlocal enabledelayedexpansion
for %%a in ("C:\Test\*.txt") do (
set FileName=%%~a
echo Filename is: !FileName!
)
endlocal
Here is a working example of a job in a .gitlab-ci with a loop running on GNU/Linux OS and using Sh/Bash shell :
edit:
stage: edit
script:
- for file in $(find ${CI_PROJECT_DIR} -type f -name deployment.yml)
do
CURRENT_IMAGE=$(grep "image:" $file | cut -d':' -f2- | tr -d '[:space:]' | cut -d':' -f3)
sed -ie "s/$CURRENT_IMAGE/$VERSION/g" "$file"
done
only:
- master
I'm not an expert on Gitlab-Runner on Windows but Windows Batch is default shell used but you can also use Powershell.
In .gitlab.yml anything you write under "script" is shell. Thus for loop will be same as it works in shell script.
for var in ${NAME_1} ${NAME_2} ${NAME_3} ; do
*----computations----*
done

Resources