2

I'm deploying an application with elastic beanstalk, which has its own deploy tool. This tool takes the latest commit, creates a zip from it, and deploys that to the cloud. To verify the deploy on each server, I'd like it to be able to report its own SHA once it has been deployed. There are actually a few valid approaches:

  • Add SHA to current commit, with a git hook.
  • Alter the EB deploy scripts to include a specific uncommitted file, which can be easily created in a deploy script or git hook.
  • Make elastic beanstalk current application version label available to the instance.
Peter Ehrlich
  • 6,969
  • 4
  • 49
  • 65

3 Answers3

10

I solved this with .gitattribtues export-subst. (http://git-scm.com/docs/gitattributes) This automatically adds the SHA to the repo when it is archived, which is what elastic-beanstalk does upon deploy.

My .gitattributes:

*.py diff=python
version.txt export-subst

My version.txt:

$Format:%H$

See https://stackoverflow.com/a/16365314/478354

Community
  • 1
  • 1
Peter Ehrlich
  • 6,969
  • 4
  • 49
  • 65
  • I'm trying this, and when I manually run `git archive` it works fine, but when I use `eb deploy`, the substitutions are not made on the EC2 instance. Any idea why this could be? I am using the single-container docker stack, in case that has anything to do with it. Do the AWS docs promise somewhere that `eb deploy` will always use `git archive`? – gmr Jun 22 '16 at 18:43
  • 3
    OK, I figured it out. If you have an `.ebignore` file, `eb deploy` doesn't use git. So you have to get rid of that file for your technique to work. – gmr Jun 23 '16 at 15:14
0

Add SHA to current commit, with a git hook.

This doesn't seem practival, as it would change the commit (and its SHA1)!

So generating the right file based on the commit, at deploy time is usually the best practice.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

You can create a post deploy hook, and access the SHA version in a bash script.

Here is an example that worked for me:

Create a file like .ebextensions/post_deploy_hook.config with this content:

commands:
  create_pre_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/pre"
    ignoreErrors: true
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/rollback_deploy_tracking.sh":
    content: |
      #!/bin/bash

      REVISION=`unzip -z /opt/elasticbeanstalk/deploy/appsource/source_bundle | tail -n 1`

      # put your own logic here...

      exit 0;

Now when deploying with eb deploy that script will run.

Lucas D'Avila
  • 437
  • 4
  • 7