首页 > 解决方案 > 如何在 ruby​​ / chef 中打印 git latest_tag 命令的值

问题描述

我正在尝试从 git repo 中获取最新的 git 标签,它是克隆到我的 EC2 Ubuntu 实例的。下面是我正在使用的厨师食谱,即 git.rb

execute 'test' do
cwd 'myapp-codecommit/myfiles'
 #command 'git clone https://'+node[:awscli][:GIT_USER]+':'+node[:awscli][:GIT_PASS]+'@git-codecommit.us-east-1.amazonaws.com/v1/repos/xxxxxxxx'
  command '$(git describe --abbrev=0 --tags)'
  puts "output is #{command}"

end

输出:EC2 日志

Use `bundle show [gemname]` to see where a bundled gem is installed.
output is $(git describe --abbrev=0 --tags)
[2018-07-13T12:04:28+00:00] INFO: Processing execute[test] action run 
Error executing action `run` on resource 'execute[test]'
Mixlib::ShellOut::ShellCommandFailed
Expected process to exit with [0], but received '127'
---- Begin output of $(git describe --abbrev=0 --tags) ----
STDOUT:
STDERR: sh: 1: master-1.0.177: not found
---- End output of $(git describe --abbrev=0 --tags) ----
Ran $(git describe --abbrev=0 --tags) returned 127
Resource Declaration:
# In /var/chef/runs/bb7cdd55-4fa8-4979-8485-dc5706d9db32/local-mode-cache/cache/cookbooks/deployfile/recipes/git.rb
3: execute 'test' do
4:     cwd 'myapp-codecommit/myfiles'
5:   #command 'git clone https://'+node[:awscli][:GIT_USER]+':'+node[:awscli][:GIT_PASS]+'@git-codecommit.us-east- 1.amazonaws.com/v1/repos/xxxxxxxx'
6:   command '$(git describe --abbrev=0 --tags)'
7:
8:   puts "output is #{command}"
9:
10: end

正如我们在输出日志中看到的那样,我能够在 (STDERR) 下获取最新标签,即 master-1.0.177 但我希望将最新标签,即 (master-1.0.177) 打印为

output is master-1.0.177

我在哪里

output is $(git describe --abbrev=0 --tags)

标签: rubychef-infrachef-recipeaws-opsworks

解决方案


Chef 资源没有输出值。您可能想要的是shell_out!辅助方法的一些用法,但您必须更具体地了解您最终要完成的工作。这是为了快速调试吗?

编辑:

你想要这样的东西:

# You can swap in some other resource like s3_file, the principle is the same.
remote_file "download the artifact" do
  source lazy {
    git_describe = shell_out!('git describe --abbrev=0 --tags', cwd: '/path/to/myapp-codecommit/myfiles').stdout.strip
    "https://something/path/to/myapp-#{git_describe}.jar"
  }
end

推荐阅读