Skip to content

Latest commit

 

History

History
177 lines (127 loc) · 9.42 KB

File metadata and controls

177 lines (127 loc) · 9.42 KB

Continuous integration and testing

← Cloud-based development with GitHub Codespaces Next: Helping GitHub Copilot understand context →

Chances are you've heard the abbreviation CI/CD, which stands for continuous integration and continuous delivery (or sometimes continuous deployment). CI is centered on incorporating new code into the existing codebase, and typically includes running tests and performing builds. CD focuses on the next logical step, taking the now validated code and generating the necessary outputs to be pushed to the cloud or other destinations. This is probably the most focused upon component of DevOps.

CI/CD fosters a culture of rapid development, collaboration, and continuous improvement, allowing organizations to deliver software updates and new features more reliably and quickly. It ensures consistency, and allows developers to focus on writing code rather than performing manual processes.

GitHub Actions is an automation platform upon which you can build your CI/CD process. It can also be used to automate other tasks, such as resizing images and validating machine learning models.

Scenario

A set of unit tests exist for the Python server for the project. You want to ensure those tests are run whenever someone makes a pull request (PR). To meet this requirement, you'll need to define a workflow for the project, and ensure there is a trigger for pull requests to main. Fortunately, GitHub Copilot can aid you in creating the necessary YML file!

Exploring the test

Let's take a look at the tests defined for the project.

Note

There are only a few tests defined for this project. Many projects will have hundreds or thousands of tests to ensure reliability.

  1. Return to your codespace, or reopen it by navigating to your repository and selecting Code > Codespaces and the name of your codespace.
  2. In Explorer, navigate to app > server and open test_app.py.
  3. Open GitHub Copilot Chat and ask for an explanation of the file.

Note

Consider using the following GitHub Copilot tips to gain an understanding of the tests:

  • /explain is a slash command to quickly ask for an explanation
  • Highlight specific sections of the file to focus on areas you may have questions about

Understanding workflows

To ensure the tests run whenever a PR is made you'll define a workflow for the project. Workflows can perform numerous tasks, such as checking for security vulnerabilities, deploying projects, or (in our case) running unit tests. They're central to any CI/CD.

Creating a YML file can be a little tricky. Fortunately, GitHub Copilot can help streamline the process! Before we work with Copilot to create the file, let's explore some core sections of a workflow:

  • name: Provides a name for the workflow, which will display in the logs.
  • on: Defines what will trigger the workflow to run. Some common triggers include pull_request (when a PR is made), merge (when code is merged into a branch), and workflow_dispatch (manual run).
  • jobs: Defines a series of jobs for this workflow. Each job is considered a unit of work and has a name.
    • name: Name and container for the job.
    • runs-on: Where the operations for the job will be performed.
    • steps: The operations to be performed.

Create the workflow file

Now that we have an overview of the structure of a workflow, let's ask Copilot to generate it for us!

  1. Create a new folder under .github named workflows.
  2. Create a new file named server-test.yml and ensure the file is open.
  3. If prompted to install the GitHub Actions extension, select Install.
  4. Open GitHub Copilot Chat.
  5. Add the test file test_app.py to the context by using the # in the Chat dialog box and beginning to type test_app.py, and pressing enter when it's highlighted.
  6. Prompt Copilot to create a GitHub Action workflow to run the tests. Use natural language to describe the workflow you're looking to create (to run the tests defined in test_app.py), and that you want it to run on merge (for when new code is pushed), when a PR is made, and on demand.

Important

A prescriptive prompt isn't provided as part of the exercise is to become comfortable interacting with GitHub Copilot.

  1. Add the generated code to the new file by hovering over the suggested code and selecting the Insert at cursor button. The generated code should resemble the following:
name: Server Tests

on:
  push:
    branches: [ main ]
    paths:
      - 'app/server/**'
  pull_request:
    branches: [ main ]
    paths:
      - 'app/server/**'

jobs:
  server-test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.10'
        
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        if [ -f app/server/requirements.txt ]; then pip install -r app/server/requirements.txt; fi
        pip install pytest
        
    - name: Run tests
      working-directory: ./app/server
      run: |
        python -m pytest test_app.py -v

Important

Note, the file generated may differ from the example above. Because GitHub Copilot uses generative AI, there results will be probabilistic rather than deterministic.

Tip

If you want to learn more about the workflow you just created, ask GitHub Copilot!

Push the workflow to the repository

With the workflow created, let's push it to the repository. Typically you would create a PR for any new code (which this is). To streamline the process, we're going to push straight to main as we'll be exploring pull requests and the GitHub flow in a later exercise. You'll start by obtaining the number of the issue you created earlier, creating a commit for the new code, then pushing it to main.

Note

All commands are entered using the terminal window in the codespace.

  1. Use the open terminal window in your codespace, or open it (if necessary) by pressing Ctrl + `.

  2. List all issues for the repository by entering the following command in the terminal window:

    gh issue list
  3. Note the issue number for the one titled Implement testing.

  4. Stage all files by entering the following command in the terminal window:

    git add .
  5. Commit all changes with a message by entering the following command in the terminal window, replacing <ISSUE_NUMBER> with the number for the Implement testing issue:

    git commit -m "Resolves #<ISSUE_NUMBER>"
  6. Push all changes to the repository by entering the following command in the terminal window:

    git push

Congratulations! You've now implemented testing, a core component of continuous integration (CI)!

Seeing the workflow in action

Pushing the workflow definition to the repository counts as a push to main, meaning the workflow will be triggered. You can see the workflow in action by navigating to the Actions tab in your repository.

  1. Return to your repository.
  2. Select the Actions tab.
  3. Select Server test on the left side.
  4. Select the workflow run on the right side with a message of Resolves #<ISSUE_NUMBER>, matching the commit message you used.
  5. Explore the workflow run by selecting the job name

You've now seen a workflow, and explore the details of a run!

Summary and next steps

Congratulations! You've implemented automated testing, a standard part of continuous integration, which is critical to successful DevOps. Automating these processes ensures consistency and reduces the workload required for developers and administrators. You have created a workflow to run tests on any new code for your codebase. Let's explore context with GitHub Copilot chat.

Resources

← Cloud-based development with GitHub Codespaces Next: Helping GitHub Copilot understand context →