In the newer versions of the yarn package manager, almost all the commands have a --no-color option.
I'm running yarn under a continuous integration server (Jenkins) and the color escape chars pollute the output. I'd like to put something in the .yarnrc file to prevent the output of these escape chars. But I'd also to leave it on for when the developers run it on the terminal.
How to globally configure the --no-color option?
You can set the environment variable FORCE_COLOR to 0 to disable any colored output (this option comes from chalk which is used by yarn to output color).
pipeline {
agent {
docker {
image 'node:10'
}
}
environment {
FORCE_COLOR = '0'
}
stages {
stage('run yarn') {
steps {
sh 'yarn'
}
}
}
}
For yarn 1.22 worked:
export FORCE_COLOR=false
in scripts.
Note: FORCE_COLOR=0 has no effect
Related
I use package.json variables like this in NPM scripts:
// package.json
{
"version": "0.12.1",
"scripts": {
"get-version": "echo %npm_package_version%"
}
}
npm run get-version currently echoes %npm_package_version% instead of 0.12.1. In the past, the scripts worked without any problems. Suddenly only the variable name comes back. With multiple repositories. I run Windows 10 2004 and NodeJS v15.4.0.
Was there a change for NPM scripts in Node.js 15? Is it a bug or a feature?
UPDATE: Failure to expand environment variables on Windows appears to be a recent high-priority known bug in the npm CLI.
Because this is npm#7 specific, until a fix is released, you can downgrade to npm#6.
ORIGINAL ANSWER:
The easiest solution for the specific case in this question is to use node.
"get-version": "node -p process.env.npm_package_version"
This will work on every platform that Node.js supports.
If you need a more general solution and don't want to rewrite a bunch of scripts to use node, you can try cross-var as mentioned by #RobC in the comments.
As for the source of the problem, perhaps you are running under the Windows bash shell, in which case you can use this:
"get-version": "echo $npm_package_version"
That won't work for non-bash Windows environments though.
I found simple hack which is working perfect in my case,
Specifically in your use case
// package.json
{
"version": "0.12.1",
"scripts": {
"get-version": "node -e \"console.log(process.env.npm_package_version)\""
}
}
Usage
npm run get-version
However you want to pass arguments.
// package.json
{
"scripts": {
"get-argument": "node -e \"console.log('your argument:', process.argv[1] )\"",
}
}
Test example
npm run get-argument hello_world
Default values are a great way to handle undefined values. We use a predefined value instead. Inside our NPM script we can achieve that by using the following syntax;
{
"version": "0.12.1",
"scripts": {
"get-version": "echo ${npm_package_version:0.99}"
}
}
And of course, running npm from a bash prompt might help. I guess running from a Cmd/Powershell "could work" but I would be careful about that.
FYI - A related change in Version 7 if you are using the Package config variables:
The variable name changed from npm_package_config_customFooVar in V6 to npm_config_customFooVar in V7
Delineate these appropriate (as below) to the environment (Windows bash linux etc) being used. or Use lib like cross-var.
Package.json
{
"config": {
"customFooVar": "bar",
"env": "development"
},
"scripts": {
"get-var": "echo using env1 $npm_config_customFooVar OR env2 %npm_config_customFooVar%"
"build": "npm config set myAppName:env"
"postbuild": "cross-var ng build --configuration=$npm_config_env && cross-var node myOtherBuildSript.js $npm_config_env"
}
}
e.g. npm-cli call (note space after --) as this is passed to the script. Not to npm itself.
npm run build -- production
pass args from package.json to cli
echo %npm_package_version%
This solution allowed me to use the npm_package_version variable in both Windows and Unix:
Install run-script-os as a dev dependency. Then in your package.json the variable can be used:
"scripts": {
...
"postversion": "yarn postversion-wrapper",
"postversion-wrapper": "run-script-os",
"postversion-wrapper:windows": "echo %npm_package_version%",
"postversion-wrapper:nix": "echo $npm_package_version"
}
We are trying to access some of the experimental features of Docker with DOCKER_BUILDKIT. We see that this works fine on Mac and Linux, just not Windows. Any idea how to get this to work on Windows?
The ability to build Windows images is a known limitation of buildkit. You can subscribe to and add your vote to this issues on the roadmap if you are interested in the feature:
https://github.com/microsoft/Windows-Containers/issues/34
Otherwise, for building Linux images, buildkit should work the same on Docker for Windows as it does on other environments, with either the DOCKER_BUILDKIT=1 environment variable for the feature flag set in the daemon.json file (configurable from the Docker preferences UI):
{ "features": { "buildkit": true } }
Note that the environment variable overrides the feature flag on the engine. You can also use buildx which is another method to access buildkit. This has the same limitations as accessing buildkit directly (mainly you cannot build Windows images).
It works for me using Docker Desktop for Windows
Try to add the following to your daemon.json:
"features": { "buildkit": true }
I use docker within powershell and this worked for me:
# for docker build ...
$env:DOCKER_BUILDKIT = 1
# for docker-compose build ... (additional!)
$env:COMPOSE_DOCKER_CLI_BUILD = 1
This worked for me without any changes in the settings (as described in some other answers).
I know it is pretty late for answer but....
better late then never.
First of all, in docker desktop, go to settings >> docker engine and make sure you have everything set as shown below
{
"registry-mirrors": [],
"insecure-registries": [],
"debug": true,
"experimental": false,
"features": {
"buildkit": true
}
}
"features": {
"buildkit": true } is set to true by default i believe.
But mark that debug is set to true, while by default it is set to false.
So you will probably have to change it.
And second of all. The most obvious thing that is realy hard to find in documentation.
Unlike on ubuntu, you DO NOT actualy add DOCKER_BUILDKIT=1 at the start of your build instruction.
Personaly, it was extremaly confusing for me, because im so used to adding this phrase from Linux systems. But in Windows, if you enable the options as shown above, buildkit option will always trigger by default.
From version 0.33.1 onwards, aws-sam-cli supports colored output. I'm trying to run the sam deploy command from Jenkins pipeline and the output is not displaying in colored format. I've installed ANSIColor Jenkins plugin and wrapped the sam deploy command with ansiColor('xterm') {}. The command works as expected and the Cloudformation stack is getting created. The concern is the output is not in colored format.
node {
stage('Example') {
ansiColor('xterm') {
sh "sam deploy --parameter-overrides ${someparameter} --template-file ${templatefile} --stack-name ${stackname} --capabilities CAPABILITY_NAMED_IAM --no-fail-on-empty-changeset --no-execute-changeset"
}
}
}
In order to verify my Jenkins, I tried test-snippet in Jenkins and it displayed the colored output.
ansiColor('xterm') {
stage "\u001B[31mI'm Red\u001B[0m Now not"
}
So Jenkins is able to display ANSI color, but the aws-sam-cli output is not in colored format.
Any ideas or pointers would be helpful.
aws-sam-cli uses click library to format its output, including color handling.
The documentation for click explains why you're seeing what you're seeing:
Starting with Click 2.0, the echo() function gained extra
functionality to deal with ANSI colors and styles. [...]
Primarily this means that:
Clickâs echo() function will automatically strip ANSI color codes if the stream is not connected to a terminal.
This is a typical behavior of most programs, however some programs allow overriding this, usually with --color parameter.
In your case, I'd suggest asking for an enhancement on click's issue tracker.
Edit: There's already been one.
Problem
I'm using Jenkins pipeline and testing python with tox.
This combination explodes when tox creates a python virtualenv with a pip
whose shebang line exceeds the hard coded system imposed maximum of 127 characters.
To set a shorter workspace location,I want to add:
agent {
node {
label 'debian-slave'
customWorkspace "workspace/${env.GIT_BRANCH}"
}
}
but env.GIT_BRANCH is not yet defined. environment has not yet been calculated when the agent block runs.
How can I add the branch number or something similar to the workdir definition?
I don't want to lose the per-branch unique workspaces.
What Jenkins variables exist at the "agent" stage?
How can I display build timestamps for each line of a multi-branch pipeline project? Is it a supported feature? If yes, does it need to be enabled in the Jenkinsfile or is there a GUI option?
Adding options to the declarative pipeline
pipeline {
agent any
options { timestamps () }
// stages and rest of pipeline.
}
Credit goes to the comment above Jenkins Pipeline: Enable timestamps in build log console
For scripted pipeline just wrap your script in timestamps { }
Eg.
timestamps {
// do your job
}
Note: You must have the timestamper plugin installed: wiki.jenkins.io/display/JENKINS/Timestamper
I'm wondering why #roomsg comment on the accepted answer didn't become an answer.
I just noticed that (at least in our setup) you can configure this
globally: check the "Enabled for all Pipeline builds" in the
"Timestamper" section in Jenkins configuration
I think this is the best answering for Q. So,in case you have access as admin you can set it for all pipeline jobs through GUI