将 CircleCI 更改部署到 Heroku

Deploying CircleCI changes to Heroku

我对 Git 和自动部署还很陌生,我正在尝试将作为 CI 的一部分完成的更改部署到 Heroku。

高级想法:

  1. 将我的代码发送到 GitHub
  2. CircleCI 拾取它并做一些缩小
  3. CircleCI 运行一些测试
  4. CircleCI 将我的文件(包括我对它们所做的更改)部署到 Heroku

一切正常,除了我在 Heroku 上获得的文件似乎是来自 Git 的文件,而不是 modified/minified 文件。

我想问题出在我的 YAML 中:

... build steps

deploy:
  docker:
    - image: buildpack-deps:trusty
  steps:
    - checkout
    - run:
        name: Deploy to Heroku
        command: |
            git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git master

但是,我不太确定如何更改它。

完整的YAML作为参考:

version: 2
jobs:
build:
    docker:
    # https://circleci.com/docs/2.0/circleci-images/
    - image: circleci/node:10.10
    - image: circleci/postgres:10.5-alpine-postgis
        environment:
        POSTGRES_USER: myproject
        POSTGRES_DB: myproject

    working_directory: ~/repo

    steps:
    - checkout

    - restore_cache:
        keys:
        - v1-dependencies-{{ checksum "package.json" }}
        - v1-dependencies-

    - run: npm install

    ..........

    - save_cache:
        paths:
            - node_modules
        key: v1-dependencies-{{ checksum "package.json" }}

    - run:
        name: Unit Testing
        command: npm run test_unit

    - run:
        name: Build client files
        command: npm run build

    - run:
        name: API Testing
        command: |
            npm start &
            npm run test_api

deploy:
    docker:
    - image: buildpack-deps:trusty
    steps:
    - checkout
    - run:
        name: Deploy to Heroku
        command: |
            git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git master

workflows:
version: 2
build-deploy:
    jobs:
    - build
    - deploy:
        requires:
            - build
        filters:
            branches:
            only: master

您的 CI 工具不应为 Heroku 构建应用程序。 (当然,它可以构建它以进行 运行 测试。)

Heroku 将自行构建您的应用程序。推送您的 源文件 并让它做它的事情。使用 Node.js 构建包,您可以 Heroku will run your postinstall script, if provided,这是 运行 您的构建命令的好地方:

"scripts": {
  "start": "node index.js",
  "test": "mocha",
  "postinstall": "npm rum build"
}

运行 针对缩小文件(相对于未缩小文件)的测试可能会让您感觉更安全,但您实际上是在测试缩小工具以及您自己的代码。理想情况下,您应该使用已经过良好测试的工具,并将您自己的测试集中在您自己的代码上。 (如果你仍然喜欢 运行 反对缩小代码,那也没什么坏处。)

如果您想确保测试 运行 是针对您在 Heroku 上的代码的逐位精确副本(即只构建一次),请考虑构建和部署您的通过 Docker container.

申请

我最近写了一个使用 CirclCI、Heroku、Docker 和 FastAPI 设置 CI/CD 的教程。尽管您没有使用 FastAPI,但大部分过程都是相同的。

随时查看:production-ready-ci-cd-using-circleci-docker-heroku-fastapi