我有两个Perl脚本和GIT钩子script.In,在那里我正在验证GIT工作,flow.Here是调用堆栈的脚本。
预推->取消推送更改->依赖树
在撤消更改的perl脚本中有一个for循环,它将调用依赖树perl脚本。
预推
system("unpushed-changes");
my $errorMsg = $ENV{'GIT_FLOW_ERROR_MSG'}// '';
if($errorMsg eq "true"){
print "Error occured!";
}unpush-changes.pl
for my $i (0 .. $#uniqueEffectedProjectsList) {
my $errorMsg = $ENV{'GIT_FLOW_ERROR_MSG'}// '';
if($errorMsg ne "true"){
my $r=system("dependency-tree $uniqueEffectedProjectsList[$i]");
}else{
exit 1;
}
}dependency-tree.pl
if(system("mvn clean compile -DskipTests")==0){
print "successfully build";
return 1;
}else{
$ENV{'GIT_FLOW_ERROR_MSG'} = 'true';
print "Error occured";
return 0;
}在我的dependency-tree脚本中,如果出现错误,我已经设置了 ENV 变量,这将在unpush-changes script.But中的每个迭代中进行检查,它的ENV值为空值,而不是true。如果失败,我还尝试返回一些值,并尝试验证它,但它似乎也不是working.So,我的要求是,如何在所有scripts.Please之间共享一个全局变量,让我知道是否有更好的方法。
发布于 2017-06-23 14:00:42
通常,子进程从其父进程继承单独的环境副本,并且子进程所做的更改不会传播到父进程的环境。Env::Modify为这个问题提供了一个解决办法,实现perlfaq所讨论的“壳魔法”。
典型用法:
use Env::Modify 'system',':bash';
print $ENV{FOO}; # ""
system("export FOO=bar");
print $ENV{FOO}; # "bar"
...
print $ENV{GIT_FLOW_ERROR_MSG}; # ""
system("unpushed-changes");
print $ENV{GIT_FLOW_ERROR_MSG}; # "true"
...发布于 2017-06-24 14:35:12
正如@mob所提到的,有两种方法可以实现这一点:Env::Modify或者作为perl lib.So,我选择了lib而不是Env::Modify.because,我希望在每台机器上运行这个脚本,不管是否安装了Env::Modify包。
我编写了将/c/lib/My/Utils.pm.和dependency-tree功能捆绑在一起的Utils.pm,并将其保存在Utils.pm下。
Utils.pm
package My::Utils;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(build deploy);
sub build {
system("mvn clean compile -DskipTests")
//Do other things
}
sub deploy {
//Do things
}
1;然后,我在我的pre-push钩子中使用了以前创建的库。
预推
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw(dirname);
use Cwd qw(abs_path);
use lib dirname(dirname abs_path $0) . '/lib';
use My::Utils qw(build deploy); // or use lib '/c/lib';
build();
deploy();不再需要担心ENV变量.参考文献
https://stackoverflow.com/questions/44722935
复制相似问题