这是我参与11月更文挑战的第18天,活动详情查看:2021最后一次更文挑战
前端需要知道的CI / CD知识不完全指北(一)
初识 GitHubActions
本系列主要介绍 利用Git Action 实现 CI/CD 流程, 涉及到的知识点包括
CI/CDGithub actionsDocker Docker-compose, 实现使用Github action Docker / Docker compose 自动发布代码到测试机
CI/CD
持续集成 Continuous Integration (CI) 和 持续交付 Continuous Delivery (CD)
解决的问题 是保证研发流程, 让开发人员更关注于业务代码的开发
合理全面的 CI/CD, 自动化研发流程, 可以提高研发效率,增加系统稳定性
Github Actions
网上的CI /CD 有很多, 例如 travis , 任选一个即可。
Github Actions 高效稳定, 功能强大, 易学易用
目标: 使用 Github actions 自动化构建和测试
什么是 Github Actions ?
Github 2019年秋天发布的 CI/CD工具, 功能强大且稳定
介绍
# 选择 Publish Nodejs Package
npm ci
npm test
npm ci
# 选择 Node.js
npm ci
npm run build --if-present
npm test
# 最终 代码在 .github/workflows 目录下 .yml 格式文件
中文文档 docs.github.com/cn/actions/…
典型的github actions 文件
name: GitHub Actions Demo
on: [push] # 触发条件 on push
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v2
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
应用场景
# `自动化测试` `自动部署` `tag自动上线(支持回滚) `
# .github/workflows/deploy-dev.yml
# dev 分支 ,自动部署到测试机
# .github/workflows/deploy-prd.yml
# master分支, 自动化测试, v*.*.*格式的tag,自动上线(支持回滚)
# .github/workflows/test.yml #
对应代码
# /.github/workflows/demo.yml
# 自动化测试
name: demo
on: # 触发条件
push: # 项目 git push 的时候触发
branches: [ main ] # master 分支 push的时候触发
paths: # [触发条件] push的文件在这个范围之内触发
- '.github/workflows/**'
- '__test__/**'
- 'src/**'
pull_request:
branches: [ main ]
jobs: # 任务
test: # 任务1 test
runs-on: ubuntu-latest # 在 ubuntu系统的基础上运行
steps: # 步骤 steps, 可自定义也可以使用第三方
# Step 1 直接使用 uses 第三方,
- uses: actions/checkout@v2 # 意思是 git pull 获取当前最新代码
# Step 2 使用 name 和 uses 第三方
- name: Use Node.js # name: step 名称
uses: actions/setup-node@v1 # 第三方 actions , 安装nodejs并指定版本号
with:
node-version: 14
# Step 3 使用 name
- name: print node version
run: | # RUN 自定义的命令
node -v
npm -v
test2: # 任务2 test2
runs-on: ubuntu-latest
steps:
- run: touch a.txt
- run: echo 100 > a.txt
- run: cat a.txt
git commit -am"modify demo.yml"
git push # 触发 github actions
Step 的四种形式
# 直接使用 uses 第三方
# 使用 name 和 uses 第三方
# 使用 name 和 run
# 直接使用 run
扩展 28原则
20% 的功能 可以 满足 80% 的需求
20% 的功能 可能 花费 80% 的成本
思考投入产出比