天天看點

Heroku 常用指令行Command line如何檢查自己是否安裝成功?登入Heroku如何檢視自己的token?如何分叉應用?展示Heroku 伺服器的地區如何run console?檢視目前的add-ons如何建立一個PostgreSQL資料庫如何建立一個mongolab 資料庫建立一個scheduler

Heroku 常用指令行Command line如何檢查自己是否安裝成功?登入Heroku如何檢視自己的token?如何分叉應用?展示Heroku 伺服器的地區如何run console?檢視目前的add-ons如何建立一個PostgreSQL資料庫如何建立一個mongolab 資料庫建立一個scheduler

#安裝Heroku CLI

首先需要安裝Heroku CLI在本地中,安裝連接配接如下:

macOS Homebrew 安裝如下:

$ brew tap heroku/brew && brew install heroku
           

Ubuntu 16+ 安裝如下:

$ sudo snap install --classic heroku
           

或者:

$  curl https://cli-assets.heroku.com/install.sh | sh
           

當然,在snapcraft上也是可以https://snapcraft.io/ 也是可以下載下傳的。

snapcraft連結如下:

https://snapcraft.io/heroku

或者參考官方連結:https://devcenter.heroku.com/articles/heroku-cli#download-and-install

npm方法:

npm的方法需要提前安裝Node和npm

$ npm install -g heroku
           

Windows電腦

64位電腦安裝下載下傳:

https://cli-assets.heroku.com/heroku-x64.exe

32位電腦安裝下載下傳:

https://cli-assets.heroku.com/heroku-x86.exe

如何檢查自己是否安裝成功?

如下指令行檢視目前版本

heroku --version
           

如果發現自己版本不夠新,可以使用下面指令行更新

heroku update
           

登入Heroku

首先需要在本地登入,如下方法是會跳轉到網頁進行授權登入。

$ heroku login
           

有時候,我們在伺服器上登入,可以用下面的方法登入

$ heroku login -i
heroku: Enter your login credentials
Email: [email protected]
Password: ***************
Two-factor code: ********
Logged in as [email protected]
           

登入後,指令行會将您的電子郵件位址和API token 儲存到~/.netrc 中。

#如果登入有問題怎麼辦?

如果您在登入時遇到問題,請嘗試移動.netrc檔案。

mv ~/.netrc ~/.netrc.backup
heroku login
           

同時,登出指令行為heroku logout。

如何檢視自己的token?

$ heroku auth:token
c4cd94da15ea0544802c2cfd5ec4ead324327430
           

#如何建立一個項目?

直接輸入heroku create就會建立一個app

$  heroku create
 ›  Creating app... done, ⬢ fierce-sands-29965
https://fierce-sands-29965.herokuapp.com/ | https://git.heroku.com/fierce-sands-29965.git
           

但是該方法會建立一個随機名字的app,這裡是fierce-sands-29965,它的git位址為https://git.heroku.com/fierce-sands-29965.git。

如果我們需要建立一個名為“ example”的新應用,可以參考如下:

$ mkdir example
$ cd example
$ git init
$ heroku apps:create example
Creating ⬢ example... done
https://example.herokuapp.com/ | https://git.heroku.com/example.git
Git remote heroku added
           

如果你已經有Repository,可以 在新目錄或現有目錄中初始化git存儲庫

$ cd my-project/
$ git init
$ heroku git:remote -a my-project
這裡所謂的git:remote -a, 翻譯成git語言就是git remote add heroku my-project
           

#如何删除你的app?

假如這裡我要删除上午建立的app ‘fierce-sands-29965’,可以使用heroku apps:destroy指令行:

heroku apps:destroy fierce-sands-29965
 ›   ▸    WARNING: This will delete ⬢ fierce-sands-29965 including all add-ons.
 ▸    To proceed, type fierce-sands-29965 or re-run this command with --confirm fierce-sands-29965

> fierce-sands-29965
Destroying ⬢ fierce-sands-29965 (including all add-ons)... done
           

#如何複制你的Repository 到本地?

$ heroku git:clone -a test-app
$ cd test-app
           

test-app為你在Heroku上的app

#​如何部署你的應用?

​參考如下代碼:

$ git add .
$ git commit -am "make it better"
$ git push heroku master
           

這裡就是完全使用Git語言。

如何分叉應用?

