DevSecOps with GitLab Duo - Hands-On Lab: Getting Started with GitLab Duo

This Hands-On Guide walks you through using GitLab Duo to generate code.

Estimated time to complete: 30 minutes

Objectives

To get started with GitLab Duo, we will test out some of the basic code generation features. Code generation allows you to prompt GitLab Duo for code that solves a specific programming problem.

Task A. Generating some Simple Code

In this task, we are going to generate a basic ‘hello world’ program in Go using GitLab Duo. This task will help you get familiar with the process of creating code in a project with GitLab Duo.

  1. Navigate to your GitLab Duo Principles project.

  2. Select Edit > Web IDE.

  3. In the Web IDE, locate the GitLab Duo Chat icon. This icon will be the last item in the left side bar, showing the GitLab icon.

  4. In the chat text input, enter the text How do I create a hello world go program?

  5. The AI should respond with code similar to this:

    package main
    
    import "fmt"
    
    func main() {
      fmt.Println("hello world")
    }
    

    Note: Any AI generator will be non-deterministic in nature. This means that when you prompt an AI, there is a possibility that your answer may differ from someone else’s answer. If you ever receive an output that appears significantly different from the lab guide, please use the lab guide snippets to ensure you can follow along with other examples.

  6. Copy the code generated by the AI. Select the explorer icon in the left sidebar to return to your code.

  7. Right click in the left sidebar, and select New File. The new file should be stored in the root level of your repository.

  8. Name the file main.go.

  9. Inside the file, paste the AI generated code.

You have now created a simple hello world program in Go! In the next section, you will learn how to explain AI generated code.

Task B. Explaining AI Code

Being able to generate code is only one part of the software development process. In many cases, you also want to be able to understand code that has been written. Whether the code is generated by coworkers or AI, GitLab Duo can explain any code snippet you provide.

  1. In the Web IDE, highlight all of the code in your main.go file.

  2. Select the GitLab Duo icon from the left sidebar.

  3. With the code still highlighted, type /explain into the GitLab Duo chat.

  4. You will see an explanation of all of the code you have highlighted as a result of the prompt.

Task C. Commit your Code

Now that you have code generated and you understand the code, you can commit the code into your project.

  1. Select Source Control in the left sidebar.

  2. Enter any commit message, then select Commit to 'main'

  3. If you receive a prompt stating You are committing your changes to the default branch, select Continue.

  4. Select Go to Project to return to your GitLab repository.

Task D. Add a .gitlab-ci.yml File

The last step of this lab is to create a .gitlab-ci.yml file to build our Go application.

  1. In the project repository, at the root level, create a new file in the main branch by clicking (+) > This directory > New file.

  2. In the Filename field, type .gitlab-ci.yml.

  3. Copy the YAML below into the file.

    stages:
      - build
    
    default:
      image: golang:latest
    
    build app:
      stage: build
      script:
        - go run main.go
    

    This .gitlab-ci.yml file has one stage build with a job build app that runs as part of the stage. This job runs our main.go app.

    Again, feel free to highlight the YAML and use /explain in GitLab Duo chat to explain the CI pipeline!

  4. In the Commit message, type any appropriate message.

  5. Ensure the Target Branch field is set to main.

  6. Select Commit changes.

  7. To view the pipeline, select Build > Pipelines to see the pipeline running.

  8. Once the pipeline has successfully completed running, click on the build app job to view the output of your main.go app. The build app job can be found under your Stages section in the build stage.

Lab Guide Complete

You have completed this lab exercise. You can view the other lab guides for this course.

Suggestions?

If you’d like to suggest changes to the lab, please submit them via merge request.

Last modified June 27, 2024: Fix various vale errors (46417d02)