Azure DevOps pipeline template - how to concatenate a parameter - templates

All afternoon I have been trying to get my head around concatenating a parameter in an ADO template. The parameter is a source path and in the template a next folder level needs to be added. I would like to achieve this with a "simple" concatenation.
The simplified template takes the parameter and uses it to form the inputPath for a PowerShell script, like this:
parameters:
sourcePath: ''
steps:
- task: PowerShell#2
inputs:
filePath: 'PSRepo/Scripts/MyPsScript.ps1'
arguments: '-inputPath ''$(sourcePath)/NextFolder''
I have tried various ways to achieve this concatenation:
'$(sourcePath)/NextFolder'
see above
'$(variables.sourcePath)/NextFolder'
I know sourcePath is not a variable, but tried based on the fact that using a parameter in a task condition it apparently only works when referencing through variables
'${{ parameters.sourcePath }}/NextFolder'
And some other variations, all to no avail.
I also tried to introduce a variables section in the template, but that is not possible.
I have searched the internet for examples/documentation, but no direct answers and other issues seemed to hint to some solution, but were not working.
I will surely be very pleased if someone could help me out.
Thanx in advance.

We can add the variables in our temp yaml file and pass the sourcePath to the variable, then we can use it. Here is my demo script:
Main.yaml
resources:
repositories:
- repository: templates
type: git
name: Tech-Talk/template
trigger: none
variables:
- name: Test
value: TestGroup
pool:
# vmImage: windows-latest
vmImage: ubuntu-20.04
extends:
template: temp.yaml#templates
parameters:
agent_pool_name: ''
db_resource_path: $(System.DefaultWorkingDirectory)
# variable_group: ${{variables.Test}}
temp.yaml
parameters:
- name: db_resource_path
default: ""
# - name: 'variable_group'
# type: string
# default: 'default_variable_group'
- name: agent_pool_name
default: ""
stages:
- stage:
jobs:
- job: READ
displayName: Reading Parameters
variables:
- name: sourcePath
value: ${{parameters.db_resource_path}}
# - group: ${{parameters.variable_group}}
steps:
- script: |
echo sourcePath: ${{variables.sourcePath}}
- powershell: echo "$(sourcePath)"
Here, I just use the workingDirectory to as the test path. You can use the variables also.
Attach my build result:

Thanx, Yujun. In meantime did get it working. Apparently there must have been some typo that did block the script from executing right as the se solution looks like one of the options mentioned above.
parameters:
sourcePath: ''
steps:
- task: PowerShell#2
inputs:
filePath: 'PSRepo/Scripts/MyPsScript.ps1'
arguments: '-inputPath ''$(sourcePath)/NextFolder''

Related

Copy Files to: $(Build.ArtifactStagingDirectory) -Input required: TargetFolder

I am new to YAML and build pipelines. I am receiving the following error, can anyone advice what's wrong with the target folder.
Unhandled: Input required: TargetFolder
[warning]Directory 'D:\a\1\a' is empty. Nothing will be added to build
artifact 'drop'.
Below is my YAML file:
# Build app using Azure Pipelines
pool:
vmImage: 'vs2017-win2016'
steps:
- script: echo hello world
- task: NodeTool#0
inputs:
versionSpec: '8.x'
- task: CopyFiles#1
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(build.sourcesdirectory)'
Contents:
\C:\VSCodeGit\CollMod.Web\Web.config\
TartgetFolder: '$(Build.ArtifactStagingDirectory)'
condition: succeededOrFailed()
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: drop'
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
condition: succeededOrFailed()
I think it's the contents field that looks to be invalid here.
The docs at https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/copy-files?view=vsts&tabs=yaml and further documentation on https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/file-matching-patterns?view=vsts which both give some great examples.
If you're unsure set the contents to **/* which will copy absolutely everything in the $(build.sourcesdirectory), but it will give you a feel for the shape of the directory structure so that you can change **/* into something more selective and scoped for the file(s) you want to copy.
The Source folder should be : Build.SourcesDirectory instead of '$(build.sourcesdirectory)'
This is from : https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables

How pass named arguments with parameters to aws batch?

Consider piece of AWS batch job definition:
MyJobDefinition:
Type: "AWS::Batch::JobDefinition"
Properties:
Type: container
Parameters: {}
JobDefinitionName: "my-job-name"
ContainerProperties:
Command:
- "java"
- "-jar"
- "my-application-SNAPSHOT.jar"
- "--param1"
- "Ref::param1"
- "--param2"
- "Ref::param2"
Which result to call:
java -jar my-application-SNAPSHOT.jar --param1 someValue1 --param2 someValue2
How do I change job definition to make it like this? (notice the = sign):
java -jar my-application-SNAPSHOT.jar --param1=someValue1 --param2=someValue2
Please note that Ref::param1 is not cloudformation template params, but aws batch job params.
As I understand, AWS batch parameters are substituted by looking for the Ref:: prefix. I could find only one thread where they tried to use a parameter in a larger string and it works.
Given that, the following should work
MyJobDefinition:
Type: "AWS::Batch::JobDefinition"
Properties:
Type: container
Parameters: {}
JobDefinitionName: "my-job-name"
ContainerProperties:
Command:
- "java"
- "-jar"
- "my-application-SNAPSHOT.jar"
- "--param1=Ref::param1"
- "--param2=Ref::param2"

Cloudbuild.yaml command with nested quotes

I am trying to run the following command found at http://blog.wrouesnel.com/articles/Totally%20static%20Go%20builds/:
CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' .
The two inner layers of quotes are tripping me up. How to deal with this in a cloudbuild.yaml file?
Escaping quotes don't seem to work:
steps:
- name: 'gcr.io/cloud-builders/go'
args: ['build', '-o', 'main', '-ldflags', "'-extldflags \"-static\"'", '.']
env:
- 'GOOS=linux'
Update:
There is no need for such quotes. See comment in Github here: https://github.com/GoogleCloudPlatform/cloud-builders/issues/146#issuecomment-337890587
===
Original Answer
Well, to quote ' within '-quoted strings, use '' as per YAML specification:
http://yaml.org/spec/current.html#id2534365
e.g. 'here''s to a toast!'
For the above args, it would be:
['build', '-o', 'main', '-ldflags', '''-extldflags "-static"''', '.']
Whether or not the command works within Cloud Builder is beyond the scope of this question.

Is it possible to set a top-level only/except in a .gitlab-ci.yml file?

I have three stages in my CI file, they all have only/except like this:
test:
only:
- tags
except:
- branches
script:
- npm run test
Seems redundant to have the only/except in three places. Is there a way to set this at the top level of the script config? Don't see anything like that in the docs.
You can use the map merging feature: https://docs.gitlab.com/ee/ci/yaml/#special-yaml-features
.job_template: &job_definition
only:
- tags
except:
- branches
test1:
<<: *job_definition
script:
- npm run test
test2:
<<: *job_definition
script:
- # ...

Unable to execute bash scripts from heat template

I am new to heat templates and I am trying to run bash scripts from the heat template. However, the instance is coming up in active state but the shell script was not executed at all. Any suggestions will be highly appreciated.
Hope this is helpful for you.
parameters: DBRootPassword:
type: string
label: Database Password
description: Root password for MySQL
hidden: true
resources: my_instance:
type: OS::Nova::Server
properties:
# general properties ...
user_data:
str_replace:
template: |
#!/bin/bash
echo "Hello world"
echo "Setting MySQL root password"
mysqladmin -u root password $db_rootpassword
# do more things ...
params:
$db_rootpassword: { get_param: DBRootPassword }