GitLab Duo Principles - Hands-On Lab: Code Generation with GitLab Duo
Estimated time to complete: 15 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.
-
Navigate to your GitLab Duo Principles project.
-
Select Edit > Web IDE.
Note: In this lab we are using the Web IDE, but you can also use Duo Chat and other Duo tools in your local IDE. See available extensions.
-
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.
-
In the chat text input, enter the text
How do I create a hello world Go program?
-
Duo chat 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.
-
Copy the code generated by GitLab Duo.
-
Select the explorer icon in the left sidebar to return to your code.
-
Next to your project name, select the New File icon. The new file should be stored in the root level of your repository.
-
Name the file
main.go
. -
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.
-
In the Web IDE, highlight all of the code in your
main.go
file. -
Select the GitLab Duo icon from the left sidebar.
-
With the code still highlighted, type
/explain
into the chat prompt text box. -
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.
-
Select Source Control in the left sidebar.
-
Enter any commit message, then select
Commit and push to 'main'
-
If you receive a prompt stating
You are committing your changes to the default branch
, selectContinue
. -
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.
-
In the project repository, at the root level, create a new file in the main branch by clicking (+) > This directory > New file.
-
In the Filename field, type
.gitlab-ci.yml
. -
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 stagebuild
with a jobbuild app
that runs as part of the stage. This job runs ourmain.go
app.Again, feel free to highlight the YAML and use
/explain
in GitLab Duo chat to explain the CI pipeline! -
In the Commit message, type any appropriate message.
-
Ensure the Target Branch field is set to main.
-
Select Commit changes.
-
To view the pipeline, select Build > Pipelines to see the pipeline running.
-
Once the pipeline has successfully completed running, click on the
build app
job to view the output of your main.go app. Thebuild 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.
6f6d0996
)