This question already has answers here:
Multiline string in Azure Pipelines
(3 answers)
Closed 2 years ago.
Is it possible to use multiline in YAML in Azure Pipelines tasks? For instance for the ARM deployment task, there is an overrideParameters property. It would be nice if this could be split accross several lines instead of putting everything in one line:
- task: AzureResourceManagerTemplateDeployment#3
displayName: 'ARM deploy MyFunctionAPP'
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: ${{ parameters.serviceConnection }}
subscriptionId: ${{ parameters.subscriptionId }}
action: 'Create Or Update Resource Group'
resourceGroupName: ${{ parameters.resourceGroupName }}
location: 'West Europe'
templateLocation: 'Linked artifact'
csmFile: '$(ARMtemplatesPath)\MyFunctionAPP\template.json'
csmParametersFile: '$(ARMtemplatesPath)\MyFunctionAPP\parameters.json'
deploymentMode: 'Incremental'
overrideParameters: '-environment_name ${{ parameters.environmentName }} -vnetAddressPrefix ${{ parameters.vnetAddressPrefix }} -subnet1Prefix ${{ parameters.subnet1Prefix }} -APIkey ${{ parameters.APIkey }} -queueName ${{ parameters.queueNameMyQueue }} -SendGridAPIkey ${{ parameters.SendGridAPIkey }} -StorageConnectionAppSetting ${{ parameters.StorageConnectionAppSetting }}'
Is it somehow possible to split the overrideParameters values on multiple lines?
This has worked for me:
overrideParameters: >-
-environment_name "${{ parameters.environmentName }}"
-vnetAddressPrefix "${{ parameters.vnetAddressPrefix }}"
-subnet1Prefix "${{ parameters.subnet1Prefix }}"
-APIkey "${{ parameters.APIkey }}"
-queueName "${{ parameters.queueNameMyQueue }}"
-SendGridAPIkey "${{ parameters.SendGridAPIkey }}"
-StorageConnectionAppSetting "${{ parameters.StorageConnectionAppSetting }}"
So using >- on a single line and then using double-quotes to surround settings values works.
Is it somehow possible to split the overrideParameters values on
multiple lines?
According to the document Expressions, we could use a pipe character (|) for multiline strings:
- task: AzureResourceManagerTemplateDeployment#3
displayName: 'ARM deploy MyFunctionAPP'
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: ${{ parameters.serviceConnection }}
subscriptionId: ${{ parameters.subscriptionId }}
action: 'Create Or Update Resource Group'
resourceGroupName: ${{ parameters.resourceGroupName }}
location: 'West Europe'
templateLocation: 'Linked artifact'
csmFile: '$(ARMtemplatesPath)\MyFunctionAPP\template.json'
csmParametersFile: '$(ARMtemplatesPath)\MyFunctionAPP\parameters.json'
deploymentMode: 'Incremental'
overrideParameters: |
-environment_name ${{ parameters.environmentName }}
-vnetAddressPrefix ${{ parameters.vnetAddressPrefix }}
-subnet1Prefix ${{ parameters.subnet1Prefix }}
-APIkey ${{ parameters.APIkey }}
-queueName ${{ parameters.queueNameMyQueue }}
-SendGridAPIkey ${{ parameters.SendGridAPIkey }}
-StorageConnectionAppSetting ${{ parameters.StorageConnectionAppSetting }}
You could check this thread for some more details.
Related
I want to check that all necessary secrets exist and fail the build if some of them are missing.
In my script I have this step
- name: Check if secrets exist
env:
secret_key1: ${{ secrets.MY_SECRET_1 }}
secret_key2: ${{ secrets.MY_SECRET_2 }}
secret_key3: ${{ secrets.MY_SECRET_3 }}
if: ${{ env.secret_key1 == '' }} || ${{ env.secret_key2 == '' }} || ${{ env.secret_key3 == '' }}
run: exit 1
but this always exists with status code 1, even if all secrets are present.
I have checked that if I use only one secret it works correctly, e.g.
- name: Check if secret exists
env:
secret_key: ${{ secrets.MY_SECRET }}
if: ${{ env.secret_key == '' }}
run: exit 1
Am I using wrong syntax or is the problem somewhere else?
Your condition should look like this:
- name: Check if secrets exist
env:
secret_key1: ${{ secrets.MY_SECRET_1 }}
secret_key2: ${{ secrets.MY_SECRET_2 }}
secret_key3: ${{ secrets.MY_SECRET_3 }}
if: ${{ (env.secret_key1 == '') || (env.secret_key2 == '') || (env.secret_key3 == '') }}
run: exit 1
Also, you can omit the expression syntax (${{ }}) because GitHub automatically evaluates the if conditional as an expression:
- name: Check if secrets exist
env:
secret_key1: ${{ secrets.MY_SECRET_1 }}
secret_key2: ${{ secrets.MY_SECRET_2 }}
secret_key3: ${{ secrets.MY_SECRET_3 }}
if: env.secret_key1 == '' || env.secret_key2 == '' || env.secret_key3 == ''
run: exit 1
Screenshot: click
For more information, see Expressions.
is there any way to loop inside one object type Parameters again in Azuredevops
I am planning to automate tag create/update to resources using Azuredevops pipeline and I decided to use Azure CLI command for the same(not sure if this is the right choice)
So I created a template (template.yaml) file as below.
parameters:
- name: myEnvironments
type: object
- name: tagList
type: object
stages:
- ${{ each environment in parameters.myEnvironments }}:
- stage: Create_Tag_${{ environment }}
displayName: 'Create Tag in ${{ environment }}'
pool:
name: my-spoke
jobs:
- ${{ each tag in parameters.tagList }}:
- ${{ if eq(tag.todeploy, 'yes') }}:
- job: Create_Tag_For_${{ tag.resourcename }_${{ environment }}}
displayName: 'Tag the reource ${{ tag.resourcename }'
condition: eq('${{ tag.todeploy }}', 'yes')
workspace:
clean: all
pool:
name: myspoke
steps:
- task: AzureCLI#2
displayName: "Tag the resource"
inputs:
azureSubscription: ${{ variables.subscription }}
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: az tag update --resource-id ${{ tag.resourceid }} --operation replace --tags key1=value1 key3=value3
and my pipeline input as below
stages:
- template: template.yaml
parameters:
myEnvironments:
- development
################################################################################################
# Tag List #
################################################################################################
tagList:
- resourcename: myaksservice
todeploy: yes
tagname1: tagvalue of 1
tagname2: tagvalue of 2
.
.
.
.
tagn : tagvalue of n
- resourcename: myappservice
todeploy: yes
tagname1: tagvalue of 1
tagname2: tagvalue of 2
.
.
.
.
tagn : tagvalue of n
- resourcename: mystorageaccount
todeploy: yes
tagname1: tagvalue of 1
tagname2: tagvalue of 2
.
.
.
.
tagn : tagvalue of n
But I was able to loop through the envlist , and the taglist elelments, but not able to loop through the tag values for each resources to crate them at a shot.
trigger:
- none
pool:
vmImage: ubuntu-latest
parameters:
- name: myEnvironments
type: object
default:
- 111
- 222
- 333
- name: tagList
type: object
default:
- resourcename: myaksservice
todeploy: yes
tagname1_1: tagvalue of 1
tagname2_1: tagvalue of 2
- resourcename: myappservice
todeploy: yes
tagname1_2: tagvalue of 1
tagname2_2: tagvalue of 2
- resourcename: mystorageaccount
todeploy: yes
tagname1_3: tagvalue of 1
tagname2_3: tagvalue of 2
stages:
- ${{ each environment in parameters.myEnvironments }}:
- stage:
displayName: 'Create Tag in ${{ environment }}'
pool:
vmImage: ubuntu-latest
jobs:
- ${{ each tag in parameters.tagList }}:
- ${{ each tagcontent in tag }}:
- ${{ if and(ne(tagcontent.Key, 'resourcename'),ne(tagcontent.Key, 'todeploy')) }}:
- job:
displayName: 'Tag the reource ${{ tag.resourcename }}'
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host "Hello World"
Write-Host ${{tagcontent.Key}}
For the first stage, the pipeline will foreach the tagname in taglist and output:
tagname1_1
tagname2_1
tagname1_2
tagname2_2
tagname1_3
tagname2_3
So the key is 'object.Key' and 'object.Value', use them to get other contents in yaml object.
I'm trying to add env variables and then use them.
name: Extract branch/server/ssh/project names into ENV variable
shell: bash
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: "true"
SERVER_IP_DEV: ${{ secrets.SERVER_IP_DEV }}
SERVER_IP_MASTER: ${{ secrets.SERVER_IP_MASTER }}
EC2_SSH_KEY_DEV: ${{ secrets.EC2_SSH_KEY_DEV }}
EC2_SSH_KEY_MASTER: ${{ secrets.EC2_SSH_KEY_MASTER }}
PROJECT_NAME: ${{ secrets.PROJECT_NAME }}
run: |
branch=$(sed 's|/|_|g' <<< ${GITHUB_REF#refs/heads/})
server="SERVER_IP_${branch_name^^}"
ssh_key="EC2_SSH_KEY_${branch_name^^}"
project="PROJECT_NAME"
echo "::set-env name=BRANCH_NAME::branch"
echo "::set-env name=SERVER_IP::server"
echo "::set-env name=SSH_KEY::ssh_key"
echo "::set-env name=PROJECT_NAME::project"
and then use them somethin like this
name: Install SSH Key
uses: shimataro/ssh-key-action#v2
with:
key: ${SSH_KEY}
known_hosts: "just-a-placeholder-so-we-dont-get-errors"
but it does not work. what am doing wrong?
When translating existing Azure DevOps YAML pipelines to GitHub Actions YAML, I noticed some of my Azure pipelines were just templates calling other YAML files.
trigger:
- master
resources:
repositories:
- repository: templates
type: git
name: 'Cloud Integration\PipelineTemplates'
name: $(Build.SourceBranchName)_$(Build.Reason)_$(rev:r)
variables:
- group: var-lc-integration-emailservice
- name: logicapp_workflows
value: false
- name: base_resources
value: false
- name: functionapp_resources
value: false
- name: functionapp
value: false
- name: ia_resources
value: false
- name: ia_configs
value: false
- name: apim_resources
value: true
stages:
- template: yml-templates\master.yml#templates
parameters:
logicapp_workflows: ${{ variables.logicapp_workflows }}
base_resources: ${{ variables.base_resources }}
functionapp_resources: ${{ variables.functionapp_resources }}
functionapp: ${{ variables.functionapp }}
ia_resources: ${{ variables.ia_resources }}
ia_configs: ${{ variables.ia_configs }}
apim_resources: ${{ variables.apim_resources }}
While writing a GitHub workflow for the above Azure pipeline, Can we have a "dummy job" or no job at all within a workflow to solve this issue?
IIUC yes, see reusing GitHub Actions workflows.
It allows you to call another workflow from your repository and provide inputs/secrets as necessary.
I read this document https://learn.microsoft.com/zh-cn/azure/devops/pipelines/process/expressions?view=azure-devops#conditional-insertion
but not like what demoed in the document, I need add three variables with same condition as below:
name: arm_temp
resources:
repositories:
- repository: self
type: git
variables:
- ${{ if in(lower(coalesce(variables['ENV'], variables['Build.SourceBranchName'])), 'release', 'prod') }}:
- newEnv: 'Prod'
- account: '$(ACCOUNT)'
- password: '$(PASSWORD)'
- ${{ if eq(lower(coalesce(variables['ENV'], variables['Build.SourceBranchName'])), 'qa') }}:
- newEnv: 'QA'
- account: '$(ACCOUNT)'
- password: '$(PASSWORD)'
- resGroupName: ${{ format('RESGROUP-{0}', variables['newEnv']) }}
ACCOUNT, PASSWORD and ENV are variables defined in azure build pipeline
but I always get error before run the build pipeline.
and error notification is about line under the if conditiona.
From your Yaml sample, it seems that the Yaml format has some issues.
You could refer to the following YAML Sample:
variables:
${{ if in(lower(coalesce(variables['ENV'], variables['Build.SourceBranchName'])), 'release', 'prod') }}:
newEnv: 'Prod'
account: $(myaccount)
password: $(mypassword)
${{ if eq(lower(coalesce(variables['ENV'], variables['Build.SourceBranchName'])), 'qa') }}:
newEnv: 'QA'
account: $(myaccount)
password: $(mypassword)
resGroupName: ${{ format('RESGROUP-{0}', variables['newEnv']) }}
pool:
vmimage: windows-latest
steps:
- script: |
echo $(newEnv)
echo $(account)
echo $(password)
Variable:
Result:
Note: You need to change the variable name $(ACCOUNT) $(PASSWORD). They cannot have the same name as the variable defined in yaml($(account), $(password)). Or the variable couldn't pass successfully.