I have a list of email ids in a Jenkins environment variable(emailsdl) and trying to use this for recipients: in Jenkins Pipeline Mailer as below:
mail (to: 'Mailer', recipients: '${env.emailsdl}',
subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) is waiting for input",
body: "Please go to ${env.BUILD_URL}.")
With the above code I am not receiving email and receiving an error:
Email not sent. No recipients of any kind specified ('to', 'cc', 'bcc').
But when I replace ${env.emailsdl} with real email(xyxyx#foo.com) then it does trigger an email. I even tried env['emailsdl'] and it didn't work.
Is there a way I can pass environment variable for recipients in this case?
In groovy if you use single quoted string it will not be interpolated, which means that in the string '${env.emailsdl}' the variable env.emailsdl will not be replaced. You need to use double quoted string: "${env.emailsdl}"
#Gergely: Your suggestion helped me resolving my issue. The problem is that I have a local environment variable which was assigned with a value from other global environment variable: globalVar = xyxyx#foo.com and emailsdl=${globalVar} is in job's local properties. Now I am calling this emailsdl in pipeline script. This has been resolved by:
env.((env.emailsdl).replaceAll("\$", ""))
Related
I'm a newbie to both Azure DevOps and Terraform but, I'm trying to deploy a pipeline using a YAML file.
I have tried to run a terraform plan using a YAML file and passing variables (from AZ DevOps) but, I got the following error:
2021-11-24T18:39:46.4604561Z Error: "name" may only contain alphanumeric characters, dash, underscores, parentheses and periods
2021-11-24T18:39:46.4604832Z
2021-11-24T18:39:46.4605940Z on modules/aks/main.tf line 2, in resource "azurerm_resource_group" "aks-resource-group":
2021-11-24T18:39:46.4606436Z 2: name = var.resource_group_name
2021-11-24T18:39:46.4606609Z
2021-11-24T18:39:46.4606722Z
2021-11-24T18:39:46.4606818Z
2021-11-24T18:39:46.4607525Z Error: Error: Subnet: (Name "#{vnet_subnet_name}#" / Virtual Network Name "#{vnet_name}#" / Resource Group "RG-XX-XXXX-XXXXX-001") was not found
2021-11-24T18:39:46.4608006Z
2021-11-24T18:39:46.4608580Z on modules/aks/main.tf line 16, in data "azurerm_subnet" "subnet-project":
2021-11-24T18:39:46.4609335Z 16: data "azurerm_subnet" "subnet-project" {
The 'name' has the following format at the Variable group in the Azure DevOps UI:
RG-XX-XXXX-XXXXX-001
This is the snippet of where I included the replace token at the YAML file:
displayName: 'Replace Secrets'
inputs:
targetFiles: |
variables.tfvars
encoding: 'utf-8'
actionOnMissing: fail
tokenPattern: #{MyVar}#
And this is a sample of the variables I have in a variable group:
variable-group-sample
Also, I replace the terraform.tfvars file with something like this:
resource_group_name = "#{resource_group_name}#"
I have checked the name inserted at the UI several times but I feel the error is pointing to something else I cannot see.
Have anyone experienced something related to this error?
Thank you in advance!
tokenPattern: #{MyVar}#
It is looking for the pattern #{MyVar}# to replace. Not "something contained between #{ and }#, but the actual value #{MyVar}#. I'm guessing it's expecting a regular expression, but I'm not familiar with that task.
So the end result is that your #{token values}# aren't getting replaced.
Assuming you're using https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens, you probably want to specify tokenPrefix: #{ and tokenSuffix: }# instead of using tokenPattern.
Now, having said that...
There is no reason for you to be using token replacement on a tfvars file. You should create different tfvars files for each environment, then pass in a tfvars file via the -var-file argument to Terraform. Secrets can be passed in on the command line via -var 'foo=bar'
Storing variables that represent application or deployment configuration in Azure DevOps (or GitHub, or any other CI system) is a big, big anti-pattern, because it's tightly coupling your deployment process to a particular platform. If you're sourcing all of your variables from Azure DevOps, you can't easily test locally or migrate to a different CI/CD provider like GitHub Actions in the future.
For values that shouldn't be in source control, such a secrets, you should use a secret provider like Azure KeyVault and integrate it with your application (or, in this case, use a data resource in Terraform to pull the necessary secrets automatically at deployment time).
I have the following credentials.yml file :
test: 123
test2: ((test))
When I upload a pipeline, feeding it with the credentials file , whenever test2 variable is used it is not interpolated and I'm getting a "undefined vars : test" error in Concourse.
Is it even possible to refer to another variable in the very same yaml or do you have to always refer to variables in a configured credentials manager (e.g. Vault) ?
Solved it using anchors and aliases . Sadly keys containing dots or hyphens do not work at all.
e.g. :
test: &test 123
test2: *test
I have my release pipeline Variables tab set like:
I would like to access my123 variable in task's display name by concatenating initialVariable's result.
Outputs
I have tried so far referencing only initialVariable and it returned proper value in Job's display name.
But when I try to create my123 value by using initialVariable(=123), I am not getting proper value (was hoping that $(initialVariable) would convert to 123 and $(my123) would get proper "finalValue").
Azure DevOps: Getting variable value by concatenating other variables'value as task input
This is a known issue. Because the value of nested variables (like $(my$(initialVariable)) are not yet supported in the build/release pipelines.
Check my other thread for some details.
The workaround is add a Run Inline Powershell task to set the variable based on the input pipeline variables, just like Josh answered.
For you case, I test it by following Powershell scripts:
if ($(initialVariable)-eq "123")
{
Write-Host "##vso[task.setvariable variable=my123]finalvalue"
}
else
{
Write-Host "##vso[task.setvariable variable=my123]otherValue"
}
Then we could get the variable my123 based on the value of variable initialVariable in following task, I add command line task to display the value:
In the result, the value in the command line task is correct finalvalue. But the display name is still $(my123):
Important:
That is also the question in your comment. This behavior is expected. That because the variable in the display name is just to get the predefined value. It's static acquisition, not dynamic. The variable my123 is assigned when running powershell. The static variable my123 in the display name does not go in to the environment where the powershell code is running.
So, the variable my123 in the title could not get the value in the task powershell. But other task could use it very well.
Hope this answer clean your puzzle.
It's ugly, but...
Like I mentioned in my comment, I don't think you're going to get this to work in the UI by default.
Luckily you can use PowerShell to hack this together if your REALLY need the ability to address a variable name based on the value of another variable.
All the variables (secrets are handled a little differently) in your build or release pipeline definition are made available to your powershell script FILE (not inline) via environment variables (ie. $env:initialVariable).
Suppose your situation is thus:
selector = selectable1 //this is the value that can change
selectable1 = theFirstSelection
selectable2 = theSecondSelection
selectable3 = theThirdSelection
In this case (assuming I understand your request) you want to be able to change the value of the selector and force tasks to access the appropriate selectable variable.
So...
Define a new variable in your pipeline.
selector = selectable1 //this is the value that can change
selected = "" //this is the variable you use in your tasks
selectable1 = theFirstSelection
selectable2 = theSecondSelection
selectable3 = theThirdSelection
Write a VariableSelection.ps1 script. This powershell script will be what you need to run to assign the value of $(selected) before it gets used.
# VariableSelection.ps1
Write-Host "select variable: $env:selector"
$selectedValue = (gci env:"$env:selector").value
Write-Host "##vso[task.setvariable variable=selected]$selectedValue"
Note: it is my observation that if you write this script inline, it will not work b/c the environment variable functionality is different for scripts run from a file.
Given the value of $(selector) is selectable2, when the script is run, then the value of the $(selected) will be theSecondSelection.
Example in a Pipeline
Powershell
YAML
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
pool:
name: Hosted VS2017
variables:
- name: "selector"
value: "var1"
- name: "selected"
value: ""
- name: "var1"
value: "var1_value"
- name: "var2"
value: "var2_value"
steps:
- task: PowerShell#2
inputs:
filePath: '$(build.sourcesdirectory)/varSelector.ps1'
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Write-Host "env:selected: $env:selected"
Write-Host "selected: $(selected)"
Results
I have a Source DacPac and a Target DacPac. With powershell I would like to compare both and generate a upgrade script. This per se is simple for all cases with DacServices as long as there is a valid Connection String. However, for my CD pipeline, I do not know the eventual connection string nor do I wish to know. And all I want is for the script to generate the SQL.
I tried this out with sqlpackage.exe by sending args as follows
$args = #(
"/a:Script",
"/sf:$PathToDacpacSource",
"/tf:$PathToDacpacTarget",
"/op:$outputFile",
"/tdn:$TargetDatabaseName"
)
and this works fine. so far I had managed by adding just a dependency to DacServices via [Microsoft.SqlServer.DacFx.x64], but this inability to generate scripts without a connection string has forced me to add the dependency on SQLPackage.exe via [Microsoft.Data.Tools.Msbuild] too
The constructors for DacServices all require a ConnectionString
Has anyone else encountered a similar situation and any solutions?
or Do you exclusively use SQLPackage at the deployment side to overcome this issue?
There is a static version of GenerateDeployScript that does not need a connection string as you don't call the constructor for a static method, it isn't very clear in the msdn documentation but the "S" which looks like it is floating means it is static :)
https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dac.dacservices.generatedeployscript(v=sql.120).aspx#M:Microsoft.SqlServer.Dac.DacServices.GenerateDeployScript%28Microsoft.SqlServer.Dac.DacPackage,Microsoft.SqlServer.Dac.DacPackage,System.String,Microsoft.SqlServer.Dac.DacDeployOptions%29
` public static string GenerateDeployScript( DacPackage sourcePackage, DacPackage targetPackage, string targetDatabaseName, DacDeployOptions options = null )
I am trying to set new environment variables in InvokeProcess in tfs2010, I tried creating a variable ENV_VAR of type IDictionary() and then in InvokeProcess then I tried adding new variable as ENV_VAR.Add("New","Variable") but it shows me an error.
Thanks
/G
For someone with similar issue , I managed to resolve it , Way to define EnvironmentVariable in InvokeProcess activity
New Dictionary(Of String, String) From {{"Key1", "Value1"}, {"key2", "value2"}}