news 2026/5/21 4:14:28

CI/CD流水线:使用GitHub Actions自动化部署

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
CI/CD流水线:使用GitHub Actions自动化部署

CI/CD流水线:使用GitHub Actions自动化部署

大家好,我是欧阳瑞(Rich Own)。今天想和大家聊聊CI/CD这个话题。作为一个全栈开发者,我深知自动化部署的重要性。一个好的CI/CD流水线可以大大提高开发效率,减少人为错误。今天就来分享一下如何使用GitHub Actions构建CI/CD流水线。

什么是CI/CD?

CI/CD代表持续集成(Continuous Integration)和持续部署(Continuous Deployment)。

阶段描述
持续集成代码提交后自动构建和测试
持续交付自动部署到测试环境
持续部署自动部署到生产环境

为什么需要CI/CD?

  • 提高效率:自动化重复任务
  • 减少错误:避免人为失误
  • 快速反馈:立即发现问题
  • 持续改进:频繁发布新功能

GitHub Actions基础

什么是GitHub Actions?

GitHub Actions是GitHub提供的CI/CD服务,可以自动化软件开发工作流程。

核心概念

概念描述
Workflow自动化流程的定义
Job一组步骤的集合
Step单个命令或动作
Action可重用的步骤
Runner执行工作流的服务器

创建第一个Workflow

# .github/workflows/hello-world.yml name: Hello World on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run a one-line script run: echo "Hello, World!" - name: Run a multi-line script run: | echo "This is a multi-line script" echo "It can have multiple commands"

Node.js项目CI/CD

项目结构

my-node-app/ ├── src/ │ └── index.js ├── package.json └── .github/ └── workflows/ └── ci-cd.yml

Workflow配置

# .github/workflows/ci-cd.yml name: Node.js CI/CD on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [16.x, 18.x] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} cache: 'npm' - name: Install dependencies run: npm ci - name: Run tests run: npm test - name: Build run: npm run build deploy: needs: build runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - uses: actions/checkout@v3 - name: Deploy to production uses: some-deployment-action@v1 with: target: production api-key: ${{ secrets.DEPLOY_API_KEY }}

React项目CI/CD

构建和部署到Vercel

name: React CI/CD on: push: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18.x' cache: 'npm' - name: Install dependencies run: npm ci - name: Run tests run: npm test - name: Build run: npm run build env: CI: false deploy: needs: build runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Deploy to Vercel uses: amondnet/vercel-action@v20 with: vercel-token: ${{ secrets.VERCEL_TOKEN }} vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} vercel-args: '--prod'

Docker项目CI/CD

构建Docker镜像

name: Docker CI/CD on: push: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Login to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: Build and push uses: docker/build-push-action@v4 with: context: . push: true tags: ${{ secrets.DOCKER_USERNAME }}/my-app:latest

Kubernetes部署

部署到K8s集群

name: Kubernetes Deployment on: push: branches: [ main ] jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Kubectl uses: azure/setup-kubectl@v3 with: version: 'latest' - name: Authenticate to cluster uses: google-github-actions/get-gke-credentials@v2 with: cluster_name: ${{ secrets.GKE_CLUSTER_NAME }} project_id: ${{ secrets.GCP_PROJECT_ID }} location: ${{ secrets.GKE_REGION }} - name: Deploy to Kubernetes run: | kubectl set image deployment/my-app my-app=${{ secrets.DOCKER_USERNAME }}/my-app:${{ github.sha }} kubectl rollout status deployment/my-app

智能合约CI/CD

编译和测试Solidity合约

name: Solidity CI/CD on: push: branches: [ main ] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Install Foundry uses: foundry-rs/foundry-toolchain@v1 with: version: nightly - name: Run tests run: forge test -vvv - name: Run coverage run: forge coverage deploy: needs: test runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - uses: actions/checkout@v3 - name: Install Foundry uses: foundry-rs/foundry-toolchain@v1 with: version: nightly - name: Deploy to Goerli run: forge create --rpc-url ${{ secrets.GOERLI_RPC_URL }} --private-key ${{ secrets.PRIVATE_KEY }} src/MyContract.sol:MyContract

多环境部署

