workflow-automation

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Workflow Automation

工作流自动化

When to use this skill

何时使用该技能

  • 반복 작업: 매번 같은 명령어 실행
  • 복잡한 빌드: 여러 단계 빌드 프로세스
  • 팀 온보딩: 일관된 개발 환경
  • 重复任务: 每次执行相同命令
  • 复杂构建: 多阶段构建流程
  • 团队入职: 一致的开发环境

Instructions

操作指南

Step 1: npm scripts

步骤1:npm脚本

package.json:
json
{
  "scripts": {
    "dev": "nodemon src/index.ts",
    "build": "tsc && vite build",
    "test": "jest --coverage",
    "test:watch": "jest --watch",
    "lint": "eslint src --ext .ts,.tsx",
    "lint:fix": "eslint src --ext .ts,.tsx --fix",
    "format": "prettier --write \"src/**/*.{ts,tsx,json}\"",
    "type-check": "tsc --noEmit",
    "pre-commit": "lint-staged",
    "prepare": "husky install",
    "clean": "rm -rf dist node_modules",
    "reset": "npm run clean && npm install",
    "docker:build": "docker build -t myapp .",
    "docker:run": "docker run -p 3000:3000 myapp"
  }
}
package.json:
json
{
  "scripts": {
    "dev": "nodemon src/index.ts",
    "build": "tsc && vite build",
    "test": "jest --coverage",
    "test:watch": "jest --watch",
    "lint": "eslint src --ext .ts,.tsx",
    "lint:fix": "eslint src --ext .ts,.tsx --fix",
    "format": "prettier --write \"src/**/*.{ts,tsx,json}\"",
    "type-check": "tsc --noEmit",
    "pre-commit": "lint-staged",
    "prepare": "husky install",
    "clean": "rm -rf dist node_modules",
    "reset": "npm run clean && npm install",
    "docker:build": "docker build -t myapp .",
    "docker:run": "docker run -p 3000:3000 myapp"
  }
}

Step 2: Makefile

步骤2:Makefile

Makefile:
makefile
.PHONY: help install dev build test clean docker

.DEFAULT_GOAL := help

help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

install: ## Install dependencies
	npm install

dev: ## Start development server
	npm run dev

build: ## Build for production
	npm run build

test: ## Run all tests
	npm test

lint: ## Run linter
	npm run lint

lint-fix: ## Fix linting issues
	npm run lint:fix

clean: ## Clean build artifacts
	rm -rf dist coverage

docker-build: ## Build Docker image
	docker build -t myapp:latest .

docker-run: ## Run Docker container
	docker run -d -p 3000:3000 --name myapp myapp:latest

deploy: build ## Deploy to production
	@echo "Deploying to production..."
	./scripts/deploy.sh production

ci: lint test build ## Run CI pipeline locally
	@echo "✅ CI pipeline passed!"
사용:
bash
make help        # Show all commands
make dev         # Start development
make ci          # Run full CI locally
Makefile:
makefile
.PHONY: help install dev build test clean docker

.DEFAULT_GOAL := help

help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

install: ## Install dependencies
	npm install

dev: ## Start development server
	npm run dev

build: ## Build for production
	npm run build

test: ## Run all tests
	npm test

lint: ## Run linter
	npm run lint

lint-fix: ## Fix linting issues
	npm run lint:fix

clean: ## Clean build artifacts
	rm -rf dist coverage

docker-build: ## Build Docker image
	docker build -t myapp:latest .

docker-run: ## Run Docker container
	docker run -d -p 3000:3000 --name myapp myapp:latest

deploy: build ## Deploy to production
	@echo "Deploying to production..."
	./scripts/deploy.sh production

ci: lint test build ## Run CI pipeline locally
	@echo "✅ CI pipeline passed!"
使用:
bash
make help        # 查看所有命令
make dev         # 启动开发服务
make ci          # 本地运行完整CI流程

