Custom environment variable - environment-variables

Is it possible to set a custom environment variable, which will be accessible from any other plugin, the same way as $platform and $path works?
There is a package EnvironmentSettings by Daniele Niero, but it seems my task is simpler and therefore there is a probablity that there is no need to dive deep into its code.

In Sublime, any plugin can modify the global process environment through os.environ from the Python run time. All plugin code runs under the same process, so once one plugin sets an environment variable, any other plugin could access it. I would imagine that this is how the package that you linked to in your question modifies the environment.
A simple example of this in action can be found in Default/exec.py which you can open by using View Package File from the command palette. In the __init__ method of AsyncProcess() there is code that modifies the Sublime process environment if you pass the path argument in your sublime-build file.
A simple example that you can run from the Sublime console would be the following snippet. Once you execute that code, any plugin that you create can access os.environ["MY_VARIABLE"] to see the value.
import os
os.environ["MY_VARIBLE"]="Some Value"
With that said, in Sublime $platform is not an environment variable, it's a special variable that Sublime knows how to expand itself which is divorced from the system environment outlined above.
A complete list of such variables can be viewed by executing the following code from the Sublime console:
from pprint import pprint
pprint(window.extract_variables())
The list of variables you get and their content depends on application state (platform, whether there is currently a project open in the window, the current file, etc).
The names of the variables that this returns are hard coded in the Sublime core and can't be augmented, so if you wanted extra variables here you would need to communicate that to other plugins and they would have to be modified to know how to use them.
From the sounds of what you're trying to accomplish in the comments on your question, what you want might be a sublime-settings file that contains a setting that specifies the directory to use for file actions in your custom plugins. If they all load the settings file to get the path you can modify the location in the config and have it take effect immediately. Alternatively you could do something like a top level module variable in one plugin and import it into the others.

Related

TFS integration - how to set environment variables accessible from test code

I'm writing a TFS / VSTS integration with our server by using extension.
I want to present extra data after the build, specifically I want to show an IFRAME and navigate to our server, to a specific URL, determined in part by a dynamically generated unique string.
I have the function that generates the string, but I need to set it as an environment variable before the tests start to run. This is important because the tests need to create that string on the server.
I searched the documentation, examples and other places, but couldn't find a complete example that sets a dynamically generated environment variable and then runs tests.
How do I do it?
You can add/set an environment variable through Logging Commands (e.g. PS: Write-Host "##vso[task.setvariable variable=testvar;]testvalue"), then the following tasks can get this variable as general variable.
With Logging Commands, you also can add additional information in build summary. (##vso[task.uploadsummary]local file path)
You also can deploy a build result extension to display additional information. vsts-extension-samples

Is there a way to overwrite a value contained within a config.properties file via Jenkins?

Is there a way to overwrite a value contained within a config.properties file via Jenkins?
I have the following config.properties file contained within my automation framework:
browser=chrome
url=http//www.example.com
If the value of chrome get changed to firefox then all tests will now execute within firefox browser.
I can manually change this value by directly accessing the config.properties file but can the value get altered via jenkins?
I use the Pipeline Utility Steps plugin to read properties files, and it looks like it can write a few other types of files, but not properties files.
It seems to me that you want to make this change in this file so you can run some tests first in one browser, then in another. If this is the case, I think a better way to handle this is to try to get your tests to point to different files. This is a little cleaner, and allows things like parallel execution and when you find that another thing needs to change in the future, you won't be writing so many things to the file in a script, which gets a little error prone.
If you can't make your tests execute against a different properties file, you could have a copy of each file you need, and then copy them to them appropriate filename to execute your tests.
But maybe I made poor assumptions as to your setup here. ;)
Yes.
You can create a build parameter as $browser to accept the value say "firefox" and using sed inside "execute shell", replace the value in config.properties.
Once done, execute your scripts.
This is just overview as you have not posted details about your config.properties file, its location, if you are using Jenkins jobs or jenkinsfile/pipeline etc.

How to autoload environment variables specific to one file path?

