How do you properly use Azure DevOps functions in a variable template? - function

How can I use the Azure DevOps counter function in a variable template?
Up until now, I have been using the counter function to set a variable in an pipeline and the value has been set as expected - it started at 1 and has incremented every time I run the pipeline.
Variable template - /Variables/variables--code--build-and-deploy-function-app.yml
variables:
- name: major
value: '1'
- name: minor
value: '0'
- name: patch
value: $[counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1)]
- name: branch
${{ if eq( variables['Build.SourceBranchName'], 'master' ) }}:
value: ''
${{ if ne( variables['Build.SourceBranchName'], 'master' ) }}:
value: -${{ variables['Build.SourceBranchName'] }}
However, after moving the exact same variables in to a variable template, the value of counter is the literal value specified in the template.
Digging further in to the documentation for templates, I found some words on template expression functions, together with an example of how to use a functon -
You can use general functions in your templates. You can also use a few template expression functions.
Given that counter is listed on the page that the link above refers to, I assumed I would be able to use it. However, no matter what I've tried, I can't get it to work. Here are a few examples -
${{ counter('${{ format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']) }}', 1) }}
${{ counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1) }}
$[counter('${{ format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']) }}', 1)]
What am I doing wrong?
Update
My variable template is as above, an here is how I use it in the pipeline -
pr: none
trigger: none
variables:
- template: ../Variables/variables--code--build-and-deploy-function-app.yml
name: ${{ variables.major }}.${{ variables.minor }}.${{ variables.patch }}${{ variables.branch }}
The expanded pipeline obtained from logs after a run is as follows -
pr:
enabled: false
trigger:
enabled: false
variables:
- name: major
value: 1
- name: minor
value: 0
- name: patch
value: $[counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1)]
- name: branch
value: -CS-805
name: 1.0.$[counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1)]-CS-805
As can be seen from the extended pipeline, the patch variable isn't being evaluated, resulting in the name containing the literal value -

I inserted the same variables into the template and the patch variable works as expected. It seems that your counter is correct.
Here are my sample, you could refer to it:
Template Yaml: build.yml
variables:
- name: major
value: '1'
- name: minor
value: '0'
- name: patch
value: $[counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1)]
Azure-Pipelines.yml
name: $(patch)
trigger:
- none
variables:
- template: build.yml
pool:
vmImage: 'windows-latest'
steps:
- script: echo $(patch)
displayName: 'Run a one-line script'
In order to make the result more intuitive, I set patch variable to build name.
Here is the result:
Update:
Test with $(varname) and it could work as expected.
trigger:
- none
variables:
- template: build.yml
name: $(major).$(minor)-$(patch)$(branch)
Result:
The $(varname) means runtime before a task executes.

Related

Set an env variable using bash command that checks for existence of input variable