Step 3: Husky + lint-staged (Git Hooks)

步骤3:Husky + lint-staged(Git钩子)

package.json:
json
{
  "lint-staged": {
    "*.{ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ],
    "*.{json,md}": [
      "prettier --write"
    ]
  }
}
.husky/pre-commit:
bash
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

echo "Running pre-commit checks..."
package.json:
json
{
  "lint-staged": {
    "*.{ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ],
    "*.{json,md}": [
      "prettier --write"
    ]
  }
}
.husky/pre-commit:
bash
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

echo "Running pre-commit checks..."

Lint staged files

Lint staged files

npx lint-staged
npx lint-staged

Type check

Type check

npm run type-check
npm run type-check

Run tests related to changed files

Run tests related to changed files

npm test -- --onlyChanged
echo "✅ Pre-commit checks passed!"
undefined
npm test -- --onlyChanged
echo "✅ Pre-commit checks passed!"
undefined

Step 4: Task Runner 스크립트

步骤4:任务运行器脚本

scripts/dev-setup.sh:
bash
#!/bin/bash
set -e

echo "🚀 Setting up development environment..."
scripts/dev-setup.sh:
bash
#!/bin/bash
set -e

echo "🚀 Setting up development environment..."

Check prerequisites

Check prerequisites

if ! command -v node &> /dev/null; then echo "❌ Node.js is not installed" exit 1 fi
if ! command -v docker &> /dev/null; then echo "❌ Docker is not installed" exit 1 fi
if ! command -v node &> /dev/null; then echo "❌ Node.js is not installed" exit 1 fi
if ! command -v docker &> /dev/null; then echo "❌ Docker is not installed" exit 1 fi

Install dependencies

Install dependencies

echo "📦 Installing dependencies..." npm install
echo "📦 Installing dependencies..." npm install

Copy environment file

Copy environment file

if [ ! -f .env ]; then echo "📄 Creating .env file..." cp .env.example .env echo "⚠️ Please update .env with your configuration" fi
if [ ! -f .env ]; then echo "📄 Creating .env file..." cp .env.example .env echo "⚠️ Please update .env with your configuration" fi

Start Docker services

Start Docker services

echo "🐳 Starting Docker services..." docker-compose up -d
echo "🐳 Starting Docker services..." docker-compose up -d

Wait for database

Wait for database

echo "⏳ Waiting for database..." ./scripts/wait-for-it.sh localhost:5432 --timeout=30
echo "⏳ Waiting for database..." ./scripts/wait-for-it.sh localhost:5432 --timeout=30

Run migrations

Run migrations

echo "🗄️ Running database migrations..." npm run migrate
echo "🗄️ Running database migrations..." npm run migrate

Seed data (optional)

Seed data (optional)

read -p "Seed database with sample data? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then npm run seed fi
echo "✅ Development environment ready!" echo "Run 'make dev' to start the development server"

**scripts/deploy.sh**:
```bash
#!/bin/bash
set -e

ENV=$1

if [ -z "$ENV" ]; then
    echo "Usage: ./deploy.sh [staging|production]"
    exit 1
fi

echo "🚀 Deploying to $ENV..."
read -p "Seed database with sample data? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then npm run seed fi
echo "✅ Development environment ready!" echo "Run 'make dev' to start the development server"

**scripts/deploy.sh**:
```bash
#!/bin/bash
set -e

ENV=$1

if [ -z "$ENV" ]; then
    echo "Usage: ./deploy.sh [staging|production]"
    exit 1
fi

echo "🚀 Deploying to $ENV..."

Build

Build

echo "📦 Building application..." npm run build
echo "📦 Building application..." npm run build

Run tests

Run tests

echo "🧪 Running tests..." npm test
echo "🧪 Running tests..." npm test

Deploy based on environment

Deploy based on environment

