A Git command run from a GitHub workflow returns a different (wrong) result than what it returns when run locally (correctly)

Advertisements

After a process of trial and error, I managed to write this command that should run inside a Git repository to get the last modification date according to the last commit in the directories lib/, images/, and web/:

export LAST_UPDATE=`git ls-tree -r --name-only HEAD lib/ images/ web/ | while read filename; do echo "$(git log -1 --format=%ad --date=iso -- $filename) $filename"; done | sort -r | head -n 1 | cut -d' ' -f1`

This correctly calculates the last modification date (according to Git, not to the filesystem) when run locally. $LAST_UPDATE will have a value like 2024-01-26, even though the last commit of the repository is on 2024-02-01 because the file affected by the last commit is not included in the targeted folders, and the targeted folders were not modified since 2024-01-26. This is the behavior that I wanted.

However, when this command is included in a GitHub workflow, $LAST_UPDATE will contain the date of the last commit (or maybe, the workflow run date, they are the same because it is run on every push). I don’t understand why this is happening on the GitHub servers, even though the command used in the workflow is the same.

Here is the workflow file. The command is run one time in the Build job, at the Build the Web app step, and sends the value to a Flutter web app during the build process (I can see the result through the built app)

name: CI/CD
on:
  push:
    branches:
      - master
jobs:
  Build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.16.x'
          channel: 'stable'
          cache: true
          cache-key: 'flutter-:os:-:channel:-:version:-:arch:'
      - name: Install Flutter packages
        run: flutter pub get
      - name: Configure GitHub Pages
        id: pages
        uses: actions/configure-pages@v4
        with:
          token: ${{ secrets.PAGES_TOKEN }}
      - name: Project build cache
        uses: actions/cache@v3
        with:
          path: |
            build/
            .dart_tool/
          key: ${{ runner.os }}-interactive-cv_build
      - name: Build the Web app
        run: |
          export LAST_UPDATE=`git ls-tree -r --name-only HEAD lib/ images/ web/ | while read filename; do echo "$(git log -1 --format=%ad --date=iso -- $filename) $filename"; done | sort -r | head -n 1 | cut -d' ' -f1`
          flutter build web --base-href="${{ steps.pages.outputs.base_path }}/" \
            --dart-define=OWM_API_KEY=${{ secrets.OWM_API_KEY }} --dart-define=LAST_UPDATE=$LAST_UPDATE \
            --dart-define=GITHUB_PROFILE_TOKEN=${{ secrets.GH_PROFILE_TOKEN }} \
            --dart-define=STACKEXCHANGE_ACCESS_TOKEN="${{ secrets.STACKEXCHANGE_ACCESS_TOKEN }}" \
            --dart-define=STACKEXCHANGE_KEY="${{ secrets.STACKEXCHANGE_KEY }}"
      - name: Upload the Web app
        uses: actions/upload-pages-artifact@v2
        with:
          path: ./build/web
  Deploy:
    runs-on: ubuntu-latest
    needs: Build
    permissions:
      contents: read
      pages: write
      id-token: write
    concurrency:
      group: "pages"
      cancel-in-progress: false
    environment:
      name: github-pages
      url: ${{steps.deployment.outputs.page_url}}
    steps:
      - name: Deploy the Web app
        id: deployment
        uses: actions/deploy-pages@v3

Any suggestions will be greatly appreciated.

>Solution :

Can you try setting the fetch-depth parameter to 0, which indicates that all history for all branches and tags should be fetched:

- name: Checkout
  uses: actions/checkout@v4
  with:
    fetch-depth: 0

This should provide the full commit history needed for your LAST_UPDATE command to work correctly in the GitHub Actions environment, just like it does locally.

Leave a ReplyCancel reply