1

I am trying to implement Github-actions(bot), which runs gradle test when PR has been created. To assure that my workflow file works as I expected, I explicitly wrote a test method which should cause failure.

@Test
fun thisShouldFail() {
    assertEquals(1, 2)
}

When I try testing on my local machine, I get the log below.

> Task :test FAILED

FAILURE: Build failed with an exception.

# More

Above log indicates that there was something wrong in the test codes as I expected it to be. But then Github actions bot runs this command, the test code result is SUCCESS.

Below is my github workflow yaml file for this action.

name: PullRequestGradleTest

on:
  pull_request_target:
    types: [labeled]

jobs:
  test:
    name: GradleTest
    runs-on: ubuntu-latest
    if: contains(github.event.pull_request.labels.*.name, 'STAGING')

    steps:
      - name: checkout
        uses: actions/checkout@v2

      - name: Setup JDK 1.8
        uses: actions/setup-java@v2
        with:
          java-version: '8'
          distribution: 'adopt'

      - name: Grant Permissions to gradlew
        run: chmod +x gradlew
      - name: Test
        run: gradle test --tests "*"

      - name: Test Success
        if: success()
        uses: actions/[email protected]
        with:
          github-token: ${{ github.token }}
          script: |
            const pull_number = "${{github.event.number}}"
            await github.pulls.createReview({
              ...context.repo,
              pull_number,
              body: "All tests passed.",
              event: "APPROVE"
            })
      - name: Test Fail
        if: failure()
        uses: actions/[email protected]
        with:
          github-token: ${{ github.token }}
          script: |
            const pull_number = "${{github.event.number}}"
            await github.pulls.createReview({
              ...context.repo,
              pull_number,
              body: "There is something wrong with test codes.",
             event: "REQUEST_CHANGES"
            })
            await github.pulls.update({
              ...context.repo,
              pull_number,
              state: "closed"
            })

2 Answers 2

3

I found that you are using gradle, not gradlew.

name: PullRequestGradleTest

on:
  pull_request_target:
    types: [labeled]

jobs:
  test:
    name: GradleTest
    runs-on: ubuntu-latest
    if: contains(github.event.pull_request.labels.*.name, 'STAGING')

    steps:
      - name: checkout
        uses: actions/checkout@v2

      - name: Setup JDK 1.8
        uses: actions/setup-java@v2
        with:
          java-version: '8'
          distribution: 'adopt'

      - name: Grant Permissions to gradlew
        run: chmod +x gradlew
      - name: Test
        run: ./gradlew test --tests "*"

      - name: Test Success
        if: success()
        uses: actions/[email protected]
        with:
          github-token: ${{ github.token }}
          script: |
            const pull_number = "${{github.event.number}}"
            await github.pulls.createReview({
              ...context.repo,
              pull_number,
              body: "All tests passed.",
              event: "APPROVE"
            })
      - name: Test Fail
        if: failure()
        uses: actions/[email protected]
        with:
          github-token: ${{ github.token }}
          script: |
            const pull_number = "${{github.event.number}}"
            await github.pulls.createReview({
              ...context.repo,
              pull_number,
              body: "There is something wrong with test codes.",
             event: "REQUEST_CHANGES"
            })
            await github.pulls.update({
              ...context.repo,
              pull_number,
              state: "closed"
            })

If you use gradle in the command, it will depend on the machine's environment. In this case, there is a possibility that occurs error because of Gradle version. Therefore, you need to use the project's Gradle which is included with your repo. The way to use is using gradlew scripts.

I also recommend following these three steps to test the branch for the pull request.

Clean -> Assemble(or build) -> Test

Sign up to request clarification or add additional context in comments.

2 Comments

Great! Actually changed from ./gradlew test to gradle test because I also thought that something was wrong with gradle, but it wasn't the root cause of this problem. :)
Then, you might need to provide detailed information like showing exception message or stacktrace to solve this problem.
1

Base problem was that if we use pull_request_target event, the action runs on the target branch, which would be the base branch that the PR will be merged into. To solve this problem, I had to explicitly set where this action will run on.

On job => steps

steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          repository: ${{github.event.pull_request.head.repo.full_name }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.