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

Time:11-28

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.

CodePudding user response:

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 an huge ammounts 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

    - name: Get Weekly Commits
      run: echo "WEEKLY_COMMITS=$(git log --format=%B --since=7.days | tr '\n' '-')" >> $GITHUB_ENV

    - name: Print Commits List
      run: echo $WEEKLY_COMMITS

The tr '\n' '-' is needed because the newline char ('\n') is not correctly stored in the variable and without it the $WEEKLY_COMMITS variable will print ONLY the first commit of the list.

  • Related