Quantcast
Channel: DevOps Tutorials Archives
Viewing all articles
Browse latest Browse all 23

GitHub Actions 101 – Introductions

0
0

Hello,

In this article, I want to present GitHub Actions in a small test automation project. GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. The best part is using Github Action is free when you have a GitHub account.

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository or deploy merged pull requests to production.

GitHub Actions goes beyond just DevOps and lets you run workflows when other events happen in your repository. For example, you can run a workflow to automatically add the appropriate labels whenever someone creates a new issue in your repository.

In that article, I will evaluate GitHub Action in a small test automation project.

In this example, I used Gradle as a build tool for my automation project. In order to apply to any other language, all you need to change is some commands.

Let’s start

Project Structure

Firstly, I have a project having WEB and API automation test cases.

Web test cases are implemented by using the PlayWright library. API test cases are implemented by RestAssured and Test Runner library is TestNG. I also used ExtentReport for fancy reporting. Those libraries won’t change anything on my GitHub Actions configuration. So you can use Selenium, Karate or any other library. 

Gradle File Content

My build.gradle file contains two custom commands to run web and API projects separately. The first command is testAPI and the second one is testWeb.

Both take different parameters and execute different TestNg suite.xml files. This part depends on your project. I just wanted to share so you can understand the yml file that I created for GitHub Actions.

Workflow YML File Content

Like every other CI/CD Tool, GitHub actions needs a YML file. In order for GitHub to execute GitHub Actions, you need to create .github>workflows folders and create main.yml file inside workflows folder.

Content of the YML File

GitHub Actions has specific commands. Let’s explain a few of those.

  • name: The name of your workflow. GitHub displays the names of your workflows on your repository’s actions page.
  • on: To automatically trigger a workflow, we use on. We can combine on with some other commands like push. Then we will trigger an action when a push command is executed. You may even combine it with command branches. This is used in my example. In that case, we tell GitHub Actions to do something when a push is done to some specific branch like ‘main’. You can change the branch name of course. 
    on:
    push:
    branches: [ "main" ]
  • permission: You can use permissions to modify the default permissions granted to the GITHUB_TOKEN
  • steps: Each step is either a shell script that will be executed or an action that will be run.
    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK 11
      uses: actions/setup-java@v3
      with:
         java-version: '11'
         distribution: 'temurin'

    Here’s we tell Actions to do a checkout action by using ‘actions/checkout@v3‘ plugin. Then setup java environment with version 11.

Let’s go over how we will execute our Gradle commands inside yml file.

- name: Run Front End Tests
       uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
       with:
         arguments: testWeb -Dheadless=true

Firstly we give a name to our step like ‘Run Front End Tests‘. Then we tell our build to use Gradle inside the ‘gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee‘ plugin. Finally, we provide the arguments we created inside our build.gradle file. You can also pass parameters to your gradle executions.

In my example, if we want to execute our gradle command in a terminal, we would use:

gradle westWeb -Dheadless=true

or

./gradle westWeb -Dheadless=true

The whole yml file is described below

name: Java CI with Gradle

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'temurin'
      - name: Run Front End Tests
        uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
        with:
          arguments: testWeb -Dheadless=true
      - name: Run Backend Tests
        uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
        with:
          arguments: testApi
      - name: Report
        uses: JamesIves/github-pages-deploy-action@4.1.5
        with:
        branch: main
        folder: ./test-output/ExtentReport

Now Action!

Let’s push a change into our project. GitHub will start a new build. Each build will be shown under the Workflow Runs. Now click one and see the details.

In the details, you will see that there’s only one execution. and it’s called build as defined in the yml file. You can see information like ‘who pushed a commit’ and ‘which push’ started this build, then statistics like the total duration of the build. After the execution ends, in case an artifact is created it will be uploaded under the Artifacts tab.

In my project, I used the below Action commands to upload ExtentTest report created after the build to GitHub results.

name: Report
uses: JamesIves/github-pages-deploy-action@4.1.5
with:
branch: main
folder: ./test-output/ExtentReport

 

Then you can click the build and see more details about the execution.

This was a basic intro to GitHub Actions. When I have more time, I will try to add more details to it.

Thanks,
Canberk Akduygu

The post GitHub Actions 101 – Introductions appeared first on Software Test Academy.


Viewing all articles
Browse latest Browse all 23

Latest Images

Trending Articles





Latest Images