I've been reading about expressions.
I need to create an env var that all jobs in a workflow can reference that, in English, checks for the existence of ${{ inputs.db_schema }} and if it exists, use it, otherwise set it to 'prod'.
Tried (borrows from JWLs solution on this SO post):
env:
db_schema: ${{ ${{inputs.db_schema}} :-'prod }}
This returned an error when I tried to run:
The workflow is not valid. .github/workflows/main.yaml (Line: 17, Col: 14): Unexpected symbol: '${{inputs'. Located at position 1 within expression: ${{inputs.db_schema .github/workflows/update-sk4-caller.yaml (Line: 18, Col: 16): Unexpected symbol: '${{inputs'. Located at position 1 within expression: ${{inputs.update_date
How can I create an env variable that can be used by all jobs in a workflow where the value is either what exists in ${{inputs.db_schema}} else if that input doesn't exist then 'prod'?
[EDIT]
Adding a more complete example of what I"m trying to do. Here's a piece of my workflow:
name: MyWorkflow
on:
workflow_dispatch:
inputs:
db_schema:
required: true
type: string
default: US.DATA_SCIENCE
schedule:
- cron: '10 3 * * *' # daily at ten past 3am
env:
db_schema: ${{inputs.db_schema || 'US.DATA_SCIENCE'}}
jobs:
check-env-vars:
runs-on: ubuntu-latest
steps:
- name: check env vars
run: |
echo ${{ env.db_schema }}
update-clicks:
uses: ./.github/workflows/update-clicks.yaml
secrets: inherit
with:
db_schema: ${{ env.db_schema }}
I need to pass a value to db_schema within the with statement.
You're mixing access to GitHub Actions context and shell parameter expansion. You might be able to do something like
db_schema: ${{ inputs.db_schema || 'prod' }}
or if you access db_schema in a shell script, you could use
db_schema: ${{ inputs.db_schema }}
and then access via
${db_schema:-prod}

Schedule Trigger Github Action workflow with Input parameters

I want to run my Github workflow two ways:
Manually by user
Cron job
Now, everything was running fine until I added input parameters. After that, the cron job is running but not picking default value.
Here is my yaml:
name: WebDriverIO Automation
on:
workflow_dispatch:
inputs:
typeOfTesting:
type: choice
description: Select Type of Test
default: 'stage-test-local-All'
required: true
options:
- stage-test-local-All
- stage-test
- stage-test-local-Sanity
- prod-test
branches:
- workingBranch
- JSNew
schedule:
- cron: "*/5 * * * *"
inputs are available to workflows triggered by the workflow_dispatch event only (and to any workflows that are called by dispatched workflows).
You can use the || expression operator to set the default values for input parameters. For example:
on:
schedule:
- cron: '15 0,18 * * 0-5'
workflow_dispatch:
inputs:
logLevel:
required: true
type: string
jobs:
test:
uses: ./.github/workflows/run-job-with-params.yml
secrets: inherit
with:
springProfile: 'production'
logLevel: ${{ inputs.logLevel || 'DEBUG' }}
In the above example the logLevel parameter is set to DEBUG when the workflow is triggered by cron. When the job is triggered by a dispatch event it gets set to the input value.
Below code worked
name: WebDriverIO Automation
on:
workflow_dispatch:
inputs:
typeOfTesting:
type: choice
description: Select Type of Test
default: 'stage-test-local-All'
required: true
options:
- stage-test-local-All
- stage-test
- stage-test-local-Sanity
- prod-test
branches:
- workingBranch
schedule:
- cron: "*/5 * * * *"
...
..
..
- name: Test
run: |
if [ ${{ github.event.inputs.typeOfTesting }} != "" ]; then
npm run ${{ github.event.inputs.typeOfTesting }}
else
npm run stage-test-local-All
fi

How to check if an optional workflow_dispatch input is set or not

I have an input defined as
workflow_dispatch:
inputs:
level:
description: 'level to process'
type: number
required: false
I would like to run named actions in the step depending on if the value is set or not but I cannot figure how
This might be working:
if: ${{ github.event.inputs.level}}
But I cannot find the opposite version, this is invalid:
if: !${{ github.event.inputs.level}}
I've tested it in this workflow and the correct way to check if an input (or any variable) is empty or not in an IF conditional is by using the following syntax:
if: "${{ github.event.inputs.<INPUT_NAME> != '' }}"
Note: using != "" will break the workflow as the interpreter doesn't accept this symbol in expressions.
Therefore, your workflow would look like this:
on:
workflow_dispatch:
inputs:
level:
description: 'level to process'
type: number
required: false
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Print variable if not empty
if: "${{ github.event.inputs.level != '' }}"
run: echo Level value is ${{ github.event.inputs.level }}
- name: Print message if empty
if: "${{ github.event.inputs.level == '' }}"
run: echo Level value is empty
I've made two tests here if you want to check:
workflow run with empty input value
workflow run with not empty input value

Dropdown for GitHub Workflows input parameters

I want to create a dropdown list for my GitHub Action Input parameter. This should help in selecting a value from the dropdown just like how the option is there to select the branches.
When using workflow_dispatch, it's now possible to have choice, boolean and environment inputs instead of only just strings. choice is a dropdown, boolean is a checkbox and environment is like choice but will auto-populate with all environments configured in your repos settings.
Here's an example workflow using the new types:
name: CI
on:
workflow_dispatch:
inputs:
environment:
type: environment
description: Select the environment
boolean:
type: boolean
description: True or False
choice:
type: choice
description: Make a choice
options:
- foo
- bar
- baz
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: greet
run: |
echo "environment is ${{ github.event.inputs.environment }} / ${{ inputs.environment }}"
echo "boolean is ${{ github.event.inputs.boolean }}" / ${{ inputs.boolean }}
echo "choice is ${{ github.event.inputs.choice }}" / ${{ inputs.choice }}
Note that inputs can now be accessed directly using the inputs context, instead of using github.event.inputs.

variable from loop in when statement ansible

I am trying to use a variable in a when statement and ansible pops up a warning like this
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ item.name}}.changed
I use a loop first:
- include_tasks: anaconda_env.yml
with_items: "{{anaconda_templates}}"
and in the anaconda_env file.yml i have this:
- name: anaconda.templates
template:
owner: "{{item.owner|default(common_owner)}}"
group: "{{item.group|default(common_group)}}"
mode: "{{item.mode|default(common_mode)}}"
src: "{{item.template}}"
dest: "{{item.dest}}"
register: "{{item.name}}"
- name: anaconda.handler
command: echo '1'
notify: "{{item.name}}"
when: "{{ item.name}}.changed"
And in another situation I tried "{{ item.name}}.rc == 1" and I have the same issue. Any idea how can I avoid this Wanring message.
I found the issue here but no solution
https://github.com/ansible/ansible/issues/27225
My original answer didn't work, but I believe the one below will (or at least did with my limited mock data):
- set_fact:
current_template: "{{item}}"
- name: anaconda.handler
command: echo '1'
notify: "{{item.name}}"
when: current_template.changed is defined