name: Multi-environment Deployment on: push: branches: - main - staging jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build run: npm run build deploy-staging: needs: build runs-on: ubuntu-latest if: github.ref == 'refs/heads/staging' steps: - name: Deploy to staging run: echo "Deploying to staging..." deploy-production: needs: build runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - name: Deploy to production uses: chrnorm/deployment-action@releases/v1 with: token: ${{ github.token }} target_url: https://example.com environment: production

自动化测试和质量检查

name: Quality Check on: [push, pull_request] jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run ESLint run: npx eslint . typecheck: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run TypeScript check run: npx tsc --noEmit audit: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run npm audit run: npm audit --audit-level=critical

自定义Action

创建JavaScript Action

// action/index.js const core = require('@actions/core'); const github = require('@actions/github'); async function run() { try { const name = core.getInput('name'); console.log(`Hello, ${name}!`); const octokit = github.getOctokit(core.getInput('github-token')); const { data } = await octokit.rest.repos.get({ owner: github.context.repo.owner, repo: github.context.repo.repo }); console.log(`Repository: ${data.full_name}`); } catch (error) { core.setFailed(error.message); } } run();
# action/action.yml name: 'Hello World' description: 'Greet someone' inputs: name: description: 'Name to greet' required: true default: 'World' github-token: description: 'GitHub token' required: true runs: using: 'node16' main: 'index.js'

最佳实践

1. 使用缓存

- name: Cache dependencies uses: actions/cache@v3 with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }} restore-keys: | ${{ runner.os }}-node-

2. 并行作业

jobs: test-linux: runs-on: ubuntu-latest steps: [...] test-windows: runs-on: windows-latest steps: [...] test-macos: runs-on: macos-latest steps: [...]

3. 条件执行

steps: - name: Deploy if: github.event_name == 'push' && github.ref == 'refs/heads/main' run: npm run deploy

4. 环境变量和密钥

steps: - name: Run script env: API_KEY: ${{ secrets.API_KEY }} NODE_ENV: production run: node script.js

总结

GitHub Actions是一个功能强大的CI/CD工具,可以帮助你自动化软件开发流程。从简单的测试到复杂的多环境部署,GitHub Actions都能胜任。

我的鬃狮蜥Hash对CI/CD也有自己的理解——它总是按照固定的流程:晒太阳→吃蟋蟀→睡觉,形成了一个完美的"持续生活"循环。这和我们做CI/CD的道理是一样的。

如果你有CI/CD方面的问题,欢迎留言交流!我是欧阳瑞,极客之路,永无止境!


技术栈:GitHub Actions · CI/CD · Docker · Kubernetes · Vercel

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/18 11:40:30

番茄小说下载器:三步构建个人离线小说库的终极指南

番茄小说下载器:三步构建个人离线小说库的终极指南 【免费下载链接】fanqienovel-downloader 下载番茄小说 项目地址: https://gitcode.com/gh_mirrors/fa/fanqienovel-downloader 你是否曾为网络小说突然下架而痛心?是否在通勤路上因信号不佳无法…

作者头像 李华
网站建设 2026/5/18 11:40:22

穆斯堡尔谱(Mössbauer spectroscopy)原理简介、应用实例文献解读

什么是穆斯堡尔谱?介绍:在固体里,某些核(最经典是 57Fe)可以无反冲地吸收/发射能量极其精确的γ射线;当样品里核的能级因为周围化学或磁环境略微变化时,吸收能量也会微微偏移,我们就能从谱线的…

作者头像 李华
网站建设 2026/5/18 11:39:25

进化智能体架构解析:从遗传算法到深度强化学习的AI系统设计

1. 项目概述:从蓝图到智能体,一次开源协作的深度实践最近在开源社区里,一个名为planck-lab/hermes-evolving-agents-public-blueprint的项目引起了我的注意。乍一看这个标题,信息量不小:“planck-lab”像是一个研究机构…

作者头像 李华
网站建设 2026/5/18 11:37:22

通过curl命令快速测试Taotoken各大模型API的连通性与响应

🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 通过curl命令快速测试Taotoken各大模型API的连通性与响应 在接入大模型服务时,直接使用curl命令进行测试是一种高效、轻…

作者头像 李华
网站建设 2026/5/18 11:36:22

从账单明细看Taotoken按Token计费模式的透明与灵活

🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 从账单明细看Taotoken按Token计费模式的透明与灵活 1. 理解按Token计费的核心价值 在大模型应用开发与日常使用中,成本…

作者头像 李华