Continuous Integration (CI) and Continuous Deployment (CD) are crucial practices in modern software development that automate the process of integrating code changes, testing them, and deploying them to production. This chapter will provide a comprehensive guide to setting up CI/CD for TypeScript projects, from basic concepts to advanced techniques. We will cover various tools and practices to ensure your TypeScript applications are continuously integrated and deployed efficiently.
Continuous Integration (CI) is a practice where developers frequently integrate their code changes into a shared repository, triggering automated builds and tests to detect issues early. Continuous Deployment (CD) extends this practice by automatically deploying the integrated code to production environments.
To set up a TypeScript project for CI/CD, you need a version control system (like Git), a build tool, a testing framework, and a CI/CD service.
mkdir my-typescript-project
cd my-typescript-project
npm init -y
npm install typescript --save-dev
npx tsc --init
my-typescript-project/
├── src/
│ ├── index.ts
├── tests/
│ ├── index.test.ts
├── tsconfig.json
├── package.json
├── .gitignore
├── .eslintrc.json
├── jest.config.js
// tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
git init
git add .
git commit -m "Initial commit"
git remote add origin
git push -u origin main
Automated testing ensures that code changes do not break the existing functionality.
npm install jest ts-jest @types/jest --save-dev
// jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/tests/**/*.test.ts']
};
// src/index.ts
export function add(a: number, b: number): number {
return a + b;
}
// tests/index.test.ts
import { add } from '../src/index';
test('adds 1 + 2 to equal 3', () => {
expect(add(1, 2)).toBe(3);
});
npx jest
PASS tests/index.test.ts
✓ adds 1 + 2 to equal 3 (5 ms)
Linting helps maintain code quality by enforcing coding standards.
npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
// .eslintrc.json
{
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/recommended"
],
"rules": {
"semi": ["error", "always"],
"quotes": ["error", "single"]
}
}
npx eslint src/**/*.ts
Building the project is the process of compiling TypeScript to JavaScript.
// package.json
{
"scripts": {
"build": "tsc"
}
}
npm run build
Deployment strategies include direct deployment, blue-green deployment, and canary deployment.
scp -r dist/* user@server:/path/to/deploy
ssh user@server 'pm2 restart my-app'
GitHub Actions is a powerful CI/CD tool integrated with GitHub.
// .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run Lint
run: npm run lint
- name: Run Tests
run: npm test
- name: Build
run: npm run build
git add .github/workflows/ci.yml
git commit -m "Add GitHub Actions workflow"
git push
You can see the CI results in the Actions tab of your GitHub repository.
GitLab CI is another popular CI/CD tool that is integrated with GitLab.
.gitlab-ci.yml
File:
// .gitlab-ci.yml
stages:
- build
- test
build:
image: node:14
stage: build
script:
- npm install
- npm run build
artifacts:
paths:
- dist/
test:
image: node:14
stage: test
script:
- npm install
- npm test
git add .gitlab-ci.yml
git commit -m "Add GitLab CI configuration"
git push
CI/CD pipelines will be visible in the Pipelines section of your GitLab repository.
Jenkins is an open-source automation server for CI/CD.
// Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
post {
success {
archiveArtifacts 'dist/**/*'
}
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
}
}
git add Jenkinsfile
git commit -m "Add Jenkins pipeline"
git push
Jenkins will automatically pick up the Jenkinsfile and start the pipeline.
Setting up CI/CD for TypeScript projects ensures that your code is continuously tested and deployed, leading to faster development cycles and more reliable software. By following the practices and techniques outlined in this chapter, you can establish a robust CI/CD pipeline for your TypeScript projects, enhancing both development efficiency and software quality. Happy coding !❤️