Circle CI

2020-04-15  本文已影响0人  梁某人的剑

概念

Jobs

任务是step的集合,单个任务必须指定一种executor

executor type:

executor type文档

version 2
jobs:
  build1: # job name
    docker: 
      - image: buildpack-deps:trusty
      - image: postgres:9.4.1
        environment: 
          POSTGRES_USER: root

steps

可执行命令的集合

steps:
  - checkout
  - run: 
      name: Running tests
      command: make test

cache

可以缓存诸如项目中源代码的依赖的文件或者目录。

version: 2
jobs:
  build1:
    docker: 
      - image: circleci/ruby:2.4-node
      - image: circleci/postgres:9.4.12-alpine
    steps:
      - checkout
      - save_cache: 
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - ~/circleci-demo-workflows

  build2:
    docker:
      - image: circleci/ruby:2.4-node
      - image: circleci/postgres:9.4.12-alpine
    steps:
      - restore_cache: # Restores the cached dependency.
          key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}

workflow

工作流定义了一系列的任务和他们的运行顺序。它可以使任务并行,穿行和按计划或者手动控制运行。工作流的作用:

平行任务
workflows:
  version: 2
  build:
    jobs:
      - "ruby-2.2"
      - "ruby-2.3"
      - "ruby-2.4"
顺序任务
workflows:
  version: 2
  build-test-and-deploy:
    jobs:
      - build
      - test:
          requires:
            - build
      - deploy:
          requires:
            - test
通过手动批准来控制工作流

工作流可以设置为需要手动批准再进行下一个任务。任何有权利访问代码库的人都可以继续工作流。为了做到这一点,在任务列表中添加type: approval

workflows:
  version: 2
  build-test-and-approval-deploy:
    jobs:
      - build
      - test: 
          requires:
            - build
      - deploy: 
          type: approval 
          requires: 
            - test
在夜晚执行的任务

默认工作流的触发依赖于每次git push,为了按照计划执行工作流,我们可以添加triggers键来特殊化工作调度。

下面的例子中,我们将运行一个旨在夜晚12点后运行的工作流。使用POSIX crontab语法指定cron键。crontab man page来查看基本语法。

workflows:
  version: 2
  commit:
    jobs:
      - test
      - deploy
  nightly:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only:
                - master
                - beta
    jobs:
      - coverage
使用任务上下文来分享环境变量

下面的例子展示了如何使用上下文来在一个工作流的四个平行任务中分享环境变量

workflows:
  version: 2
  build-test-and-deploy:
    jobs:
      - build
      - test1:
          requires:
            - build
          context: org-global
      - test2:
          requires:
            - test1
          context: org-global
      - deploy:
          requires:
            - test2
分支级别的任务执行

下面的例子会展示,如何在三个分支配置任务流。工作流会在忽略在任务底下的分支键,如果想在工作流中添加任务级别的分支,需要移除任务级别的分支,转而描述它

workflows:
  version: 2
  dev_stage_pre-prod:
    jobs:
      - test_dev:
          filters:  
            branches:
              only:
                - dev
                - /user-.*/
      - test_stage:
          filters:
            branches:
              only: stage
      - test_pre-prod:
          filters:
            branches:
              only: /pre-prod(?:-.+)?$/

CircleCI 镜像

最佳实践

注意: 如果没有固定标签,Docker将会使用latest标签。latest引用的是最有一个稳定版本的镜像。这样的镜像时十分不稳定的,所以最佳时间推荐使用稳定的镜像。

语言镜像变体

Circleci维持了一些语言变量的变体镜像。要使用这些变种将以下后缀之一添加到图像标记的末尾。

服务器镜像

服务镜像是数据库等服务的便利镜像。这些镜像应该在语言镜像之后列出,使之成为二级镜像。

服务镜像变体

使用RAM加速需要添加-ram后缀。举例,circleci/postgres:9.5-postgis => circleci/postgres:9.5-postgis-ram。

Obs

https://circleci.com/orbs/registry/

Deploy To GCP

在circle ci中使用git-crypt

- run:
    name: Install git-crypt
    command: >-
      sudo dpkg --add-architecture i386 &&
      sudo apt-get update &&
      sudo apt-get install g++ libssl-dev &&
      git clone --depth=1 https://github.com/AGWA/git-crypt &&
      cd git-crypt &&
      sudo make &&
      sudo make install
    - run:
    name: Decode git-crypt key
    command: echo "$GIT_CRYPT_KEY" | base64 -d > git-crypt.key
    
    - run:
    name: Decrpyt Secrets
    command: git-crypt unlock git-crypt.key
上一篇下一篇

猜你喜欢

热点阅读