I have a yaml pipeline in Azure DevOps. In one step I am using "bash" task to find a file and create a variable named "ArtifactName". The task is as follows. The issue is it cannot create the variable and the pipeline although runs successfully does not create the variable and prints: "ArtifactName: command not found."
Bash syntax in pipeline:
- bash: |
echo $(Build.SourcesDirectory)
ArtifactName=find $(Build.SourcesDirectory) -name '*.whl'
echo "Artifact name value is " $(ArtifactName)
displayName: 'Find the artifact in source directory'
workingDirectory: $(Build.SourcesDirectory)
The error message is as follows. As it is shown the variable ArtifactName is empty:
I changed the code to make sure it gets some value and see if the problem was from regular expression part but again I get the same error with this code:
- bash: |
echo $(Build.SourcesDirectory)
ArtifactName=$(find $(Build.SourcesDirectory) -name '*.whl')
echo "Artifact name value is " $(ArtifactName)
displayName: 'Find the artifact in source directory'
workingDirectory: $(Build.SourcesDirectory)
The error is like previous part "ArtifactName command not found":
Even when I hard code the value of the variable ArtifactName to a string like "foo", still the same error of "ArtifactName command not found":
- bash: |
echo $(Build.SourcesDirectory)
ArtifactName="foo"
echo "Artifact name value is " $(ArtifactName)
displayName: 'Find the artifact in source directory'
workingDirectory: $(Build.SourcesDirectory)
My last try was to use the below syntax following this link to define a variable in bash, but this one also raised the same issue that ArtifactName is again empty:
- bash: |
echo $(Build.SourcesDirectory)
echo "##vso[task.setvariable variable=ArtifactName;]foo"
echo "Artifact name value is " $(ArtifactName)
displayName: 'Find the artifact in source directory'
workingDirectory: $(Build.SourcesDirectory)
The error is exactly the same:
The question is how can I use bash task to create a variable inside it with some values that I can use in other tasks of the same and other stages.
The question is how can I use bash task to create a variable inside it with some values that I can use in other tasks of the same and other stages.
To meet your requirement, you need to use the command echo "##vso[task.setvariable variable=ArtifactName;]foo" to define the pipeline variable.
Refer to this doc: Set variables in scripts
A script in your pipeline can define a variable so that it can be consumed by one of the subsequent steps in the pipeline.Set variables in scripts
It will not work on the current task, but the updated value can be used in the next tasks.
On the other hand, the format you used to run the find command in bash script has issues.
Refer to my sample:
jobs:
- job: BuildandPublish
steps:
- task: Bash#3
inputs:
targetType: 'inline'
script: |
echo $(Build.SourcesDirectory)
find $(Build.SourcesDirectory) -name '*.yml'
ArtifactName=$(find $(Build.SourcesDirectory) -name '*.yml')
echo "Artifact name value is " $ArtifactName
echo "##vso[task.setvariable variable=ArtifactName;]$ArtifactName"
- task: Bash#3
inputs:
targetType: 'inline'
script: |
echo $(ArtifactName)
In this case, it can get the file name and can be used in the next tasks with the format: $(variablename).
Result:
Related
i wanted to create pipeline that would run in every merge request, and will verify file extension. I have some old file from couple of years with language codes that are no longer used. i wanted this pipeline to find and block merge request if somehow that kind of file is there.
the proper extension would look somehow like that:
for JSON files valid filenames are *.language_code.json or *.json
for RESTEXT files valid filenames are *.language_code.restext or *.restext
List of supported language codes:
fr
ja
zh-Hans
zh-Hant
example of wrong file:
my_file.zh.json
i create gitlab-ci.yml file and wrote something like this:
image: python:3.8
stages:
- test
trigger-.zh:
rules:
- changes:
- "**/*.zh.json"
- "**/*.zh.restext"
- when: always
script:
- if ! grep "\.zh$" <(git diff --name-only HEAD~1); then exit; fi
pipeline was created and passed in next meege request, but when i tried to test it by creating "empty_file.zh.json" and commiting it, pipeline did nothing and still pass mr
What should i do differently?
My question is quite simple, I'm trying to learn AzureDevOps. I have a pipeline. In this pipeline I have a task with a bash script. This task basically adds files to the archive. This archive format:
I want it to be MyPackage_09192022_MyDeploymentComment.zip
For this, I created a variable called DeploymentComment in the pipeline. When I start a queque from this pipeline, I fill in the DeploymentComment field. I added this bash script to the task as filepath, so it gives the address of the file on the machine. I also gave the $DeploymentComment variable to the arguments in this task.
My script is as follows
date="$(date +"%d%m%Y")"
zipName="MyPackage_"$date"_"$1
zip -r $zipname /home/admins/myDir/*
I am waiting for the content of the $DeploymentComment variable that I gave as an argument on the Pipeline to come to the part I specified as $1 in the script. In other words, when I start the queque, when I type my1stTry in the $DeploymentComment section, I expect the zip file created when I type my1stTry
I expect it to be MyPackage_09192022_my1stTry.zip but bash does not see this variable.
I can create a zip file as MyPackage_09192022_.zip.
What am I missing, can you help me?
This will achieve your requirements:
trigger:
- none
pool:
vmImage: ubuntu-latest
variables:
- name: DeploymentComment #Define the variable
value: MyDeploymentComment
steps:
- bash: |
xxx="$(DeploymentComment)" #Use the pipeline variable
date="$(date +"%d%m%Y")"
zipName="$(System.DefaultWorkingDirectory)/MyPackage_"$date"_"$xxx
echo $zipName
zip -r $zipName.zip ./*
ls
Successfully get the value on my side:
My concourse task is something like this:
name: test
plan:
- get: my-repo
- task: my-task
config:
inputs:
- name: my-repo
run:
path: sh
args: [my-repo/examples/run-this.sh]
And the shell script tries to fetch a file in so manner:
CONFIG_FILE=./$name.cfg
When I run the task, concourse throws this error
my-repo/examples/run-this.sh: line xx: can't open name.cfg: no such file
The location of the run-this.sh and name.cfg file are the same. Any pointers will be appreciated!
Even though the two files share the same directory, the dot in ./name.cfg uses current working directory as a reference point - so it's the directory from which the script is called, not the directory in which the script is stored.
The best option to get this to work seems to be to add some intelligence to run-this.sh to locate the name.cfg file based on its own relative location, like this:
#!/bin/bash
SCRIPT_DIR="$(dirname $0)"
CONFIG_FILE="${SCRIPT_DIR}/name.cfg"
(...)
I am new to devops and I am having the following problem:
I have cloudbuild file which configured to generate deployment for namespace named after tag.
For example: for tag v1.0.2/host/dev it should generate new deployment at "dev" namespace.
Here is part of cloudbuild.yaml code:
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args:
- '-c'
- |
export APP_VERSION
export NAMESPACE
export CLUSTER_NAME
IFS=/ read -r APP_VERSION CLUSTER_NAME NAMESPACE <<< "$TAG_NAME"
... here is my problem ...
export ENVIRONMENT
export X
export XX
IFS=/ read -r X XX ENVIRONMENT <<< "$TAG_NAME"
...
As you can see - variable NAMESPACE is set with last part of tag (like "dev")
Later in code i need to set one more variable - ENVIRONMENT - with same value ("dev"). I did it by copy the way it done before and use some X and XX unused variables.
How can this be done in more accurate way?
I tried:
export ENVIRONMENT=NAMESPACE
Or:
export ENVIRONMENT=${NAMESPACE}
Any ideas?
This should do it:
export ENVIRONMENT=${NAMESPACE}
Basically, you are doing an
export NAMESPACE
before the said assignment and in bash NAMESPACE should be available as a variable ${NAMESPACE}
My program has a log file that it outputs when it completes. I'd like for that output file to be named by the date when it was run.
I've tried setting a date variable, as well as adding it in line. This post says it works in line, but I'm having no luck with that method.
My current gitlab-ci.yml
variables:
MyProjectEXE: My\Project\myproject.exe
MSBuild: C:\Path\to\my\MSBuild.exe
Solution: C:\Path\to\my\project.sln
stages:
- build
- test
build:
stage: build
script: '"%MSBuild%" "%Solution%"'
test:
stage: test
script:
- '"%MSBuild%" "%Solution%"'
- '%MyProjectEXE%" --results C:\path\to\my\results\log-$(date + \"%Y%m%d-%H%M%S\").csv
The second script under test just outputs a file named
C:\path\to\my\results\log-$(date
I've tried creating a variable that holds the date at the top, and putting it there, and I've tried just about every form of quotes around that log file name.
this could solve your problem https://gitlab.com/gitlab-org/gitlab/-/issues/22901
use CI_JOB_STARTED
'%MyProjectEXE%" --results C:\path\to\my\results\log-${CI_JOB_STARTED}.csv
or at the current time in a before statement