markdown Travis和Heroku CICD简而言之

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Travis和Heroku CICD简而言之相关的知识,希望对你有一定的参考价值。

# Setup Travis and Heroku CICD: Point-n-Click

## Continuous Integration with Travis CI

### Configure travis.yml

- Run `npm test` to ensure tests are working correctly locally
- Create `.travis.yml` file in repo
- Add the following to configure for `node`

```yaml
language: node_js
node_js: node
```

- Commit your changes, but don't push to GitHub yet.

### Activate Github <=> Travis integration on your repo

Activate - On Travis:

- Go to Profile: User (in upper-right) > Accounts
- Click "Sync Account"
- Activate repo

### Push to trigger Git Hook

- Push your changes to Github to trigger the git hook
- Watch Travis the build and test

## Continuous Deployment to Heroku

Configure Heroku Pipeline on Heroku

- Go to: Personal Apps > PROJECT NAME > Deploy
- Under "Deployment method" choose GitHub
- Under "Connect to GitHub" choose the account and repo to deploy
- Under "Automatic deploys" choose the branch to deploy from
- **Important** - Check "Wait for CI to pass before deploy" checkbox
- Click "Enable Automatic Deploys"
- Commit and push to trigger the process

## Extras

Add a Travis CI badge to your repo

- On Travis CI, find your project and click on the badge
- Change the dropdown menu to "Markdown" and copy the output
- Add the badge code to your `readme.md` file, commit and push
- The code looks something like this:

```markdown
[![Build Status](https://travis-ci.org/<USERNAME>/<REPO-NAME>.svg?branch=master)](https://travis-ci.org/<USERNAME>/<REPO-NAME>)
```
# Setup Travis and Heroku CICD: CLI Ninja

These are the command line only instructions

> “For those who like that sort of thing, this is the sort of thing they like.”

## Install Travis and Heroku CLI 

Ensure Travis and Heroku command line clients installed

- `travis --version`
- `heroku --version`  

If they are not installed, then install using the following directions:

- [Install Travis CI's CLI](https://github.com/travis-ci/travis.rb#installation)
- [Install Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli#download-and-install)


## Continuous Integration with Travis CI

Navigate to the project directory and checkout the correct branch

```sh
cd ~/Projects/my-project
git checkout master
```

Login to Travis (w/ GitHub Credentials)

```sh
travis login
```

Enable Travis CI to GitHub integration

```sh
travis enable
```

Generate a `.travis.yml` for Node

```sh
travis init node --node-js node
```

> Note: if setting up a database, please see [database config](#file-cicd-database-config-md) below

Add and commit `.travis.yml` then push to `master`

```sh
git add -A
git commit -m 'add .travis.yml'
git push origin master
```

View Logs (may need to run multiple times)

```sh
travis logs
```


## Continuous Deployment to Heroku

Login to Heroku

```sh
heroku login
```

Create an app on Heroku

```sh
heroku create [APP-NAME]
```

Configure `.travis.yml` with Heroku deployment info

```sh
travis setup heroku
```

Commit and push

```sh
git commit -am 'setup heroku'
git push origin master
```

View Logs (may need to run multiple times)

```sh
travis logs
```
# CICD Configure Mongo or Postgres Database

### Configure travis.yml

- **Node with no database**:

```yaml
language: node_js
node_js: node
```

- **Node with MongoDB**

```yaml
language: node_js
node_js: node
services: mongodb
```

- **Node with PostgreSQL**
- Travis needs a database and tables. The following assumes the `.sql` script contains the `CREATE TABLE` commands

```yaml
language: node_js
node_js: node
services:
  - postgresql
before_script:
  - psql -U postgres -c 'CREATE DATABASE "[YOUR-TEST-DATABASE]";'
  - psql -U postgres -f ./db/create-tables.sql -d [YOUR-TEST-DATABASE]
```

## Configure Environment Variables on Heroku

### On Heroku

- Add connection string to (Environment) Config Vars
  - Go to: Personal Apps > PROJECT NAME > Settings
  - Click "Reveal Config Vars"
  - If using Heroku Add-On for Mongo or Postgres then:
    - Browse to your Heroku App.
    - Click "Resources"
    - In the Add-ons input: type `postgres` or `mlab`
      - Select: the free plan and click `Provision`
      - Heroku will create "Config Vars" for the database
        - Postgres: `DATABASE_URL`
        - Mongo: `MONGODB_URI`
  - If using stand-alone ElephantSQL or MLab then:
    - Browse to your Heroku App.
      - Click "Settings"
      - Click "Reveal Config Vars"
      - Enter appropriate var for database
        - `DATABASE_URL` = `postgres://<UN>:<PW>@stampy.db.elephantsql.com:<PORT>/<DB>`
        - `MONGODB_URI` = `mongodb://<UN>:<PW>@<SEREVER>.mlab.com:<PORT>/<DB>`
## Troubleshooting

### Problem: Running `travis setup heroku` returns the wrong repo name.

Example:

```sh
travis setup heroku
Deploy only from username/OLD-REPO-NAME? |yes|
Encrypt API key? |yes|
```

Reason: Travis CLI adds a "slug" to the `.git/config`. The slug may be outdated if you have renamed the repo.

```txt
[travis]
  slug = username/OLD-REPO-NAME
 ```

Solution: Remove the slug from `.git/config` and rerun

* run `git config --unset travis.slug` to remove the slug
* rerun `travis setup heroku`
Alternatively, you can run `travis setup heroku --store-repo [REPO-NAME]` to store a new slug

以上是关于markdown Travis和Heroku CICD简而言之的主要内容,如果未能解决你的问题,请参考以下文章

markdown 如何在15分钟内从Travis CI设置自动上传到Anaconda

在 travis-ci 上使用秘密 api 密钥

markdown 关键Heroku功能和CI / CD概述

travis.ci 上的 Android 构建变体

Travis.ci 配置和执行 npm 脚本

Travis CI电子邮件通知主题