Py学习  »  Git

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

万有_青年 • 5 年前 • 284 次点击  
阅读 55

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

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

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

如果你太忙,不能阅读官方文档。 本教程对您作为快速备忘,非常有帮助。

1. 注册 CircleCI

打开 CircleCI 官方网站,使用您的GitHub帐户登录。

2. 启动存储库

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

3. 编写 config.yml

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

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

如果要指定触发 ci 任务的某个分支,可以使用过滤器:

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 跑。

参考


今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/x7XF3GYZGF
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/28034
 
284 次点击