1

I am writing a task for capistrano 3 and I need to get the current commit sha1. How can I read that ? Is there a variable for that ?

I have seen fetch(:sha1) in some files but this isn't working for me.

I am deploying into a docker container, and I need to tag the image with the current sha1 (and ideally, skip the deployment if there is already an image corresponding to the current sha1)

Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164

2 Answers2

2

Capistrano creates a file in the deployed folder containing the git revision. In looking at the task which creates that file, we can see how it obtains the revision: https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake#L224

So, it is obtaining the revision from fetch(:current_revision).

In the git specific tasks, we can see where it is set: https://github.com/capistrano/capistrano/blob/master/lib/capistrano/scm/tasks/git.rake#L62

As a side note, Capistrano is probably not the best tool for what you are trying to do. Capistrano is useful for repeated deployments to the same server. Docker essentially is building a deployable container already containing the code. See the answers here for more detail: https://stackoverflow.com/a/39459945/3042016

Community
  • 1
  • 1
will_in_wi
  • 2,623
  • 1
  • 16
  • 21
  • Thanks. I had indeed seen those answers, however I believe my flow is a little different from theirs. I am first building a "base image" locally without the app, outside capistrano. Then I am deploying to a container started from this image with cap. Further deployments are also made to the same container to speed up, ie. repeated deployments to the same container. Every new deployment I am also committing a new image which I can push and deploy to my ECS cluster. Sounds like a fair use of Capistrano ? – Cyril Duchon-Doris Dec 07 '16 at 01:52
  • Huh. I've never heard of that workflow. I'm not a Docker expert/user, so I can't comment on whether it is the "right" way to do it or not. – will_in_wi Dec 07 '16 at 01:57
  • Does `fetch(:current_revision)` answer your question, though? – will_in_wi Dec 07 '16 at 01:58
  • No, I am calling it before any actual deploy task, and it would seem its value is empty. – Cyril Duchon-Doris Dec 07 '16 at 02:22
  • Ah. Then that isn't really possible within Capistrano. Deploy tasks are what fetch the git repo and get the revision to be deployed. – will_in_wi Dec 07 '16 at 02:23
  • Well I was thinking I could just invoke the `git:set_current_revision` task at the very beginning of my deploy.rb file, but for some reason it would seem `invoke 'git:set_current_revision' is not available ?` – Cyril Duchon-Doris Dec 07 '16 at 02:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129954/discussion-between-will-in-wi-and-cyril-duchon-doris). – will_in_wi Dec 07 '16 at 02:27
0

Capistrano 3 is using a plugin system for the version manager application used (git, svn, etc.)

The current_revision is delegated to the version manager plugin and I don't understand how to access it...

In the meantime a dirty solution would be

set :current_revision, (lambda do
  `git rev-list --max-count=1 #{fetch(:branch)}`
end)

But I'm waiting for a good solution that would instead, manage to invoke the right task from the SCM plugin

Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164