如何在Deployer中记录部署? [英] How to log deployments in Deployer?

查看:68
本文介绍了如何在Deployer中记录部署?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用部署程序并享受它.

我还没有弄清楚该怎么做的一件事是编写我的部署的日志文件.

One thing I haven't figured out how to do yet though is write a log file of my deployments.

我正在尝试将提交哈希值和日期附加到revisions.txt:

I'm attempting to append the commit hash and date to revisions.txt:

task('log_the_deployment', function () {//https://stackoverflow.com/a/4546755/470749
    $selectedStage = Deployer::get()->getInput()->getArgument('stage'); //https://github.com/deployphp/deployer/blob/6180366acff3ca5b2ec511a84e671321c02e7af1/recipe/config/hosts.php#L15
    runLocally('set -e'); //https://deployer.org/docs/api.html#runlocally
    runLocally('commit_short_hash=$(git rev-parse --short HEAD)');
    runLocally('commit=$(git log -1 --pretty="%H%n%ci")');
    runLocally('commit_hash=$(echo "$commit" | head -1)');
    runLocally('commit_date=$(echo "$commit" | head -2 | tail -1)');
    runLocally('branch_name=$(git symbolic-ref -q HEAD)');
    runLocally('branch_name=${branch_name##refs/heads/}');
    runLocally('branch_name=${branch_name:-HEAD}');
    runLocally('echo -e "$commit_date ' . $selectedStage . ' $commit_short_hash branch=\'$branch\' $commit_hash" >> releases.txt');//TODO: prepend instead https://stackoverflow.com/questions/10587615/unix-command-to-prepend-text-to-a-file
});

结果应类似于:2020-01-09 22:07:00 -0500 staging 146f012d branch='master' 146f012d28d866105aa12605cec6f374d45aec75

不幸的是,我的任务当前仅将此内容写入文件:-e staging branch=''

Unfortunately my task currently writes only this to the file: -e staging branch=''

我对Deployer,runLocally,git或Unix有什么误解?

What am I misunderstanding about Deployer, runLocally, git, or Unix?

如果有更好的方法实现我的目标,我很乐意走完全不同的道路.

And if there is a much better approach to achieve my goal, I'm happy to go down a totally different path.

推荐答案

将它们组合为一行似乎可以使其正常工作:

Combining them into one line seemed to enable it to work:

task('log_the_deployment', function () {
    $selectedStage = Deployer::get()->getInput()->getArgument('stage');
    runLocally('set -e && deployed_moment=$(date +\'%Y-%m-%d %H:%M:%S UTC\') && commit_short_hash=$(git rev-parse --short HEAD) && commit=$(git log -1 --pretty="%H%n%ci") && commit_hash_and_date=$(echo $commit | head -1) && commit_date=$(git show -s --format=%ci) && symbRefHead=$(git symbolic-ref -q HEAD) && echo "Deployed $deployed_moment to ' . $selectedStage . ' from branch \"$symbRefHead\"; Commit: $commit_hash_and_date $commit_short_hash" >> " >> releases.txt');
    upload('releases.txt', '{{release_path}}/releases.txt');
});

然后我创建了一条这样的路线:

And then I created a route like this:

Route::get('release', function () {
    $publicDir = getcwd();
    $deployments = file($publicDir . "/../releases.txt", FILE_IGNORE_NEW_LINES);
    return response()->json(['folder' => $publicDir, 'deployments' => array_reverse($deployments)], 200, [], JSON_PRETTY_PRINT);
});

这些帖子有所帮助:

  • Why can't Deployer runLocally run this variable string replacement command?
  • https://stackoverflow.com/a/1401490/470749
  • https://stackoverflow.com/a/13210909/470749
  • https://stackoverflow.com/a/3815007/470749

这篇关于如何在Deployer中记录部署?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