2

I'm having a problem with correctly setting up CI using GitHub actions. I created one workflow with 2 jobs. First job autoformats the whole project using Google-Java-Style action. After success it creates a new commit with changes and starts a second action that runs ./gradlew build. Build is failing every time on checkstyleMain task. I need to find a way to somehow "redirect" this second action to the new autoformat commit that includes the formatting changes.

My .yml file looks like this:

name: Format and Build

on:
  push:
  workflow_dispatch:

jobs:
  formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3 # v2 minimum required
      - uses: axel-op/googlejavaformat-action@v3
        with:
          skip-commit: true
          args: "--skip-sorting-imports --replace"
          commit-message: "refactor: AUTO formatted by bot"
          version: "1.16.0"
          github-token: ${{ secrets.GITHUB_TOKEN }}

  build:
    needs: formatting
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Build with Gradle
        run: ./gradlew build --no-daemon

I was trying to run this in second action ./gradlew build -x checkstyleMain. It seems to work but I would prefer to overcome this problem, not just find the way around it.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341

1 Answers1

0

Though technically it's probably better to run the formatting script locally prior to commit or using a pre-commit hook, this works.

In your formatting job create an output variable that stores the sha. use that a ref in the checkout step in the 2nd job.

permissions:
  contents: write

jobs:
  formatting:
    outputs:
      sha: ${{ steps.postcommit.newsha }}

    steps:

    ...

    - run: |
        echo newsha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
      id: postcommit
      shell: bash

  build:
    needs: formatting
    steps:
    - uses: actions/checkout@v2
      with:
        ref: ${{ needs.formatting.outputs.sha }}

jessehouwing
  • 106,458
  • 22
  • 256
  • 341