DEV Community

Cover image for Creating an Automation Pipeline with AWS CodeBuild for Robot Framework
Mikael Bôto
Mikael Bôto

Posted on

Creating an Automation Pipeline with AWS CodeBuild for Robot Framework

AWS CodeBuild is a powerful solution for automatically building, testing, and packaging applications. You also have the freedom to control how much computing power your runner will have, thus optimizing build time. In this approach, we will explore how to set up a complete stack with AWS CodeBuild, integrating code repositories such as GitHub, GitLab, or Azure DevOps, and using webhooks to automatically trigger pipelines. In addition, we will explain the concept of buildspec.yml and its configuration to perform an E2E integrated testing pipeline using Robot Framework, with the result being made available as a static website hosted on Amazon S3.

Integration with Code Repositories and Webhooks

AWS CodeBuild supports direct integration with various code repository providers. For pipeline configuration, you can choose GitHub, GitLab, or Azure DevOps. Once configured, you can enable webhooks that monitor specific events, such as pushes or pull requests. For organizations using GitHub, specific actions in another repository can also trigger the build process.

In the CodeBuild setup, select the repository source.

In the CodeBuild setup, select the repository source.

You can also choose the runner's operating system. In our example, we will use Ubuntu with other default configurations.

You can also choose the runner's operating system. In our example, we will use Ubuntu with other default configurations.

When creating the role, ensure it has a policy allowing access to the S3 bucket for deployment. Additionally, enable

When creating the role, ensure it has a policy allowing access to the S3 bucket for deployment. Additionally, enable "Privileged" mode for the runner to execute commands as sudo.

In advanced settings, you can configure the vCPU and memory for the runner. For environments with robust tests, consider increasing these resources. Define the environment variables as shown below.

In advanced settings, you can configure the vCPU and memory for the runner. For environments with robust tests, consider increasing these resources. Define the environment variables as shown below.

Configuring Buildspec

The buildspec.yml file is used by AWS CodeBuild to define the pipeline steps, functioning similarly to the YAML file in GitHub Actions. It can be stored directly in the repository or configured manually in the CodeBuild console.

If you choose to keep the buildspec.yml file in the repository, select the

If you choose to keep the buildspec.yml file in the repository, select the "buildspec file" option and specify its path. For our example, we will define it directly in the console, switching to the editor.

Switching to the editor allows you to paste the following code to create the pipeline. Leave other CodeBuild settings as default.

Switching to the editor allows you to paste the following code to create the pipeline. Leave other CodeBuild settings as default.

Structure of a Buildspec

The buildspec.yml file follows a YAML structure and includes the following sections:

  • version: Defines the version of the buildspec format.
  • phases: Specifies the build steps: install, pre_build, build, and post_build.
  • artifacts: Defines which files or folders should be exported after execution.
  • env: Configures environment variables used during the process.

Below is an example of buildspec.yml for an E2E integrated test pipeline using Robot Framework:

version: 0.2
run-as: root

phases:
  pre_build:
    commands:
      - sudo apt-get update
      # Installation of Python3
      - echo "Installing Python3"
      - sudo apt install python3
      # Installation of NodeJS
      - echo "Installing NodeJS"
      - sudo apt install nodejs -y
      - node -v
      - sudo apt install npm -y
      - npx playwright install-deps
      - sudo apt install xvfb -y

  build:
    commands:
      # Create the Virtual Environment
      - echo "Creating virtual environment"
      - python3 -m venv venv
      - ls -lha
      # Install Libraries:
      - echo "Installing Libraries"
      - pip install -r requirements.txt
      # Initialize the Browser library
      - echo "Initializing Browser library"
      - rfbrowser init
      # Running the tests
      - echo "Running tests"
      - robot -d my_reports --variable HEADLESS_FLAG:True Tests 

  post_build:
    commands:
      # Generate a Metric Report after execution
      - ls -lha
      - echo "Generating Metric Report"
      - robotmetrics --inputpath ./my_reports/ --output output.xml 
      # Copy results to S3
      - echo "Copying results to S3"
      - aws s3 cp ./my_reports/ $S3_URI/results --recursive
      - echo "You can see the results at " $S3_URL

artifacts:
  files: imagedefinitions.json
Enter fullscreen mode Exit fullscreen mode
  • S3_URI: URI of the S3 bucket created to host the static website
  • S3_URL: URL generated by the specific website in S3
  • Headless Flag: Set to True, so the robot will not look for a graphical interface to execute the command, which is not necessary since we are executing in a pipeline

Where to Store the Buildspec

  • In the Repository: Include the buildspec.yml file in the project's root directory.
  • In the CodeBuild Console: Copy and paste the content directly into the configuration field.

Deploying Results to Amazon S3

After executing the tests, results will be stored as artifacts in Amazon S3. This bucket will be configured to host a static website, allowing QA testers to easily access the reports.

S3 Configuration

  1. Create the Bucket: Set up an S3 bucket for hosting.

  2. Define Permissions: Ensure appropriate access permissions for public viewing of results.

Viewing Results

Once the build process is complete, QA testers can access the configured S3 bucket endpoint to view the test reports.

  • Public URL: Configure the bucket to generate a public link.
  • HTML Report: Ensure the results are in a readable format (e.g., HTML generated by Robot Framework).

This is an example of the file structure generated by the pipeline.

This is an example of the file structure generated by the pipeline.

Above is an example of how it would look using S3 static site hosting.

Above is an example of how it would look using S3 static site hosting.

Conclusion

This stack using AWS CodeBuild and Amazon S3 provides a robust solution for QA automation and results visualization. The integration with webhooks ensures a dynamic process, while the buildspec.yml configuration offers flexibility and full control over pipeline stages.

For further inquiries, feel free to connect, and we can discuss this in more detail!

Top comments (0)