stefanzweifel / git-auto-commit-action

Automatically commit and push changed files back to GitHub with this GitHub Action for the 80% use case.
MIT License
1.97k stars 226 forks source link

Commit message including environment variables #232

Closed alex-page closed 2 years ago

alex-page commented 2 years ago

Is your feature request related to a problem? Please describe.

Not really, it's just an enhancement that could make this action nice to use. BTW this action is great, thank you.

Describe the solution you'd like

I would like to customise the commit message with env variables $GITHUB_SHA or other shell scripts like date. In shell you can do something like:

VERSION="1.0.0"
git commit -m "we add version $VERSION and that's it"

Describe alternatives you've considered

I tried a bunch of different approaches but it always seems to render out the string of the environment variable. I might be doing something incorrect.

Additional context

This is the GitHub action I made and the environment variable has the correct value but it's not adding it to the commit message.

name: Trigger deploy

on:
  workflow_dispatch:

jobs:
  trigger-deploy:
    runs-on: self-hosted

    steps:
      - name: Checkout branch
        uses: actions/checkout@v2

      - name: Set COMMIT_HASH
        run: echo COMMIT_HASH=`git ls-remote https://github.com/Shopify/polaris.git refs/heads/main | cut -c1-7` >> $GITHUB_ENV

      - name: Update version file
        run: echo $COMMIT_HASH > version

      - name: Commit version file
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: "deploy $COMMIT_HASH"
          skip_dirty_check: true
          commit_options: "--allow-empty"

I also tried a bunch of variations:

commit_message: deploy $COMMIT_HASH
stefanzweifel commented 2 years ago

Hi Alex

This should be doable by defining the commit_hash using output parameters.

For your usecase this would look like this

echo "::set-output name=commit_hash::$(git ls-remote https://github.com/Shopify/polaris.git refs/heads/main | cut -c1-7)";

The entire updated workflow would look like this:

name: Trigger deploy

on:
  workflow_dispatch:

jobs:
  trigger-deploy:
    runs-on: self-hosted

    steps:
      - name: Checkout branch
        uses: actions/checkout@v2

      - name: Set COMMIT_HASH
        id: commit-hash
        run: |
          echo COMMIT_HASH=`git ls-remote https://github.com/Shopify/polaris.git refs/heads/main | cut -c1-7` >> $GITHUB_ENV

          # Create a new `commit_hash` output parameter for this workflow step
          echo "::set-output name=commit_hash::$(git ls-remote https://github.com/Shopify/polaris.git refs/heads/main | cut -c1-7)";

      - name: Update version file
        run: echo $COMMIT_HASH > version

      - name: Commit version file
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          # Use the ID of the step to get the calculated commit_hash
          commit_message: "deploy ${{ steps.commit-hash.outputs.commit_hash }}"
          skip_dirty_check: true
          commit_options: "--allow-empty"
alex-page commented 2 years ago

Legend. Thank you @stefanzweifel, this worked perfectly.