Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Git Revision (Hash) in Elixir #53

Closed
3 tasks done
nelsonic opened this issue Jul 28, 2017 · 7 comments
Closed
3 tasks done

Read Git Revision (Hash) in Elixir #53

nelsonic opened this issue Jul 28, 2017 · 7 comments
Labels
enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! technical A technical issue that requires understanding of the code, infrastructure or dependencies

Comments

@nelsonic
Copy link
Member

nelsonic commented Jul 28, 2017

I would like to display the git revision hash when visiting a url in an application
so that everyone on the project knows exactly which version of the app
is in "staging" or production.

e.g visiting: https://www.healthlocker.uk/_version
image
healthlocker/healthlocker@cc66a3c

Note: currently visiting this url shows an error page:
image

We made the Git Revision available in dwyl/aws-lambda-deploy/lib/utils.js#L80-L81
So it's a matter of running a similar command in Elixir and returning that when visiting the route.

Todo

  • run an Elixir command/script to get the git revision
  • create a route in basic Phoenix app that returns the git revision
  • determine if the git revision is available in an elixir app that has been deployed with edeliver (I don't see a reason why not, we need to be certain)
@nelsonic nelsonic added enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! technical A technical issue that requires understanding of the code, infrastructure or dependencies labels Jul 28, 2017
@nelsonic
Copy link
Member Author

nelsonic commented Jul 28, 2017

https://stackoverflow.com/questions/22594988/run-shell-commands-in-elixir

git rev-parse HEAD

so the Git Revision in Elixir is:

System.cmd("git", ["rev-parse", "HEAD"])

iex demo:
image

@nelsonic
Copy link
Member Author

nelsonic commented Jul 28, 2017

surprisingly easy even with my paltry Elixir/Phoenix skills ... 😮

add the following handler to: web/controllers/page_controller.ex:

  def git_revision_hash(conn, _params) do
    {rev, i} = System.cmd("git", ["rev-parse", "HEAD"])
    text conn, String.replace(rev, "\n", "")
  end

add route to /router.ex:

get "/_version", PageController, :git_revision_hash

Result:
image

Which is exactly what I wanted. ✅

@nelsonic
Copy link
Member Author

@nelsonic
Copy link
Member Author

sadly, as I feared, the release generated by distillery does not include the .git history!
so:
image

fatal: Not a git repository (or any of the parent directories): .git

So we're back to the drawing board on this ... 😢

@nelsonic nelsonic reopened this Jul 29, 2017
nelsonic added a commit to nelsonic/hello_world_edeliver that referenced this issue Jul 29, 2017
@nelsonic
Copy link
Member Author

nelsonic commented Jul 29, 2017

  def git_revision_hash(conn, _params) do
    IO.inspect(System.cmd("pwd", []))
    {ls, _} = IO.inspect(System.cmd("ls", ["-a"]))

    ls = String.split(ls, "\n")
    IO.inspect ls

    unless Enum.member?(ls, ".git") do
      File.cd("./builds")
      IO.inspect(System.cmd("pwd", []))
    end

    {rev, _} = System.cmd("git", ["rev-parse", "HEAD"])
    IO.puts(String.replace(rev, "\n", ""))
    text conn, String.replace(rev, "\n", "")
  end

still debugging it, will reduce debug lines once it's working.

@nelsonic
Copy link
Member Author

nelsonic commented Jul 29, 2017

Result!
image
7cb64a0be4bd7346ee4a4cc2bf770e401f9f76c9

This Git Hash corresponds to this commit: nelsonic/hello_world_edeliver@7cb64a0
image

So it's trivial to see exactly which version of the project is running in a given environment!

nelsonic added a commit to nelsonic/hello_world_edeliver that referenced this issue Jul 29, 2017
@nelsonic
Copy link
Member Author

Got it working in: dwyl/learn-devops#19 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! technical A technical issue that requires understanding of the code, infrastructure or dependencies
Projects
None yet
Development

No branches or pull requests

1 participant