In TeamCity 2017.2.2 I am using parameters like %host% and when deploying TeamCity replaces those parameters by what I specified on the parameters section - at least when I paste my Powershell script right into the "Script source" box (setting Script: "Source code").
When I use the setting Script: "File" instead and pick the same .ps1 file from my repository, then TeamCity will run the script as before but leaves parameters like %host% untouched.
How do I use parameters with scripts from my repo?
The file is not altered from your vcs with a token replacement from: %host% to your value when you use the File input.
You should specify parameters for your script, and inside the "Script Arguments" section on TeamCity, you can specify the parameters like this:
-Host: %host% -SomeCustomValue: %extraArgs%
This will cause the script to be executed with the parameters transformed with proper values.
Related
Is it possible to change Xcode BuildSettings environment variable on a build phase in a script?
The whole idea is the following:
I want to publish an open source project with unit tests.
My tests require sensitive data like "login" and "password", so I can't hardcode them.
I want to pass login through process environment to be able to do something like this:
let login = ProcessInfo().environment["login"]!
I've found the way how to pass a variable from a build settings environment to a tests process: select "Tests" scheme, go to environment variables and add a new value with name "login" and value "$(login)".
If I set value for "login" right in the build settings tab – it will be accessible from the ProcessInfo just like I expected.
But what I want is to set "login" from a project's root folder script file "SetupCreds.sh" which is in .gitignore, so it's safe to keep login and password in it.
So the idea is:
Add Build Phase script
This script runs "SetupCreds.sh"
"SetupCreds.sh" consist of export lines like export login="MyLogin
If I add echo ${login} in a Build Phase script it tells me that the "login" really has value "MyLogin".
But this export does not affect Build Settings environment, only the Build Phase script environment.
How to do exports that override Build Settings environment variables?
Or maybe there is another way to do what I want?
I've copied the answer #Vladex mentioned in the comments here so it's more easily readable.
Add the following script phase right after the "Copy Resources" script.
$file then
copy_target="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.xctest/Tests.cfg"
cp -r $file $copy_target echo "Tests.cfg file copied to
${copy_target}" else echo "warning: No Tests.cfg file found. Some
tests will be ignored." fi
Put "Tests.cfg" to gitignore.
Create "Tests.cfg" file and fill it with sensitive or
config-specific values.
In tests use Bundle.for(type(of:
self).self) to get "Tests.cfg" url.
Given a Command line build step, with the following custom script:
msdeploy -verb:sync -source:package='%teamcity.build.projectid%\%WebProjectName%.csproj.zip'
how can i see what the variables ended up being replaced with?
I don't see any "verbose logging" option that holds the final output for debugging the build server anywhere
There's Parameters tab (accessible like http://teamcity/viewLog.html?buildId=BUILD_ID&tab=buildParameters where BUILD_ID should be replaced with id of the given build) in Build overview.
There're parameters values as they were resolved when build started.
This page is also accessible via popup displayed at the end of a build status text in build results row in Build Configuration Overview or Project view.
There is a file in [tempdirectory]\agentTmp called build.start.properties.gz open with something like winrar or 7zip and there is a file called build.start.properties.
In there you will be able to see the parameters used for the build like %teamcity.build.projectid%
In case anyone else comes here, the reson you are probably looking for that file is that your script isnt working, I would suggest you change
-source:package='%teamcity.build.projectid%\%WebProjectName%.csproj.zip'
"%WebProjectName%"
and include double quotes around your variables, if WebProjectName contains spaces your script might not work.
If your custom script is running in Unix-like environment you can use shell command set -x to print each command before execution
Full explanation here https://stackoverflow.com/a/2853811/2584381
If you are running on Windows, your command step has an implicit #echo off at the start.
To get full info in the logs, add
echo on
to the top of your command script.
I have a Jenkins project with one "Execute shell" that executes some bash commands and exports one variable to the env. variables:
#!/bin/bash
...
TARGET_FULLPATH="blablabla"
export TARGET_FULLPATH
In addition, I have configured to "Post-build Actions" with:
"Trigger parameterized build on other projects"
"Current build parameters"
"Predefined parameters" -> FULLPATH=$TARGET_FULLPATH
But, when I run this project the other project that will start as soon the first one is finished seems that it doesn't get the parameter FULLPATH at all!
At least in the "Execute shell" (of the second project) the bash script print nothing for echo $FULLPATH! Also at the "Environment Variables" of the second project the FULLPATH is not included!
Any clue what am I doing wrong?
One more thing, i can't use a properties file to store the parameters, since the two projects are running on different servers and there are restrictions on copying files between those servers!
Did you have a look to this solution?
Jenkins: How to use a variable from a pre-build shell in the Maven "Goals and options"
Using a shell pre-build step + the InjectEnv plugin, you should be able to solve your problem.
Update from June 22nd, I add some screen copies.
1/ Add a first "Execute shell" script to create the properties file and an "Inject environment variables" step to load the properties file:
2/ For the demo, I add a "post-build task" step to read the variable
3/ Here is the console output, it works :)
Within a TeamCity project running on a windows agent, I would like to read the contents of a file and then create a directory based on the file contents.
Performing this operation as a command line build step seems logical. I have tried creating a local variable "VERSION" as well as a custom teamcity parameter, but I can't get either to work. It does not seem that windows cmd variables are playing nicely with the TeamCity defined env and system variables. I am using the following custom script:
echo "Distributing"
set VERSION=< component_version.txt
echo %VERSION%
echo "Copying files to dir \path\to\dir\%VERSION%\"
mkdir \path\to\dir\%VERSION%\
Any suggestions on how I can achieve this?
You need to escape the variable with %% so it isn't treated as a TeamCity variable.
echo "Distributing"
set VERSION=< component_version.txt
echo %%VERSION%%
echo "Copying files to dir \path\to\dir\%%VERSION%%\"
mkdir \path\to\dir\%%VERSION%%\
Try creating a .bat file or Powershell file that accepts a parameter, and executes the steps you have outlined above.
Then switch your Build step to run an "executable with parameters", and pass the %VERSION% in as a parameter.
I have a simple batch file. I want to call/run/execute that file from Jenkins.
Is there are plugin in Jenkins for the same ?
How can execute the batch file from Jenkins ?
If there is any tutorial or documentation for the same.
No need to add a new plugin for that, in Jenkins , select your job name and go to the configure section.
There is a Build section , on that section there is a combo box, select Execute windows Batch command on that text box you can specify either the batch file details directly or specify the file path.
While building the job the batch file will automatically executed.
On Linux, go to your jenkins job, go to configuration, add build step "execute shell", then type the name of your script.
Please be sure that your file is executable (chmod 777 yourscript.sh) and in the right place (checkout via GIT or SVN, please check your job workspace, your current dir for execute is the job dir, base of job workspace) and to have a valid shebang (first line in your script file e.g. #!/bin/sh).
If you are using Jenkins Pipeline, you can simply use bat step.
You can generate Groovy by clicking "Pipeline Syntax".
Go to your pipeline and click "Configure"
Go to the bottom and under pipeline script click"Pipeline Syntax"
Choose Sample Step - "Windows Batch Script"
Enter your Batch Script and click "Generate Groovy"
For example you can echo the systemdrive variable:
bat 'echo %systemdrive%'