cURL and shell task
Asked Answered
P

2

6

I am using gulpfile.js in my project to download files and it is working fine.

var shell = require('gulp-shell');
gulp.task('folder_xyz', shell.task('curl --output xyz.zip --header "PRIVATE-TOKEN: theyuuin_io2_kj" "https://example.com/api/v4/download?job=xyz"'));

here everything is working fine and i am able to download the file. However i have set environment variable in GitLab called download_folder_xyz . Now i want to use this environment, want to replace Private token with new variable download_folder_xyz in the gulp.task() how can i do this ?

Photocopy answered 22/4, 2022 at 9:25 Comment(1)
Does this answer your question? How to pass environment variables from docker-compose into the NodeJS project?Shabbir
E
1

You can access any environment variable in nodejs via process.env object. Let's see an example. Assuming you want to access download_folder_xyz that is a environment variable

var shell = require('gulp-shell');
gulp.task('folder_xyz', shell.task(`curl --output xyz.zip --header "PRIVATE-TOKEN: ${process.env.download_folder_xyz}" "https://example.com/api/v4/download?job=xyz"`));
Elzaelzevir answered 22/4, 2022 at 10:28 Comment(3)
Ok i uploaded the answerElzaelzevir
the code is wrong. The code still uses PRIVATE-TOKEN: theyuuin_io2_kjPhotocopy
Sorry i miss understood your question.I edited my answerElzaelzevir
V
1

If download_folder_xyz has successfully been set as an environment variable, you can retrieve it from process.env. The env property of the process core module hosts all environment variables at the time the process has been started. See the docs for more information on this.

You can use the enviornment variable just as any other variable and interpolate in the task string. To replace the private token, this is the updated code:

const { download_folder_xyz } = process.env;

var shell = require('gulp-shell');
gulp.task('folder_xyz', shell.task(`curl --output xyz.zip --header "PRIVATE-TOKEN: ${download_folder_xyz}" "https://example.com/api/v4/download?job=xyz"`));
Vatic answered 24/4, 2022 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.