I am working on developing a solution that simplifies hands-on debugging of failed Jenkins builds. This involves SSH-ing to the right Jenkins node and going directly on the WORKSPACE so you can interactively try different changes that could solve your problem.
While I solved the problem of starting a SSH session in the right directory there is one missing bit: your shell is missing the original environment variables defined by Jenkins, and these are critical for running any commands after that. So, not the first command of the build is a set > .envrc which saves all into this shell file.
My example refers to the direnv tool which is able to auto-load .envrc files. Due to security concerns this tool does not auto-load these files and gives a message direnv: error .envrc is blocked. Rundirenv allowto approve its content.
So my current solution is to manually run direnv allow after ending up in the right folder.
How can I automate this, so I would not have to type this? A prompting could be ok because it would involve only pressing one key instead of typing ~12.
Please note that I am not forced to use direnv itself, I am open to other solution.
As of v2.15.0, you can now use direnv's whitelist configuration to achieve what you described:
Specifying whitelist directives marks specific directory hierarchies
or specific directories as "trusted" -- direnv will evaluate any
matching .envrc files regardless of whether they have been
specifically allowed. This feature should be used with great care, as
anyone with the ability to write files to that directory (including
collaborators on VCS repositories) will be able to execute arbitrary
code on your computer.
For example, say that the directory hierarchy that contains the .envrcs you want to be evaluated without having to run direnv allow is located under /home/foo/bar.
Create the file /home/foo/.config/direnv/config.toml so that it contains the following:
[whitelist]
prefix = [ "/home/foo/bar" ]
Alternatively, if there are a fixed list of specific paths you want to whitelist, you can use exact rather than prefix:
[whitelist]
exact = [ "/home/foo/projectA", "/home/foo/projectB" ]

Is it possible to set environment variables in a .sublime-project file?

I'd like some environment variables to be set in Sublime Text when I open the project. This is for the most part just for some plugins that require them to be set.
I believe that there is a feature request for this, but I was still wondering if this isn't possible in some way or another.
http://sublimetext.userecho.com/topic/103881-sublime-project-should-also-be-able-to-set-environment-variable/
I'm asking this specifically in relation with the GoSublime plugin, for which I'd like to change the GOPATH between projects.
I see that you linked my request to sublime userecho.
Unfortunately nothing really happened over there, so I developed my own plugin to do that.
If you are still interested, please look at this:
https://bitbucket.org/daniele-niero/sublimeprojectenvironment
I have also made a pull request to Package Control to make the plugin available with it.

Jenkins global variables

I'm trying to use global variables within Jenkins on Windows to "automagically" retrieve the proper code base from our SCM system, but in each case that I've tried the variable substitution is not happening.
I've set up some global variables, with default values, within "Configure System" and have tried to access them with $VARIABLE, ${VARIABLE} and %VARIABLE% as part of the Branch field for the Surround SCM plugin with no success whatsoever.
I've also installed the Global Variable String Parameter plugin with the same success rate (0%). Using a literal value works just fine, but no type of variable substitution seems to work at all and I'm sure that someone has come upon this before and resolved it.
I've tried searching for something similar to this but nothing really approaches this usage of globals, instead it is normally discussed as a function within an external script, or parameter passed to a batch file, etc.
I've run "set" as the first step and can see that the variable is available, but the substitution is just not happening. If it means I will have to script something, then so be it, as I am trying to make this extremely flexible and as headache free as possible, but that isn't seeming to be the case in this case thus far.
My problem is eerily similar to this post: How are environment variables used in Jenkins with Windows Batch Command?, but again, I'm not looking to script this as it is a MUCH simpler solution to use the variable values directly.
from https://wiki.jenkins-ci.org/display/JENKINS/Surround+SCM+Plugin
Troubleshooting
Please contact Seapine support with questions about the integration or
to report bugs or feature requests.
Set your Jenkins project to be parameterized. Create a string parameter GIT_BRANCH that will be your branch variable (for example).
Under Source Control Management, use your branch variable in the form $GIT_BRANCH
That’s it. When you run your project, you will be prompted to enter a value for your GIT_BRANCH parameter.

Resources