tag:help.appveyor.com,2012-11-13:/discussions/problems/24567-save-environment-variables-after-build-and-set-them-again-when-deploying-that-specific-buildAppVeyor: Discussion 2019-10-14T21:01:25Ztag:help.appveyor.com,2012-11-13:Comment/474686032019-07-26T22:17:19Z2019-07-26T22:17:19ZSave environment variables after build and set them again when deploying that specific build<div><p>Sorry for the delay in getting back to you. We were running a few workaround experiments.<br>
Can you share some more details about your deployment approach. Do you deploy different builds from the same project or are different projects using the same deployment environment? Also, to what kind of server are you deploying?</p></div>Owen McDonnelltag:help.appveyor.com,2012-11-13:Comment/474686032019-07-26T22:58:40Z2019-07-29T17:30:14ZSave environment variables after build and set them again when deploying that specific build<div><p>We have multiple projects deploying to the same environment, which is a Windows 2012 server running IIS for internal testing. When deployment has failed due to problems on the server, or perhaps it succeeded and we have since made changes on the server and we want to try it again, we'd like an easy way to redo the deployment exactly as it was done the first time.</p>
<p>Basically saving all the environment variables that exist after a build and setting them again on deploy would solve the problem. Each specific build would have it's own set of saved environment variables. The deploy process could read these from a file or database and set them before starting the deployment.</p></div>jeremiahtag:help.appveyor.com,2012-11-13:Comment/474686032019-07-29T14:54:23Z2019-07-29T14:54:23ZSave environment variables after build and set them again when deploying that specific build<div><p>I see.<br>
What you could do is set something like the following in the <code>install:</code> stage of your build,</p>
<pre>
<code>- ps: >-
$env:var1 = 'val1'
$env:var2 = 'val2'
$vars = @{
"var1" = $env:var1
"var2" = $env:var2
}
$vars | ConvertTo-Json | Set-Content .\vars.json</code>
</pre>
<p>and then, make sure you specify that artifact.</p>
<pre>
<code>artifacts:
- path: vars.json</code>
</pre>
<p>And finally, include a <code>before-deploy.ps1</code> file in your zipped project artifact with something like the following...</p>
<pre>
<code>$projectName = $env:APPVEYOR_PROJECT_NAME
$appName = $env:APPLICATION_NAME
$vars = Get-Content "C:\appveyor\projects\$projectName\applications\$appName\vars.json" | ConvertFrom-Json
[Environment]::SetEnvironmentVariable("var1", "$vars.var1", "Machine")
[Environment]::SetEnvironmentVariable("var2", "$vars.var2", "Machine")</code>
</pre>
<p>Does it make sense?</p></div>Owen McDonnelltag:help.appveyor.com,2012-11-13:Comment/474686032019-07-29T17:54:20Z2019-07-29T17:54:21ZSave environment variables after build and set them again when deploying that specific build<div><p>That makes sense, but what about variables set in the AppVeyor Agent settings such as deploy_website and hostname? We set these dynamically with environment variables such as $(websitezip_hostname). I think the environment variables need to be set before the deployment agent starts running?</p></div>jeremiahtag:help.appveyor.com,2012-11-13:Comment/474686032019-07-29T21:36:27Z2019-07-29T21:36:27ZSave environment variables after build and set them again when deploying that specific build<div><p>Sure, if you need them to be set on the target server, you can put them in the file as well, otherwise they are used for deployment purposes only.</p></div>Owen McDonnelltag:help.appveyor.com,2012-11-13:Comment/474686032019-08-12T21:55:53Z2019-08-12T21:55:54ZSave environment variables after build and set them again when deploying that specific build<div><p>I think we are talking about different things. I want to save the AppVeyor agent deployment settings that were set at build time as seen above, which uses evaluations like $(websitezip_site_name) to set "site_name". Is there any way to save the appveyor agent settings that were set for that particular build and reuse them if a manual deploy is performed?</p></div>jeremiahtag:help.appveyor.com,2012-11-13:Comment/474686032019-08-14T18:20:57Z2019-08-14T18:20:57ZSave environment variables after build and set them again when deploying that specific build<div><p>I'm not sure if you specifically mean the web UI by manual deploy, but you could use the json file as a basis for scripting an <a href="https://www.appveyor.com/docs/api/environments-deployments/#start-deployment">api call</a> which passes along those variables.</p></div>Owen McDonnell