Packer: git-hash function for naming of the resulting file

Created on 1 Dec 2014  路  3Comments  路  Source: hashicorp/packer

I currently customize my post_processor like this:

"output": "boxes/rhel-server-6.5-x64-{{.Provider}}-{{.BuildName}}_{{timestamp}}.box"

It would be nice to have {{git_hash}} instead of timestamp. If there's another way of doing this, other than a function call in packer, then I guess I'm not familiar enough on how to do it.

I see where this might be done: packer/config_template.go.

I thought most people would find this useful.

Ex: If we want to know the entire configuration of our puppet stack, we should also know which packer box each node was built on, and timestamp doesn't give us that. It's a good thing to have for auditing or even the nastiest of nasty disaster recovery strategies.

Thoughts? Alternatives?

_edit_:

There's also the use case of running packer against a dirty git tree, where the resulting hash wouldn't be accurate. You'd probably want the git hash to return 'dirty_tree' in that case..

waiting-reply

Most helpful comment

Hm. I think adding git-specific functions is tricky, because it opens the door for other SCMs which we may not have the same level of expertise on. Is there a reason you can't use a user-var for this like:

packer -var "git_sha=`git rev-parse HEAD`"

And then declare the variable:

{
  "variables": {
    "git_sha": "none"
  }
}

{{git_hash}} makes a few assumptions, the most important being - you have Git installed. I do not think all our users use Git, so finding the correct UX when git is not installed might be difficult (do we error, replace with an empty string, etc).

All 3 comments

Hm. I think adding git-specific functions is tricky, because it opens the door for other SCMs which we may not have the same level of expertise on. Is there a reason you can't use a user-var for this like:

packer -var "git_sha=`git rev-parse HEAD`"

And then declare the variable:

{
  "variables": {
    "git_sha": "none"
  }
}

{{git_hash}} makes a few assumptions, the most important being - you have Git installed. I do not think all our users use Git, so finding the correct UX when git is not installed might be difficult (do we error, replace with an empty string, etc).

Agreed with @sethvargo

Another tiny tip for people that are reading this: you might want to use the --short argument for rev-parse:

packer -var "git_sha=`git rev-parse --short HEAD`"

to get a value like 21feafa instead of 21feafa9a86a45b3008d133f0803d2ac2087c63f.

Was this page helpful?
0 / 5 - 0 ratings