How can I use environmental variables in the .releaserc.json? - configuration

I want to do something like this,
["#semantic-release/exec", {
"prepareCmd": "echo hello ${HOME};"
}],
And then run this CI job with an environmental variable? Is there a method of making this work in semantic-release?

Use .releaserc.js, or release.config.js
You can rename the releaserc.json to release.config.js or .releaserc.js`, and then it's just a CommonJS module,
const greets = "HELLO ";
module.exports = {
"foo": `${greets} ${process.title}`
}
That will run as expected.
Internally semantic-release uses cosmiconfig

Related

Add namespace to PSR-4-Section in composer.json file from command line interface

I'm looking for a way adding namespaces to the autoload PSR-4-Section in the composer.json file of my Laravel project, from the command line interface.
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "modules/",
/* add more here */
}
}
}
I was hoping there is a command like composer require, but I've not found a working command for this opportunity.
Did anyone come up with a proper solution for this?
Even a script for bash, powershell, php, ... would be appreciated. Being able to run it from the command line is the main thing.
If someone needs this,
I came up with this approach, which is working for me.
public function handle($key, $namespace, $output = 'composer.json')
{
$file = 'composer.json';
$data = json_decode(file_get_contents($file), true);
$data["autoload"]["psr-4"][] = array($key => $namespace);
file_put_contents($output, json_encode($data, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
}

How to bootstrap a JSON configuration file for an Angular 2 module without using a http.get approach

I have one json file at root:
config.json
{ "base_url": "http://localhost:3000" }
and in my service class, I want to use it in this way:
private productsUrl = config.base_url + 'products';
I've found a ton of posts with either solutions that require a http.get request to load that one file to get that one variable or outdated solutions for angular.js (angular 1)
I cant believe there isnt an easier way to include this file that we already have in place without having to make an additional request to the server.
In my opinion, I would have expected that at least the bootstrapping function would be able to provide this kind of functionality, something like:
platformBrowserDynamic().bootstrapModule(AppModule, { config: config.json });
btw, this works, but its not the ideal solution:
export class Config {
static base_url: string = "http://localhost:3004/";
}
and the use it where you need it:
private productsUrl = Config.base_url + 'products';
Its not ideal, because I will have to create the class (or replace properties) in a build script. (exactly what I was thinking to do with the config.json file).
I still prefer the config.json file approach, since it would not be intrusive with the TypeScript compiler. Any ideas how to do are welcome and really appreciated!
This link explains how to use System.js to load json files in an angular app.
Special thanks to #eotoole that pointed me in the right direction.
If the link above is not clear enough, just add a map into the System.js conf. like this:
map: { 'plugin-json': 'https://unpkg.com/systemjs-plugin-json' }*
*(using external package)
or
map: { 'plugin-json': 'plugin-json/json.js' }**
**if you download the plugin from:
official system.js plugin
now I can use:
const config = require('./config.json');
anywere in my app.
and since it is official from the "systemjs" - guys, I feel comfortable using it to load app settings like base_url or other endpoints.
Now I need to figure out how to encapsulate this logic for testing purposes. Maybe requiring the file in its own class and replacing the values for the specific test case.
Are you using webpack? If you are, and you can just do
const config = require('./config.json');
#Injectable()
export class MyService {
private config:any = config;
....
}
in your webpack config you will need the json-loader
...
module: {
...
loaders: [
...
{
test: /\.json$/,
loaders: ["json-loader"]
},
...
]
}
...

--node-args in PM2 while using json config mode

I have a question, that's how to pass "--node-args" arguments in PM2 while using json config mode, like this:
pm2 start --node-args="--debug=5858" myPm2Config.json
well, I know I can write the arguments into myPm2Config.json file, but I dont want to do this, because I want to make two startup command as "debug" and "production" mode for launch application, such as "pm2_run" and "pm2_debug", and "pm2_debug" command with --node-args argument and "pm2_run" not, and I dont want to make two "myPm2Config.json" files, because that means if something needs changed, I will need to change two json config files, so, is there any easy way to do it? thanks guys!
I have found the solution! that's use js config instead of json config.
first, I create a pm2.config.js file. (mark: file name must be end with .config.js)
//[pm2.config.js]
let config = {
apps : [{
name : "node_shells",
script : "./bin/www",
log_date_format : "YYYY-MM-DD HH:mm:SS",
log_file : "logs/pm2.log",
error_file : "logs/pm2-err.log",
out_file : "logs/pm2-out.log",
pid_file : "logs/pm2.pid",
watch : true,
ignore_watch : ["logs/*", "node_modules/*", "uploads/*"]
}]
}
let debug_mode = false;
for(let arg of process.argv) {
if(arg == '-debug') {
debug_mode = true;
break;
}
}
if(debug_mode) {
console.log('== launching in debug mode ==');
config.apps[0].node_args = "--debug=5858";
}
else {
console.log('== launching in production mode ==');
config.apps[0].node_args = " "; //*require! or it will always uses latest debug options
}
module.exports = config;
then, create two launch files: "pm2_run" and "pm2_debug".
#[pm2_run]
pm2 start pm2.config.js
#[pm2_debug]
pm2 start pm2.config.js -- -debug
now, it's easy to switch debug mode or production mode!

Getting an Environment Variable in Terraform configuration?

I have two environment variables. One is TF_VAR_UN and another is TF_VAR_PW. Then I have a terraform file that looks like this.
resource "google_container_cluster" "primary" {
name = "marcellus-wallace"
zone = "us-central1-a"
initial_node_count = 3
master_auth {
username = ${env.TF_VAR_UN}
password = ${env.TF_VAR_PW}
}
node_config {
oauth_scopes = [
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring"
]
}
}
The two values I'd like to replace with the environment variables TF_VAR_UN and TF_VAR_PW are the values username and password. I tried what is shown above, with no success, and I've toyed around with a few other things but always get syntax issues.
I would try something more like this, which seems closer to the documentation.
variable "UN" {
type = string
}
variable "PW" {
type = string
}
resource "google_container_cluster" "primary" {
name = "marcellus-wallace"
zone = "us-central1-a"
initial_node_count = 3
master_auth {
username = var.UN
password = var.PW
}
node_config {
oauth_scopes = [
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring"
]
}
}
With the CLI command being the below.
TF_VAR_UN=foo TF_VAR_PW=bar terraform apply
The use of interpolation syntax throws warning with terraform v0.12.18. Now you don't need to use the interpolation syntax. You can just reference it as var.hello.
Caution :
One important thing to understand from a language standpoint is that, you cannot declare variables using environment variables. You can only assign values for declared variables in the script using environment varibles. For example, let's say you have the following .tf script
variable "hello" {
type=string
}
Now if the environment has a variable TF_VAR_hello="foobar", during runtime the variable hello will have the value "foobar". If you assign the variable without the declaration of the variable there will not be any effect.
You can do the following to get this working.
Declare the variable in terraform configuration that you want to use as environment Variable.
variable "db_password" { type= string }
In the resource section where you want to use this variable change it as
"db_password":"${var.db_password}"
Export the environment variable.
export TF_VAR_db_password="##password##"
terraform plan or terraform apply
Use a null_resource to execute a terminal command (read an environment variable), redirect output to a file, then read the file content:
resource "null_resource" "read_environment_var_value_via_cli" {
triggers = { always_run = "${timestamp()}" }
provisioner "local-exec" {
command = "echo $TF_VAR_UN > TF_VAR_UN.txt" # add gitignore
}
}
data "local_file" "temp_file" {
depends_on = [ null_resource.read_environment_var_value_via_cli]
filename = "${path.module}/TF_VAR_UN.txt"
}
# use value as desired
resource "google_container_cluster" "primary" {
master_auth {
username = data.local_file.temp_file.content # value of $TF_VAR_UN
..
}
}
Most of the providers use:
DefaultFunc: schema.EnvDefaultFunc("
https://github.com/terraform-providers/terraform-provider-infoblox/blob/master/infoblox/provider.go
https://github.com/terraform-providers/terraform-provider-openstack/blob/master/openstack/provider.go
...
Alternatively, you can replace the variables in the file itself using the envsubst utility in bash:
$ envsubst < main.tf > main.tf
Or using an intermediate file with variables and the final config on the output:
$ envsubst < main.txt > main.tf
! Variables for envsubst must be declared using export:
$ export MYVAR=1729
The variables in the source file must be of the form: $VARIABLE or ${VARIABLE}.
in order to use a variable it needs to be wrapped with ""
for example:
username = "${var.UN}"

Passing karma configuration object from gulpfile

I have a karma.conf.js file that exports a function that takes a config object and applies a bunch of configurations to that object.
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
...
If I start karma from the command line like this: karma start, it runs correctly. Clearly the karma start function is inserting the required config object when it calls the function exported by karma.conf.js.
I am trying to start it with a gulp task that looks like this:
gulp.task('test', function (done) {
var karma = require('karma').server;
var karmaConf = require('./karma.conf.js')();
karma.start(karmaConf, done);
});
This gives me an error because the config parameter is missing.
Two questions:
How can I get the karma config object to include as a parameter, and
Is there a better way to do this?
try this:
gulp.task('test', function(done) {
var Server = require('karma').Server;
new Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done).start();
});
I am aware this question is a little old, but I found it when I was about to post my own q/a to something similar. The main difference being that I'm not working with gulp, but am just using Karma's public API directly. I'm still stuck using Karma v0.12, but it doesn't look like the spec has changed in this regard. It still requires an ordinary object, and my config file exports a function, just like in the OP's situation.
The main problem with the sample in the question is it tries calling the config function without providing any arguments. That is probably what is throwing the error. In particular, the config function expects a single input config, and calls config.set(actualConfigObject). What I did was write a function of my own that provides a minimally suitable object.
All that is needed is to ensure that what is passed in to the config function has a set function that in some way captures its first argument for later use. I actually ended up wrapping all that in a function that returns the argument for convenience:
function extractConfig(configFunction) {
var last;
var shell = {
set: function (input) { last = input; }
};
configFunction(shell);
return last;
}
Then I can call this with my required config file:
var config = extractConfig(require('./local-karma.conf.js'));
Which got my tests running. I have noticed that something is slightly off, since I override the logging level in my config, but the API seems to be using config.LOG_DEBUG regardless. But that's the only problem I've had so far. Though this unanswered question seems to also be doing something similar, but with less successful results.