- Sign in to GitLab and create a new public project/repository
- Select "Import project"
- Select "Repo by URL"
- Enter the URL of this repository and omit any username/password (credentials aren't required because this repository is public).
Finish creating the project and then you should be able to clone your new repository.
Follow the instructions in the Running Locally section to try running the application locally. If you encounter issues running the app locally, feel free to skip to step 3 instead of spending time resolving the issues. The only benefit of running it locally first is to better understand what you want your pipeline to replicate.
-
Create the config file for your continuous integration pipeline:
- Create a file called
.gitlab-ci.yml
in the root of the project
- Create a file called
-
Implement a basic workflow:
stages: - test # This pipeline consists of a single stage called "test" my-job: # Start defining a job called "my-job" stage: test # This job belongs to the "test" stage script: - echo "Hello, world" # Run a script
-
Commit and push your changes to a branch on your repository.
-
On your repository page, navigate to the CI/CD tab.
-
You should see a table of pipelines. This should have one entry with a name matching your latest commit message. Select this entry.
-
You should see a page with a visual representation of your pipeline, you can click on a job to see the detailed console output.
See the GitLab documentation for more details on how to get set up GitLab CI/CD and https://docs.gitlab.com/ee/ci/yaml/index.html for more details on the syntax of the gitlab-ci.yml
file.
First, we want the CI pipeline to build the C# code and run the C# tests. This requires having the .NET SDK installed. Instead of scripting the installation of .NET on the runner, we can run the job inside a container that already has it installed.
Although we have not yet formally introduced containers it's very common to use them "by accident". For example the "Actions" of GitHub Actions often run in containers as well.
For the exercise today we only need to know how to find and reference prebuilt official containers on Docker Hub (think of it as GitHub/GitLab for containers). You do not need to know or have installed any containerisation tools for this workshop.
Amend the job in your workflow file so that it:
-
Uses a Docker image that has the .NET SDK installed
- To do this, add
image: desired-image-name-here
to your job, but with an appropriate image name. Find the correct SDK tag for Microsoft's image repository here (hint: it starts withmcr.microsoft.com
)
- To do this, add
-
Builds the C# code.
-
Runs the C# tests.
Update the script to run the two commands dotnet build
and dotnet test
. Push your changes and check that the pipeline succeeds.
Next, we want the pipeline to validate the TypeScript code. You need a different image for this (with Node installed instead of .NET) so create a second job with an appropriate image. This new job can belong to the same stage as the first one, because they do not depend on each other and can happily run in parallel.
Fill in the "script" section with the correct commands so that it:
- Uses a Docker image with Node installed
- Installs dependencies and build the TypeScript code.
- Runs the linter on the TypeScript code.
- Runs the TypeScript tests.
Remember that you want to run the npm
commands from the DotnetTemplate.Web directory.
Change your workflow so that it only runs when pushing to the main branch or by raising a Merge Request. Is there a way to ensure that no one can update the main branch except through a PR that has passed the workflow?
Look at the sequencing of your two pipeline jobs. Do they run in parallel or in series? How would you change this behaviour?
Add a pipeline status badge to your repository.
To make sure people are aware when there are issues with the build, it can be useful to send a Slack notification at the end of the workflow.
Before attempting this step please create your own personal slack workspace. This is free and can be set up here.
Take a look at the GitLab Slack integration.