if [ "$ENV" == "production" ]; then echo "🌍 Deploying to production..." # Production deployment logic ssh production "cd /app && git pull && npm install && npm run build && pm2 restart all" elif [ "$ENV" == "staging" ]; then echo "🧪 Deploying to staging..." # Staging deployment logic ssh staging "cd /app && git pull && npm install && npm run build && pm2 restart all" fi
echo "✅ Deployment to $ENV completed!"
undefined
if [ "$ENV" == "production" ]; then echo "🌍 Deploying to production..." # Production deployment logic ssh production "cd /app && git pull && npm install && npm run build && pm2 restart all" elif [ "$ENV" == "staging" ]; then echo "🧪 Deploying to staging..." # Staging deployment logic ssh staging "cd /app && git pull && npm install && npm run build && pm2 restart all" fi
echo "✅ Deployment to $ENV completed!"
undefined

Step 5: GitHub Actions Workflow 자동화

步骤5:GitHub Actions工作流自动化

.github/workflows/ci.yml:
yaml
name: CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run linter
        run: npm run lint

      - name: Type check
        run: npm run type-check

      - name: Run tests
        run: npm test -- --coverage

      - name: Upload coverage
        uses: codecov/codecov-action@v3
.github/workflows/ci.yml:
yaml
name: CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run linter
        run: npm run lint

      - name: Type check
        run: npm run type-check

      - name: Run tests
        run: npm test -- --coverage

      - name: Upload coverage
        uses: codecov/codecov-action@v3

Output format

输出格式

project/
├── scripts/
│   ├── dev-setup.sh
│   ├── deploy.sh
│   ├── test.sh
│   └── cleanup.sh
├── Makefile
├── package.json
└── .husky/
    ├── pre-commit
    └── pre-push
project/
├── scripts/
│   ├── dev-setup.sh
│   ├── deploy.sh
│   ├── test.sh
│   └── cleanup.sh
├── Makefile
├── package.json
└── .husky/
    ├── pre-commit
    └── pre-push

Constraints

约束条件

필수 규칙 (MUST)

必须遵守的规则(MUST)

  1. 멱등성: 스크립트 여러 번 실행해도 안전
  2. 에러 처리: 실패 시 명확한 메시지
  3. 문서화: 스크립트 사용법 주석
  1. 幂等性: 多次执行脚本依然安全
  2. 错误处理: 失败时给出明确提示信息
  3. 文档化: 脚本需附带使用说明注释

금지 사항 (MUST NOT)

禁止事项(MUST NOT)

  1. 하드코딩된 비밀: 스크립트에 비밀번호, API 키 포함 금지
  2. 파괴적 명령: 확인 없이 rm -rf 실행 금지
  1. 硬编码机密信息: 禁止在脚本中包含密码、API密钥等机密内容
  2. 破坏性命令: 禁止在未确认的情况下执行rm -rf等破坏性命令

Best practices

最佳实践

  1. Make 사용: 플랫폼 무관 인터페이스
  2. Git Hooks: 자동 품질 검사
  3. CI/CD: GitHub Actions로 자동화
  1. 使用Make: 提供跨平台统一接口
  2. Git Hooks: 自动执行质量检查
  3. CI/CD: 通过GitHub Actions实现自动化

References

参考资料

Metadata

元数据

버전

版本

  • 현재 버전: 1.0.0
  • 최종 업데이트: 2025-01-01
  • 호환 플랫폼: Claude, ChatGPT, Gemini
  • 当前版本: 1.0.0
  • 最后更新: 2025-01-01
  • 兼容平台: Claude, ChatGPT, Gemini

태그

标签

#automation
#scripts
#workflow
#npm-scripts
#Makefile
#utilities
#automation
#scripts
#workflow
#npm-scripts
#Makefile
#utilities

Examples

示例

Example 1: Basic usage

示例1:基础用法

<!-- Add example content here -->
<!-- 在此添加示例内容 -->

Example 2: Advanced usage

示例2:进阶用法

<!-- Add advanced example content here -->
<!-- 在此添加进阶示例内容 -->