0

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.

Luca D'Amico
  • 3,192
  • 2
  • 26
  • 38
  • `git log --since='{7 days ago}'` – phd Nov 24 '22 at 23:31
  • You could easily find https://stackoverflow.com/q/2009577/7976758 or https://stackoverflow.com/a/21743961/7976758 if you tried to search https://stackoverflow.com/search?q=%5Bgit%5D+log+days – phd Nov 24 '22 at 23:53
  • Hi @phd thank you for yor comment. As I've said in the question, I'm aware of the git log command, but I don't know how can I use it in the github workflow yaml. Can you post a complete example? thank you! – Luca D'Amico Nov 25 '22 at 09:08
  • 1
    My comment and links were about how to calculate "7 days" — put the burden onto Git. In Github Actions you just write the command as in the command line. – phd Nov 25 '22 at 09:28
  • @phd ah this make sense now, thanks. I'll create a test repository to give it a try. – Luca D'Amico Nov 25 '22 at 13:00

1 Answers1

1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Luca D'Amico
  • 3,192
  • 2
  • 26
  • 38