Search code examples
gitgithubgithub-actions

Get all commits pushed to master in the last week using GitHub Workflow


I'd like to create a GitHub Workflow to display all commits pushed to master in the last week (between the current date and the current date minus 7 days).

This is my idea so far:

  1. Get the current date: this is easy, and it was already answered here

  2. Subtract 7 days from the current date: I don't know how to do this yet, in a consistent way.

  3. Obtain the list of commits between these two dates: this can easily be done with the git log command as explained here, but how can this be converted in the GitHub Workflow Yaml?

Can I have some suggestion on points 2 and 3? or if there is any easier way to achieve what I need, please tell me.


Solution

  • I've found a solution that works for my needs, also thanks to @phd's comment.

    I'll write it here, so future users can benefit from it.

    First of all, I found a nice app called act that allows you to test your github workflow scripts locally (without the need to create a huge amount of commits just to test your script).

    The working script is this:

    name: GH-Workflow-Test
    
    on:
      push:
    
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
        - name: Checkout
          uses: actions/checkout@v3
          with:
            fetch-depth: '0'
    
        - name: Get Weekly Commits
          run: |
            echo 'WEEKLY_COMMITS<<EOF' >> $GITHUB_ENV
            git log --format=%B --since=7.days >> $GITHUB_ENV
            echo 'EOF' >> $GITHUB_ENV
    
        - name: Print Commits List
          run: echo ${{ env.WEEKLY_COMMITS }}
    

    EDIT: Updated code without the need of the "tr" command. DO NOT forget to set fetch-depth: '0', otherwise you will be able to retrieve only the last commit.