Exercise : 1 Github actions - Raneesh02/ref_workshop_2 GitHub Wiki
Creating a Github Action for Maven with Java Selenium
1. Fork the repository
Login to github and fork this repository : https://github.com/Raneesh02/selgrid_kube_aws_oct
2. Go to Actions and create a new workflow for Java Ci with Maven
Search maven in workflow search and select Java with Maven
- create a workflow yml and commit to main branch.
- Now create a new branch and do the following
Changes to be Made in the Default Workflow
Trigger Conditions:
- Run on Pull Request Only: We want to run test cases only when a pull request is raised. This helps us detect any potential issues before merging to the main branch.
- Note: Different projects may have different strategies. In some, changes are first merged into the main branch, and if issues arise, they are handled by reverting the commit. Adjust the trigger strategy based on your project requirements.
Java Version Update:
- Use Java 22: For our reference project, we will use Java 22. Modify the Java version as needed for your specific project requirements.
Update Maven Command:
- Run Tests Only: To focus on testing, we will use
mvn test
instead of packaging the project. This allows us to run tests without creating a build artifact.- For more details on the Maven lifecycle, refer to the Maven documentation.
Install Chrome on Ubuntu:
-
Chrome Browser Installation: Chrome is required for our test executions. Use the following script to install Chrome on Ubuntu:
#!/bin/bash set -ex wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo apt install ./google-chrome-stable_current_amd64.deb
Explanation:
set -ex
: This command makes the script exit on any error and prints each command as it executes.wget
: Downloads the Google Chrome.deb
package from Google's official website.sudo apt install
: Installs the downloaded Chrome package using elevated privileges.
-
Script Location: Save this script in the repository under
/scripts/InstallChrome.sh
(the folder name "scripts" is a convention for organizing scripts, but it can be named differently). Ensure this path is accessible by the GitHub Actions workflow to execute the Chrome installation.
our yaml will look like this :
name: Java CI with Maven
on:
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest # image of the virtual machine to run on
steps:
- uses: actions/checkout@v4 # checkout the repository so your workflow can access it
- name: Set up JDK 22
uses: actions/setup-java@v4
with:
java-version: '22'
distribution: 'temurin'
cache: maven
- name: Install Google Chrome # Using shell script to install Google Chrome
run: |
chmod +x ./scripts/InstallChrome.sh
./scripts/InstallChrome.sh
- name: test with maven
run: mvn clean test
What is a yaml? https://spacelift.io/blog/yaml
official documentation: https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions
3. Change to run execution in headless mode
Go to: src/main/java/Base/DriverManager.java
uncomment this line
chromeOptions.addArguments("--headless");
do the changes in the branch and raise a pull request to see the workflow trigger
4. Adding reporting to execution
Search for test reporters while editing workflow : Add this at the end of the document
- name: Report test results
uses: dorny/[email protected]
with:
name: Maven Surefire
path: "**/surefire-reports/TEST-*.xml"
reporter: java-junit