- Continuous Integration (CI) Setup for Angular Application in GitLab
- Table of Contents
- 1. Introduction
- 2. Prerequisites
- 3. Setting Up Continuous Integration (CI) for Angular App
- 4. Testing the CI Pipeline
- 5. Visualizing the CI Process in GitLab
- 6. Pre-Merge Checks and Bypassing CI Checks
- 7. Troubleshooting and Advanced Configuration
- 8. Bonus Section
- 9. Sample Repository
- 10. Conclusion
The purpose of this document is to provide a step-by-step guide for setting up a Continuous Integration (CI) pipeline for an Angular application in GitLab. The CI pipeline will automate linting and building processes to ensure code quality and reliability.
This document covers the basic setup of a CI pipeline for an Angular application in GitLab, focusing on linting and building stages. More advanced topics, such as deployment and additional stages, are outside the scope of this guide.
Before setting up the CI pipeline, ensure you have the following prerequisites:
- A GitLab account with access to your target repository.
- An Angular application repository hosted on GitLab.
- Node.js and npm installed on your local machine.
- All required configurations done as per Angular coding standards
- The CI runner needs to be properly configured by your IT administrator to execute CI tasks. In case of GitLab, you need to set up and register GitLab Runners on the machines where your CI/CD jobs will run. These runners should be configured to work with your GitLab project.
- Navigate to your Angular application repository on GitLab.
- Create a file named
.gitlab-ci.yml
in the root directory.
Define the stages for your CI pipeline. In this guide, we will use two stages: lint
and build
.
stages:
- lint
- build
Define variables that specify the branches on which you want to execute your CI processes
variables:
merge_request_branches: "/^main|development$"
You can modify the variable as per your needs. For example, if you want to run the jobs for merge requests targeting any release branch, you can modify the condition as follows:
variables:
merge_request_branches: "/^main|development|release.*/"
The rules template specifies when a job should run. In this example, jobs will run for merge requests targeting the specified branches in your variable.
.rules_template: &rules_template
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ $merge_request_branches)'
Create jobs within each stage to perform linting and building tasks.
linting:
stage: lint
script:
- npm install
- npm run lint
tags:
- Node-18-LTS
<<: *rules_template
building:
stage: build
script:
- npm install
- npm run build
dependencies:
- linting
tags:
- Node-18-LTS
<<: *rules_template
Here is the completed configuration for your .gitlab-ci.yml
file:
stages:
- lint
- build
variables:
merge_request_branches: "/^main|development$"
.rules_template: &rules_template
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ $merge_request_branches)'
linting:
stage: lint
script:
- npm install
- npm run lint
tags:
- Node-18-LTS
<<: *rules_template
building:
stage: build
script:
- npm install
- npm run build
dependencies:
- linting
tags:
- Node-18-LTS
<<: *rules_template
- Create a new branch in your Angular application repository.
- Make changes to the code, as required, and push the branch to GitLab.
- Create a merge request (MR) targeting the main or development branch.
- Navigate to your merge request on GitLab.
- Observe the pipeline execution as it runs the linting and building stages.
- Check the job logs for any errors or warnings.
- Ensure that the pipeline status reflects the success or failure of the linting and building stages.
In this section, we will visually explain the Continuous Integration (CI) process in GitLab and how it works for your Angular application.
When a developer completes a feature or bug fix, they create a new branch in the GitLab repository. Then, they make changes to the code and create a Merge Request (MR) for code review.
Upon MR creation, GitLab's CI pipeline is automatically triggered. The .gitlab-ci.yml
configuration file you've set up defines the stages and jobs to be executed in the pipeline. In our case, the stages are lint and build.
In the lint
and build
stage, the CI pipeline installs the necessary dependencies using Node.js and npm. It then runs the linting process on the codebase to check for any coding standards violations or errors.
-
If Linting or Build Fails:
-
If Linting and Build Passes:
All the pipeline and job actions can be seen and reviewed under the build menu of GitLab
The maintainer can now review the linting and building results in the Merge Request itself. If the pipeline indicates success, it signifies that the code adheres to coding standards and that the build process was successful. This reduces the risk of merging code that may cause errors or disrupt the application.
Before merging any changes into the main codebase, it's essential to ensure that the Continuous Integration (CI) checks have been successfully completed. These checks verify that code changes adhere to coding standards, pass tests, and build successfully. To enforce this, follow these steps:
- Review Pipeline Status: When a Merge Request (MR) is created, monitor the pipeline's progress and results. Ensure that all stages, such as linting and building, complete successfully.
- Merge Only After Success: As a maintainer, it's crucial to enforce the policy of merging changes only when the pipeline passes without errors. If the pipeline fails, work with the contributor to address the issues before proceeding with the merge.
- Bypass Pipeline Check: In certain scenarios, there may be valid reasons for bypassing the CI checks temporarily. It's recommended that leads add a comment in the MR describing the reason for bypassing the CI checks. This helps maintain a record of the decision and the context behind it.
Please note that bypassing CI checks should be used sparingly and only in exceptional cases. The goal is to maintain code quality and ensure that the CI process is an integral part of our development workflow.
If your merge request encounters merge conflicts during the auto-merge stage, manual intervention may be required to resolve the conflicts before the pipeline can proceed.
Modify the scripts in the .gitlab-ci.yml file to match your specific linting and building commands and any additional requirements of your Angular application.
In GitLab CI/CD, artifacts are files or directories generated by a CI/CD job that you want to preserve and make available for other stages or downstream pipelines. Artifacts provide a way to pass data between different jobs or stages in a pipeline, making it easier to manage and share important files or build outputs.
In this example, we'll see how to add artifacts for linting job.
-
In your job, create a file with the job ID in the name before executing the script:
before_script: touch lint-results-job-$CI_JOB_ID.txt
-
During the linting process, redirect the results to the pre-created file:
- npm run lint | tee -a lint-results-job-$CI_JOB_ID.txt
-
Define artifact options for your job, which may include:
- When to create the artifact
- Custom name for the artifact output
- Defining an output path
- Setting an expiry for the File
artifacts: when: always name: '$CI_JOB_ID' paths: - lint-results-job-$CI_JOB_ID.txt expire_in: 3 months
-
The final config with artifact may look like this
stages: - lint - build variables: merge_request_branches: '/^main|development|release.*/' .rules_template: &rules_template rules: - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME =~ $merge_request_branches)' linting: stage: lint before_script: touch lint-results-job-$CI_JOB_ID.txt script: - npm install - npm run lint | tee -a lint-results-job-$CI_JOB_ID.txt tags: - Node-18-LTS artifacts: when: always name: '$CI_JOB_ID' paths: - lint-results-job-$CI_JOB_ID.txt expire_in: 3 months <<: *rules_template building: stage: build script: - npm install - npm run build dependencies: - linting tags: - Node-18-LTS <<: *rules_template
Refer to the official documentation on artifact to get an extensive idea.
-
Navigate to Builds > Pipelines to see a list of pipelines that got triggered. Find the download button at the right, which gives a list of available files to download. Download the required artifact by choosing the appropriate option from the download menu.
-
Download and unzip the file to preview the artifact contents.
Explore this for practical demonstration of CI setups.
Setting up a CI pipeline for your Angular application offers several benefits:
- Improved code quality through automated linting.
- Consistent and reliable builds.
- Early detection of errors and issues.
- Streamlined collaboration through automated testing of merge requests.
Consider enhancing your CI pipeline by adding additional stages such as unit testing, integration testing, and deployment to further improve the quality and reliability of your Angular application.