简单CircleCI教程

Created by Qiwei on 10/25/2019 · Edited on 1/24/2021

选择Project

image

  1. 登陆CircleCI

  2. 左上角选择绑定的Github/Bitbucket账号

  3. “Add Project” 中可以看到选择账号中的所有项目,选择项目并点击Set Up Project按钮

image

   

配置config.yml

  1. 接下来要在我们的项目中添加circleci的配置文件(config.yml),首先在项目root directory中添加.circleci文件夹,然后在.circleci中添加config.yml。

  2. config.yml主要包含三部分,version,jobs,workflow

    • version:使用的 CircleCI 版本
    • jobs:定义需要执行的任务,里面的key是job的名称,value是执行 job 的步骤。
    • worflow:定义执行jobs的顺序和条件
  3. 具体教程和concepts

  4. 以下是我的网站使用的config,部署到AWS S3。如果大家要部署到AWS也需要在circleci上设置相关的environment variables。


version: 2.1

executors:
  node-executor:
    docker:
      - image: circleci/node:8.15

jobs:
  secrets_scan:
    executor: node-executor
    steps:
      - checkout
      - run:
         name: git-secrets scan
         command: |
           pushd /var/tmp
           git clone https://github.com/awslabs/git-secrets.git
           cd git-secrets
           sudo make install
           popd
           git secrets --register-aws --global
           git secrets --scan
  npm_audit:
    executor: node-executor
    steps:
      - checkout
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "my-page-app-client/package-lock.json" }}
          - v1-dependencies-
      - run: cd my-page-app-client && npm ci
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "my-page-app-client/package-lock.json" }}
      - run:
          name: audit
          command: |
            cd my-page-app-client
            echo npm audit
  npm_deploy:
    executor: node-executor
    parameters:
      environment:
        type: string
    environment:
      ENVIRONMENT: << parameters.environment >>
    steps:
      - checkout
      - run:
         name: deploy UI
         command: |
           cd my-page-app-client
           sudo apt -y update
           sudo apt install -y python-pip
           sudo pip install --upgrade awscli
           aws --version
           cat src/config.js
           npm ci
           npm run deploy

workflows:
  test:
    jobs:
      - secrets_scan:
          name: secrets_scan
      - npm_audit:
          name: npm_audit
          requires:
            - secrets_scan
      - npm_deploy:
          name: npm_deploy_dev
          environment: dev
          requires:
            - npm_audit