# TypeScript Development with CI/CD for Server Deployment
## Introduction
This comprehensive guide focuses on developing a TypeScript project with emphasis on preserving its rich expressiveness, implementing CI/CD practices, and deploying to a standalone run environment on a server. We'll cover the entire workflow from local development to automated deployment.
# Building a CI/CD Pipeline for TypeScript Project
## Introduction
This guide focuses on creating a comprehensive Continuous Integration and Continuous Deployment (CI/CD) pipeline for your TypeScript project. We'll use GitHub Actions to automate the build, test, and deployment processes, ensuring that your TypeScript code is consistently built, tested, and deployed to your server.
## Prerequisites
- A GitHub repository containing your TypeScript project
- A server for deployment (e.g., AWS EC2, DigitalOcean Droplet)
- Basic familiarity with Git and GitHub
## Step 1: Prepare Your TypeScript Project
Ensure your project has the following:
1. A `package.json` file with build and test scripts:
```json
{
"scripts": {
"build": "tsc",
"test": "jest",
"start": "node dist/index.js"
}
}
```
2. A `tsconfig.json` file for TypeScript compilation settings.
3. Jest configuration for testing (if applicable).
## Step 2: Set Up GitHub Actions
1. In your repository, create a new file: `.github/workflows/ci-cd.yml`
2. Add the following content to `ci-cd.yml`:
```yaml
name: CI/CD Pipeline
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
- name: Test
run: npm test
deploy:
needs: build-and-test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v2
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /path/to/your/project
git pull origin main
npm install
npm run build
pm2 restart your-app-name
```
## Step 3: Configure GitHub Secrets
In your GitHub repository:
1. Go to Settings > Secrets
2. Add the following secrets:
- `SERVER_HOST`: Your server's IP address or domain
- `SERVER_USERNAME`: SSH username for your server
- `SERVER_SSH_KEY`: Your SSH private key for server access
## Step 4: Prepare Your Server
1. Install Node.js, npm, and PM2 on your server.
2. Set up your project on the server:
```bash
cd /path/to/your/project
git clone https://github.com/your-username/your-repo.git .
npm install
npm run build
pm2 start dist/index.js --name your-app-name
```
## Step 5: Implement Continuous Integration
The `build-and-test` job in your workflow handles CI:
1. It runs on every push and pull request to the `main` branch.
2. Checks out the code, sets up Node.js, installs dependencies.
3. Builds the TypeScript project.
4. Runs tests.
This ensures that every change is built and tested before it can be merged.
## Step 6: Implement Continuous Deployment
The `deploy` job handles CD:
1. It only runs on pushes to the `main` branch, after the `build-and-test` job succeeds.
2. Uses SSH to connect to your server.
3. Pulls the latest changes, installs dependencies, builds the project, and restarts the application.
This automates the deployment process whenever changes are pushed to the main branch.
## Step 7: Monitor and Iterate
1. Check the "Actions" tab in your GitHub repository to monitor your pipeline.
2. Review logs for any failed jobs and address issues promptly.
3. Continuously refine your pipeline:
- Add code linting steps
- Implement code coverage checks
- Add security scanning tools
## Best Practices
1. **Branch Protection**: Set up branch protection rules for your `main` branch to require status checks to pass before merging.
2. **Environment Specific Deployments**: For more complex setups, use GitHub Environments to manage different deployment targets (staging, production).
3. **Caching**: Implement caching of npm packages in your GitHub Actions workflow to speed up builds.
4. **Notifications**: Set up notifications for pipeline failures to alert your team quickly.
5. **Artifact Storage**: Store build artifacts for debugging and rollback purposes.
## Conclusion