有時候你可能需要fork 一個production出來,使用heroku fork複制現有的應用程式,包括附加元件,config vars和Heroku Postgres資料。

$ heroku fork --from sourceapp --to targetapp
Creating fork targetapp... done
Copying slug... done
Adding heroku-postgresql:dev... done
Creating database backup from sourcapp... .. done
Restoring database backup to targetapp... .. done
Copying config vars... done
Fork complete, view it at http://targetapp.herokuapp.com/
           

注意:原始應用程式稱為sourceapp,而新的分支應用程式稱為targetapp。

#如何檢視現有的app?

$ heroku apps
           

如何檢視自己收藏的應用(favorited apps)?

$ heroku apps:favorites
           

#如何檢視你的app資訊?

$ heroku apps:info your-app
           

這裡可以檢視你的app網站url和安裝了那些Addons,還有容量等資訊

=== sfdc-nodejs
Addons:         scheduler:standard
Auto Cert Mgmt: false
Dynos:          web: 1
Git URL:        https://git.heroku.com/sfdc-nodejs.git
Owner:          [email protected]
Region:         us
Repo Size:      5 MB
Slug Size:      27 MB
Stack:          heroku-16
Web URL:        https://sfdc-nodejs.herokuapp.com/
           

同時,有時候需要檢視你的Heroku配置變量。

$ heroku config -a sfdc-nodejs
           

那麼怎麼添加變量呢?這裡我們添加RAILS_ENV和RACK_ENV都為staging。

$ heroku config:set -a sfdc-nodejs RAILS_ENV=staging RACK_ENV=staging
           

sfdc-nodejs為你的app名字。

假如需要删除變量,

$ heroku config:unset -a pernod-ricard-sfdc-nodejs  RAILS_ENV
           

如何重新開機dyno?

$ heroku dyno:restart -a your-app
           

如何檢視日志Logs?

$ heroku logs -a your-app
           

檢視postgresql 資料庫資訊:

$ heroku pg -a your-app
           

這裡可以檢視有多少條資料和幾個表。

#如何對app進行重命名?

$ heroku apps:rename --app oldname newname
           

注意名字需要用小寫。同時,如果修改名字,所有本地Git URL都得修改。

展示Heroku 伺服器的地區

$ heroku regions
           

會顯示如下

ID         Location                 Runtime
─────────  ───────────────────────  ──────────────
eu         Europe                   Common Runtime
us         United States            Common Runtime
dublin     Dublin, Ireland          Private Spaces
frankfurt  Frankfurt, Germany       Private Spaces
oregon     Oregon, United States    Private Spaces
sydney     Sydney, Australia        Private Spaces
tokyo      Tokyo, Japan             Private Spaces
virginia   Virginia, United States  Private Spaces
           

如何run console?

假如在yourApp 下面有nodeFile.js,需要執行,可以執行如下代碼

$ heroku run -a yourApp node nodeFile.js
           

#檢視目前sessions

$ heroku sessions
           

檢視目前的add-ons

$ heroku addons
Owning App                 Add-on                          Plan                         Price  State  
─────────────────────────  ──────────────────────────────  ───────────────────────────  ─────  ───────
bitnews1                   mongolab-shaped-58019           mongolab:sandbox             free   created
bitnews1                   newrelic-slippery-34265         newrelic:wayne               free   created
bitnews1                   scheduler-dimensional-71609     scheduler:standard           free   created
           

如何建立一個PostgreSQL資料庫

$ heroku addons:create  heroku-postgresql:hobby-dev  -a yourApp
           

如何建立一個mongolab 資料庫

$ heroku addons:create  mongolab  -a yourApp
           

清單可以參考 :https://devcenter.heroku.com/categories/add-on-documentation

建立一個scheduler

$ heroku addons:create  scheduler:standard -a yourApp
           

本文系筆者在開發時候積累的常用指令行,并不是全部指令行,欲查閱全部指令行,請參考Heroku 開發者中心

參考文章:

  • https://devcenter.heroku.com/articles/heroku-cli
  • https://devcenter.heroku.com/articles/heroku-cli-commands

Heroku 常用指令行Command line如何檢查自己是否安裝成功?登入Heroku如何檢視自己的token?如何分叉應用?展示Heroku 伺服器的地區如何run console?檢視目前的add-ons如何建立一個PostgreSQL資料庫如何建立一個mongolab 資料庫建立一個scheduler

繼續閱讀