โ€Œ
category-iconCASE STUDY

Automating Maven Tests with Allure Reports on GitHub Actions

14 Jul 202581.6K0
Blog Thumbnail

Automating Maven Tests with Allure Reports on GitHub Actions

GitHub Actions is a powerful CI/CD tool that integrates seamlessly with GitHub repositories. In this blog, you'll learn how to automate a Maven-based test framework using GitHub Actions and generate elegant Allure test reports โ€” all deployed to GitHub Pages. This article dives deep into each part of the workflow with examples and best practices.





โœจ Overview

In this example, our goals are:

  • Run Maven tests automatically on code pushes and pull requests.
  • Generate Allure Reports.
  • Deploy those reports to GitHub Pages for public or internal viewing.



๐Ÿ“‚ GitHub Actions Workflow File Breakdown

Letโ€™s go step-by-step through the following maven-tests.yml file:

name: Maven Test with Allure Report

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

โœจ Explanation:

  • This defines when the workflow runs.
  • It will trigger on every push and pull request to the main branch.
jobs:
  test:
    runs-on: ubuntu-latest
  • This starts a job called test and runs it on Ubuntu's latest version.
permissions:
  contents: write  # Needed for gh-pages deployment
  • Grants the workflow permission to write to the repository, which is necessary to push to GitHub Pages.



๐Ÿš€ Step-by-Step: The steps Section

1. โœ… Checkout Repository

- name: Checkout repository
  uses: actions/checkout@v3
  • Downloads your repository to the GitHub runner so tests can be run against the code.

2. โ˜• Set Up Java

- name: Set up JDK 21
  id: setup-java
  uses: actions/setup-java@v3
  with:
    distribution: temurin
    java-version: 21
  • Sets up Java Development Kit (JDK) 21 using the Temurin distribution (recommended).

3. ๐Ÿ“ Cache Maven Dependencies

- name: Cache Maven dependencies
  uses: actions/cache@v3
  with:
    path: ~/.m2/repository
    key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
    restore-keys: |
      ${{ runner.os }}-maven-
  • Caches dependencies to make builds faster and more efficient.
  • ~/.m2/repository is the default Maven local repository path.

4. ๐Ÿ”ฎ Run Tests

- name: Run tests (with Allure results)
  run: mvn clean test -Dallure.results.directory=target/allure-results || true
  • Runs tests using Maven.
  • If tests fail, || true ensures the workflow still proceeds (so reports can still be generated).

5. ๐Ÿ”ง Install Allure CLI

- name: Install Allure CLI
  run: sudo npm install -g allure-commandline --unsafe-perm=true
  • Installs Allure CLI globally via npm so you can generate reports later.
  • --unsafe-perm=true is required for installing CLI tools with root.

6. ๐Ÿ“„ Add Executor Info

- name: Add Allure Executor Info
  run: |
    mkdir -p target/allure-results
    echo '... JSON ...' > target/allure-results/executor.json
  • Creates metadata for Allure showing this run came from GitHub Actions.
  • This improves traceability and reporting context.

7. ๐Ÿ“ˆ Generate Allure Report

- name: Generate Allure Report
  run: |
    if [ -d "target/allure-results" ] && [ "$(ls -A target/allure-results)" ]; then
      allure generate target/allure-results --clean -o allure-report
    else
      mkdir -p allure-report
      echo "<html><body><h1>No Allure results found.</h1></body></html>" > allure-report/index.html
  • If results are found, generate the report using Allure.
  • If not, create a placeholder HTML report (to avoid deployment failure).

8. ๐Ÿš€ Deploy to GitHub Pages

- name: Deploy Allure Report to GitHub Pages
  if: always()
  uses: peaceiris/actions-gh-pages@v4
  with:
    github_token: ${{ secrets.GITHUB_TOKEN }}
    publish_dir: ./allure-report
    publish_branch: gh-pages
    force_orphan: true
  • This deploys the allure-report directory to the gh-pages branch.
  • if: always() ensures it runs even if earlier steps fail.
  • force_orphan: true creates a fresh branch with no history for cleaner pages.



๐Ÿ“ Understanding Versions: @v2 vs @v3

In lines like uses: actions/checkout@v3, the @v3 means:

  • Use version 3 of the checkout action.
  • It's like versioning libraries in Maven/Gradle/NPM.

You can use:

FormatMeaning@v3Latest major version 3@v3.1.2Specific version for full control@<commit-sha>Pin to an exact commit (most stable)

Avoid @main unless you're contributing to the action itself.





๐Ÿ”Ž Live Report Access

After deployment, your report will be viewable at:

https://<username>.github.io/<repository-name>/

Example:

https://anisurrahman.github.io/my-maven-tests/



๐Ÿ“„ Sample executor.json

Hereโ€™s a complete example of executor.json:

{
  "name": "GitHub Actions",
  "type": "github",
  "url": "https://github.com/anisurrahman/my-repo/actions/runs/1234567890",
  "buildOrder": 1,
  "buildName": "Maven Test with Allure Report",
  "buildUrl": "https://github.com/anisurrahman/my-repo/actions/runs/1234567890",
  "reportUrl": "https://anisurrahman.github.io/my-repo/",
  "reportName": "Allure Report"
}



๐Ÿ“† Best Practices

  • Use @v3 for stable versions.
  • Always add executor.json for full CI/CD traceability.
  • Use || true with test commands to prevent broken builds from skipping reports.
  • Create placeholder reports to prevent deployment errors.
  • Use gh-pages as a public archive of test results.



๐Ÿš€ Bonus Tips

  • Use GitHub Environments for staging/production separation.
  • Use matrix builds to test across multiple Java versions.
  • Use branch-specific deployment (gh-pages-dev, gh-pages-prod).



๐Ÿ“ Final Words

This GitHub Actions + Allure setup is ideal for open-source projects and internal enterprise QA pipelines. It makes test results visible, accessible, and trackable for every code change.

By mastering this setup, you're improving collaboration, transparency, and speed in your QA and development process.






๐Ÿงพ Complete Workflow File


name: Maven Test with Allure Report

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest

    permissions:
      contents: write  # Needed for gh-pages deployment

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up JDK 21
        id: setup-java
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 21

      - name: Cache Maven dependencies
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-

      - name: Run tests (with Allure results)
        run: mvn clean test -Dallure.results.directory=target/allure-results || true

      - name: Install Allure CLI
        run: sudo npm install -g allure-commandline --unsafe-perm=true

      - name: Add Allure Executor Info
        run: |
          mkdir -p target/allure-results
          echo '{
            "name": "GitHub Actions",
            "type": "github",
            "url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}",
            "buildOrder": 1,
            "buildName": "${{ github.workflow }}",
            "buildUrl": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}",
            "reportUrl": "https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/",
            "reportName": "Allure Report"
          }' > target/allure-results/executor.json

      - name: Generate Allure Report
        run: |
          if [ -d "target/allure-results" ] && [ "$(ls -A target/allure-results)" ]; then
            echo "โœ… Allure results found. Generating report..."
            allure generate target/allure-results --clean -o allure-report
          else
            echo "โš ๏ธ No Allure results found. Creating placeholder report..."
            mkdir -p allure-report
            echo "<html><body><h1>No Allure results found.</h1></body></html>" > allure-report/index.html
          fi

      - name: Deploy Allure Report to GitHub Pages
        if: always()
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./allure-report
          publish_branch: gh-pages
          force_orphan: true






๐Ÿงพ Allure Report Sample


ย 



testautomationmavencicdtoolsgitworkflowgithubactionsallurereporting