[译] 如何使用 CircleCI for GitHub Pages 持续部署

今天我将介绍如何在GitHub页面上使用 CircleCI 进行持续部署。node

CircleCI 是一个很像 Travis CI 的CI工具。 但他们的配置有不少不一样之处。 你可能会发现, 首先使用它很麻烦。git

若是你太忙,不能阅读官方文档。 本教程对您做为快速备忘,很是有帮助。github

1. 注册 CircleCI

打开 CircleCI 官方网站,使用您的GitHub账户登陆。docker

2. 启动存储库

检查要在 CircleCI 上管理的存储库的开关按钮。shell

3. 编写 config.yml

在项目根目录或 .circleci 目录中为 CircleCI 建立名为 config.yml 的配置文件 首先,您须要设置构建环境,这取决于您的项目语言和依赖项:bash

version: 2
jobs:
  build:
    docker:
      - image: circleci/node:latest
复制代码

若是要指定触发 ci 任务的某个分支,可使用过滤器:ssh

filters:
  branches:
    only: master
复制代码

而后,您能够配置要在虚拟机上运行的命令,命令能够按步骤划分:工具

steps:
  - run:
    name: Install some stuff
    command: <do-some-stuff>
  - run:
    name: Deploy if tests pass and branch is Master
    command: <my-deploy-commands>
复制代码

我正在使用 Gatsby 来构建个人 doc 站点。这是一个完整的模板:测试

version: 2
jobs:
  build:
    docker:
      - image: circleci/node:latest
    filters:
      branches:
        only: master
    steps:
      - add_ssh_keys:
          fingerprints:
            - "xx:xx:xx:xx:11:22:33:44:55:66:77:88:99:xx:xx:xx"
      - checkout
      - restore_cache:
          keys:
          - dependencies-
          # fallback to using the latest cache if no exact match is found
          - dependencies-
      - run:
          name: Install
          command: yarn install
      - save_cache:
          paths:
            - node_modules
          key: dependencies-
      - run:
          name: Gatsby build site
          command: yarn build
      - run:
          name: Prepare shell commands
          command: cp .scripts/gatsby-deploy.sh ../ && chmod +x ../gatsby-deploy.sh
      - run:
          name: Run deploy scripts
          command: ../gatsby-deploy.sh
复制代码

4. 写入 CircleCI 的权限

对我来讲,我必须受权 CircleCI 自动更新 gh 页面 。在得到读取权限以前生成的默认 ssh 密钥。因此咱们必须手动添加读/写部署密钥。网站

生成一个新的ssh密钥

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
复制代码

按照命令行交互,您将得到两个 ssh 密钥文件id_rsa和id_rsa.pub(记得更改默认文件位置或您的本地ssh密钥将被覆盖)。

上传ssh密钥

1.经过https://github.com/<your_name>/<your_repo>/settings/keys上传您的GitHub repo设置上的id_rsa.pub。

2.跳转到https://circleci.com/gh/<your_name>/<your_repo>/edit#ssh并添加您刚刚建立的私钥id_rsa。

  在主机名字段中输入github.com,而后按 提交按钮。并添加您刚刚建立的私钥id_rsa。 在主机名字段中输 入 github.com, 而后按提交按钮。

将ssh密钥添加到配置文件中

使用add_ssh_keys设置刚刚添加的ssh密钥,以便在运行部署脚本时启用它。

- add_ssh_keys:
    fingerprints:
      - "xx:xx:xx:xx:11:22:33:44:55:66:77:88:99:xx:xx:xx"
复制代码

5. 编写 deploy.sh shell 脚本

如今 CircleCI 得到了写入您的存储库的权限,您可使用任何 git 命令来操做您的存储库:

git pull
yarn build
git checkout gh-pages
# Add site files...
git push
复制代码

6. 开始测试并享受它

就这样。你如今很高兴。拿起一杯咖啡坐下来观看 CircleCI 跑。

参考

相关文章
相关标签/搜索