2
steps:
- script: |
    # BuildConfig = production
  env:
    TEST_ONE: 123

- script: |
    echo $TEST_ONE
  displayName: 'Running npm run'

I'd like to have $TEST_ONE in the second script and I also need that TEST_ONE to be in environment variables for the second script. When I echo it in the second script, iT Prints empty instead of 123. Any clue?

Nika Kurashvili
  • 6,006
  • 8
  • 57
  • 123
  • Does this answer your question? [Is there a way of extract output of bash script in Azure Pipeline](https://stackoverflow.com/questions/60300578/is-there-a-way-of-extract-output-of-bash-script-in-azure-pipeline) – Ivan Ignatiev Feb 25 '20 at 12:20

2 Answers2

2

I would not use scripts to manage your variables, otherwise anytime you need to change them, you need to be changing the scripts. And in case you use Version Control to manage changes to your pipelines, it would require commits, etc. Why don't you leverage the release variables and set scopes for your variables (so variables for master, variables for staging env, variables for all environments, etc.). And the best part is, you can manage these variables from the Azure DevOps UI. Please have a look to this article.

Edit
Since you do not want to store these variables in your environment files together with the source of your node app, then you can manage those variables on the pipeline by defining them on the variables section, and then you can send those variables as arguments to your npm script - see more here.

Hugo Barona
  • 1,303
  • 9
  • 21
  • The thing is why would i put staging variables in environment if the branch trigger is master? if i use UI, and create variables for staging/master separatelly, they will all be put in environments. makes sense? – Nika Kurashvili Feb 25 '20 at 11:29
  • Well, i do not know your use case to use different variables based on the branch. Usually you use different variables according to the release's environment. Can you tell me the use case to use specific variables for branch master and other variables to other branches? – Hugo Barona Feb 25 '20 at 11:31
  • I use pipelines for front-end project and webpack. So I need to have those variables set in envrionment at build-time. So that webpack can put it into my project. I have a trigger for 2 branches(master,staging). When it's master, i want to have production variables put in environment and npm run production will take those variables (while building)and put it in my project. When it's staging, staging variables will be put . – Nika Kurashvili Feb 25 '20 at 11:34
  • Well, which kind of variables are we talking about? Application-based variables? If that's the case, then you should save those variables on your app's config files and use transformations for the different environments? – Hugo Barona Feb 25 '20 at 11:45
  • I don't want to have those variables put in src code such as prod.env and stage.env . I don't want other people to see it in src code. – Nika Kurashvili Feb 25 '20 at 11:46
  • Fair enough. So in that case, we are talking about potentially secrets. What i would do is to use build variables, and store those values in a Key Vault, to be secure and you can control the access to those values, and in case you want to differentiate the variables per branch, etc.. you can create variable groups, and then build logic on your scripts to use the groups according to the branch. – Hugo Barona Feb 25 '20 at 11:50
  • A good guide here explaining how you can use variable groups - https://adamtheautomator.com/azure-devops-variables-complete-guide/ – Hugo Barona Feb 25 '20 at 11:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208491/discussion-between-nika-kurashvili-and-hugo-barona). – Nika Kurashvili Feb 25 '20 at 11:52
1

You can set a new variable in your powershell and then pass it to Azure Devops, so that it can be used in one of the next tasks. Here is an example

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • The thing is, if I use variables GUI to store my variables, will all of them be put into environments? I have 2 servers(master, staging) and i don't want to put all the variables in envs. I want to put staging variables in env when branch trigger is staging and same for master. any idea? – Nika Kurashvili Feb 25 '20 at 11:13