Skip to content

Add commit hash output to GitHub Action #717

Closed

Description

@michael-pisman

Description

Hi,

The issue is related to another popular GitHub Action actions/checkout which allows to checkout the repository. Unfortunately, when checking out the repository that triggered a workflow, this defaults to the reference or SHA for that event. This means, that the subsequent jobs in the workflow will not include the updates made with PSR action.

For example if we have a workflow with 3 jobs, the first to run PSR and release package to GitHub, the second job to build and release image to Docker Hub(the image should include updated package with changelog changes, new version file, etc from the job 1), and the third job to build and release package to PyPI(the built package should also include all of the updates from the job 1).
Here is an example of this workflow(with some non-important steps amended):

jobs:
  release:
    name: Release Package to GitHub
    outputs:
      released: ${{ steps.release.outputs.released }}
      version: ${{ steps.release.outputs.version }}
      tag: ${{ steps.release.outputs.tag }}
    steps:
      - id: release
        name: Python Semantic Release
        uses: python-semantic-release/python-semantic-release@master
  docker:
    name: Build and push Docker image
    needs: release
    steps:
    - name: Build and push
      uses: docker/build-push-action@v5
      with:
        push: true
        tags: username/image:${{ needs.release.outputs.version }}, username/image:latest
  pypi:
    name: Publish to PyPI
    needs: release
    steps:
    - name: Install dependencies
    - name: Download source code
      uses: actions/checkout@v3
    - name: Build package distributions
    - name: Publish package distributions to PyPI

The way GitHub actions works, the pypi and docker jobs will not include the commits made by release job. A simple git pull to HEAD reference, will pull those commits; However, such solution can also introduce a race condition, in case there were other commits made while the release job was running. More Info about this issue.

The correct solution would be for PSR GitHub Action to output the hash of the commit that has been made, which can be used in other jobs to pull the updates.

Use cases

Using separate jobs for updating version/releasing packages.

Possible implementation

Well, adding outputs would be easy(I assume), but I'm not sure how to get the commit hash after push, if someone can explain which function pushes the committed changes, I can try to work on the pull request.

Thank you

Activity

  1. added
    feature
    on Sep 21, 2023
  2. added
    confirmed
    and removed on Mar 28, 2024
  3. removed
    confirmed
    on Mar 30, 2024
  4. added
    good first issue
    on May 23, 2024
  5. added
    confirmed
    and removed on Aug 22, 2024
  6. wyardley commented on Aug 30, 2024

    @wyardley
    Contributor

    I can try and take a look at this. This should be done in https://github.com/python-semantic-release/python-semantic-release/blob/master/semantic_release/cli/github_actions_output.py vs. the shell entrypoint, right? @codejedi365 do you have a preference for the name the hash shows up as? commit_sha, and then use git.repo.head.object.hexsha to get the value?
    FWIW, this seems like a useful thing to do, but I also think having subsequent workflows run off the tag thatpython-semantic-releasecuts should be relatively reliable and resistant to the issue mentioned? Similarly, since there is already atag` output, this could probably also be used (explicitly check out the tag from the output in the downstream workflows), even if you don't want to run the workflow separately off the cut tag.

    (side note: one quirk I've found recently is that while the node semantic-release cuts releases with [skip ci] by default, and that works fine with CircleCI (doesn't prevent the jobs off the cut tag from running), Google Cloud Build and GitHub Actions won't run the job for the tag when the last commit in the tag has [skip ci] in it. This is kind of a weird corner case, since tags aren't associated with commits.)

  7. codejedi365 commented on Aug 30, 2024

    @codejedi365
    Contributor

    I can try and take a look at this. This should be done in https://github.com/python-semantic-release/python-semantic-release/blob/master/semantic_release/cli/github_actions_output.py vs. the shell entrypoint, right?

    Great! Yes, I think you can just extend the code that auto writes the variable file. That file is the object class where it is defined and modified (unfortunately not in the most standard way) inside of cli/commands/version.py. You could change it over to using property setters with validation to be more official.

    @codejedi365 do you have a preference for the name the hash shows up as? commit_sha, and then use git.repo.head.object.hexsha to get the value?

    That is a way to get the value but be aware it does perform git actions under the hood, no real preference on the name. You will need to consider probably a tag sha as well, as we do support --no-commit. We might already have the sha in one of our data objects that would not require git requests.

    (side note: one quirk I've found recently is that while the node semantic-release cuts releases with [skip ci] by default, and that works fine with CircleCI (doesn't prevent the jobs off the cut tag from running), Google Cloud Build and GitHub Actions won't run the job for the tag when the last commit in the tag has [skip ci] in it. This is kind of a weird corner case, since tags aren't associated with commits.)

    The default [skip ci] is discussed and confirmed as desired enhancement in another issue (#153, #973)

  8. github-actions commented on Oct 30, 2024

    @github-actions

    It has been 60 days since the last update on this confirmed issue. @python-semantic-release/team can you provide an update on the status of this issue?

  9. 13 remaining items

  10. removed
    needs-update
    good first issue
    on Jul 4, 2025
  11. codejedi365 commented on Aug 4, 2025

    @codejedi365
    Contributor

    🎉 This issue has been resolved in v10.3.0 🎉

    You can find more information about this release on the GitHub Releases page.

  12. added and removed
    confirmed
    on Aug 5, 2025
  13. michael-pisman commented on Aug 6, 2025

    @michael-pisman
    Author

    Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurereleased

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions