Git Commit Message in Environment Variable

hi,

I’ve looked on the docs and forum for this, but is there a way to get the git commit message in an environment variable, like you can get the $CIRCLE_SHA1? I want to use this in a deployment script to AWS Elastic Beanstalk with the aws elasticbeanstalk create-application-version command to give the new application version a meaningful description.

2 Likes

Try this:

- aws elasticbeanstalk create-application-version --description $GIT_COMMIT_DESC:
  environment:
      GIT_COMMIT_DESC: git log --format=%B -n 1 $CIRCLE_SHA1

I’m not at a pc right now so I can’t test it, but it should get you in the right ballpark :smiley:

https://circleci.com/docs/configuration#modifiers,
https://git-scm.com/docs/git-log

1 Like

Hey thanks for the quick response, I didn’t realise you could do that sort of thing.

But I did try this and I can’t get it to work, the command format is right. I chose to use --format=oneline. But I can’t get it to actually return a value.

When I have this:

machine:
  ...
  environment:
    GIT_COMMIT_DESC: git log --format=oneline -n 1 $CIRCLE_SHA1

It just seems to return “git”, i guess not executing it. I’ve tried also enclosing it in $() (which just makes it blank) and also using the UI for env vars but still no luck.

Bit of a newbie with all this so apologies for any stupidity.

Update: for completeness I should say I’m then passing this env var to a script like in this circlei documentation:

deployment:
  elasticbeanstalk:
    branch: master
    commands:
      - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
      - ./deploy.sh $CIRCLE_SHA1 $GIT_COMMIT_DESC

and then passing the var down in the script:

GIT_COMMIT_DESC=$2
...
aws elasticbeanstalk create-application-version \
    ...
    --description "$GIT_COMMIT_DESC" \
    ...
1 Like

Pretty sure Machine fires before git grabs your project, so that may be it. Testing my way now…

Yep, that’s likely it. The following code works great as a test task.

  • echo GIT_COMMIT_DESC: environment: GIT_COMMIT_DESC: (git log --format=oneline -n 1 $CIRCLE_SHA1)

(with correct tabs, of course)

1 Like

thanks - just updated my answer with some more details. also yeah i tried at first the environment under the command as you had it, but was getting syntax errors. thanks again!

1 Like

ok great, I’ll test that

1 Like

ok maybe the issue is the place i’m trying to do it in:

deployment:
  elasticbeanstalk:
    branch: master
    commands:
      - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
      - ./deploy.sh $CIRCLE_SHA1 $GIT_COMMIT_DESC:
        environment:
          GIT_COMMIT_DESC: $(git log --format=oneline -n 1 $CIRCLE_SHA1)

because i get this big red error :smile: :

‘deployment.elasticbeanstalkshould’ be a either a deploy section with commands, a Heroku deploy section or a CodeDeploy deploy section.

update: I guess since I’m calling a script I could/should just do it in that script

1 Like
deployment:
  production:
    branch: master
    commands:
       - docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
       - ./deploy.sh $CIRCLE_SHA1 $GIT_COMMIT_DESC:
          environment:
            GIT_COMMIT_DESC: $(git log --format=oneline -n 1 $CIRCLE_SHA1)

Give this a shot?

1 Like

yeah all the deployment itself to EB works and all the downstream stuff is fine without me trying to get the git message, so I’ll just play around with doing it at different points and post back here. thanks for all the help

2 Likes

cool will do, later on - have to stop on this at the moment

FYI - same error but with deployment.production:

‘deployment.productionshould’ be a either a deploy section with commands, a Heroku deploy section or a CodeDeploy deploy section.

have got it working from within the shell script itself (not surprisingly) so that’s good enough for me at the moment and i’ll see if any other thoughts come to mind and post back if so

1 Like

Maybe it’s too much trouble, but this worked for me:

echo "export COMMIT_MESSAGE=\"$(git log --format=oneline -n 1 $CIRCLE_SHA1)\"" >> ~/.bashrc

3 Likes

cheers, i may try that

1 Like

Would it be possible to get all the commit messages since the previous build?

The build page already shows all new commits included in the build right at the top. Would be great to access them as an env var or otherwise.

I don’t know how to get how many commits there have been since the previous build, but you can set how many commits to return in your arguments to git log by changing the ‘1’ to how many you want to see. For example, git log --format=oneline -n 5 $CIRCLE_SHA1 would print the last five commits (if your number is greater than the number of commits to a repo, it doesn’t break–it just prints as many as you have).
You can also do more with the format. For example, git log --format="%s" -n 5 $CIRCLE_SHA1 would print just the commit messages without the hash.
See https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History.

2 Likes

Thanks, you pointed me in the right direction. >This< is exactly what I’m after.

Is there any plan for CircleCI to add this as an env var? It is exposed in Travis-CI (https://github.com/travis-ci/travis-build/pull/189) and makes tooling much easier to write.

4 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.