TypeScript Continuous Integration and Deployment (CI/CD)

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.

Understanding CI/CD

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.

Setting Up a TypeScript Project for CI/CD

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.

Basic Setup:

Initialize a TypeScript Project:

				
					mkdir my-typescript-project
cd my-typescript-project
npm init -y
npm install typescript --save-dev
npx tsc --init

				
			

Project Structure:

				
					my-typescript-project/
├── src/
│   ├── index.ts
├── tests/
│   ├── index.test.ts
├── tsconfig.json
├── package.json
├── .gitignore
├── .eslintrc.json
├── jest.config.js

				
			

Add TypeScript Configuration:

				
					// tsconfig.json
{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

				
			

Continuous Integration with TypeScript

Version Control Integration

Git Integration:

Initialize Git

				
					git init
git add .
git commit -m "Initial commit"

				
			

Push to a Repository:

				
					git remote add origin <your-repository-url>
git push -u origin main

				
			

Automated Testing

Automated testing ensures that code changes do not break the existing functionality.

Setup Jest for Testing:

Install Jest and TypeScript Jest Preset:

				
					npm install jest ts-jest @types/jest --save-dev

				
			

Configure Jest:

				
					// jest.config.js
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testMatch: ['**/tests/**/*.test.ts']
};

				
			

Example Test:

				
					// 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);
});

				
			

Run Tests:

				
					npx jest

				
			

Output:

				
					PASS  tests/index.test.ts
✓ adds 1 + 2 to equal 3 (5 ms)

				
			

Linting and Formatting

Linting helps maintain code quality by enforcing coding standards.

Setup ESLint:

Install ESLint and TypeScript Plugin:

				
					npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev

				
			

Configure ESLint:

				
					// .eslintrc.json
{
  "parser": "@typescript-eslint/parser",
  "extends": [
    "plugin:@typescript-eslint/recommended"
  ],
  "rules": {
    "semi": ["error", "always"],
    "quotes": ["error", "single"]
  }
}

				
			

Run ESLint:

				
					npx eslint src/**/*.ts

				
			

Continuous Deployment with TypeScript

Building the Project

Building the project is the process of compiling TypeScript to JavaScript.

Build Script:

				
					// package.json
{
  "scripts": {
    "build": "tsc"
  }
}

				
			

Build Command:

				
					npm run build

				
			

Deployment Strategies

Deployment strategies include direct deployment, blue-green deployment, and canary deployment.

Direct Deployment:

Upload Build Artifacts:

				
					scp -r dist/* user@server:/path/to/deploy

				
			

Restart Application:

				
					ssh user@server 'pm2 restart my-app'

				
			

CI/CD Tools and Services

GitHub Actions

GitHub Actions is a powerful CI/CD tool integrated with GitHub.

GitHub Actions Configuration:

Create Workflow File:

				
					// .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

				
			

Push Workflow File:

				
					git add .github/workflows/ci.yml
git commit -m "Add GitHub Actions workflow"
git push

				
			

Output

				
					You can see the CI results in the Actions tab of your GitHub repository.

				
			

GitLab CI

GitLab CI is another popular CI/CD tool that is integrated with GitLab.

GitLab CI Configuration:

Create .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

				
			

Push Configuration:

				
					git add .gitlab-ci.yml
git commit -m "Add GitLab CI configuration"
git push

				
			

Output:

				
					CI/CD pipelines will be visible in the Pipelines section of your GitLab repository.

				
			

Jenkins

Jenkins is an open-source automation server for CI/CD.

Jenkins Configuration:

Create Jenkins Pipeline:

				
					// 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'
      }
    }
  }
}

				
			

Push Jenkinsfile:

				
					git add Jenkinsfile
git commit -m "Add Jenkins pipeline"
git push

				
			

Output:

				
					Jenkins will automatically pick up the Jenkinsfile and start the pipeline.

				
			

Best Practices for CI/CD

  • Automate Everything: Automate as much of the CI/CD pipeline as possible.
  • Test Early and Often: Run tests on every commit to catch issues early.
  • Keep Pipelines Fast: Optimize the pipeline to run quickly.
  • Secure Your Pipelines: Ensure that the CI/CD pipeline is secure.
  • Monitor and Improve: Continuously monitor and improve the CI/CD process.

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 !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India