heroku部署的工作流程

This is my very first Medium article and I hope this place can become the collection of my experience and experiment. To make sure I can still explain those steps, let’s get started!

这是我的第一篇中型文章,我希望这个地方可以成为我的经验和实验的集合。 为了确保我仍然可以解释这些步骤,让我们开始吧!

Create the project folder. The path can be varied, depends on your preference. Once it’s created, change the directory to the root level of the folder.

创建项目文件夹。 路径可以变化,具体取决于您的偏好。 创建目录后,将目录更改为文件夹的根目录。

# mkdir ~/code/example/my_project
# cd ~/code/example/my_project

And then, create an empty Git repository in the folder. For more details, you can check the online document here.

然后,在文件夹中创建一个空的Git存储库。 有关更多详细信息,您可以在此处查看在线文档。

# my_project git init
Initialized empty Git repository in /Users/hugoshih/code/example/my_project/.git/

Once the repository is created. We can build a Github repo by using the following code.

创建存储库后。 我们可以使用以下代码构建一个Github存储库。

# my_project hub create

If you don’t have hub, you will need to install through brew. Here is for the method for different OS system. For Windows user, you will need to install a package manager, check the link for more information here.

如果没有集线器,则需要通过brew安装。 这是针对不同OS系统的方法。 对于Windows用户,您将需要安装软件包管理器,在此处查看链接以获取更多信息。

# Mac: brew install hub
# Ubuntu: sudo apt install hub
# Windows: choco install hub

You must follow the order from the above steps. If you don’t have an empty Git repository in the folder, you won’t be able to make Github repo. The error message below:

您必须遵循上述步骤中的顺序。 如果文件夹中没有空的Git存储库,则将无法创建Github存储库。 下面的错误信息:

# my_project hub create
'create' must be run from inside a git repository

If this is the first time you execute hub create, it will ask your Github username and password. And then it will give you an Github URL link. Meaning you can check your project on your Github repository page.

如果这是您第一次执行集线器创建,它将询问您的Github用户名和密码。 然后它将为您提供一个Github URL链接。 这意味着您可以在Github存储库页面上检查项目。

# my_project git:(master) hub create
Updating origin
https://github.com/username/my_project

The next step is also very important. We need to set up a virtual environment for each project. The reason is because each project comes with specific version of packages and libraries. If we mix all together, it will be hard to manage when we come to the deployment process, also it would be possible to break the app.

下一步也很重要。 我们需要为每个项目设置一个虚拟环境。 原因是因为每个项目都带有特定版本的软件包和库。 如果我们将所有内容混合在一起,则在进行部署过程时将很难进行管理,也有可能破坏应用程序。

1 project == 1 virtualenv

1个项目== 1个virtualenv

Here comes the easy steps to build a virtual environment.

这是构建虚拟环境的简单步骤。

I use pyenv-virtualenv, which is pyenv’s plugin, to manage my virtual environment. If you don’t have it, you can check the link for pyenv here or pyenv-virtualenv here. Here comes the example.

我使用pyenv的插件pyenv-virtualenv来管理我的虚拟环境。 如果你没有拥有它,你可以检查链接pyenv 这里或pyenv-的virtualenv 这里 。 这里是例子。

pyenv virtualenv my_project# create a new virtualenv for our project
pyenv virtualenvs # list all virtualenvs
pyenv activate my_project # enable our new virtualenv
pip install --upgrade pip # install and upgrade pip
pip list # list all installed packages

Reminder: When you open a new terminal, make sure you are at right virtual environment before installing any new packages or libraries.

提醒:打开新终端时,在安装任何新软件包或库之前,请确保您处于正确的虚拟环境中。

After finishing the above steps, you can start building your brand new project.

完成上述步骤后,您可以开始构建全新的项目。

After finishing the Streamlit project and now you want to deploy your python app to the Heroku. Here are the basic steps for you as reference.

完成Streamlit项目之后,现在您要将Python应用程序部署到Heroku。 以下是供您参考的基本步骤。

First, you need to have 5 important files in your project folder and make sure it locate at the root level of your folder. Here are the file list:

首先,您需要在项目文件夹中有5个重要文件,并确保它们位于文件夹的根目录下。 这是文件列表:

setup.sh
Procfile
requirements.txt
runtime.txt
.gitignore

For setup.sh, here is the code. You just need to replace the email to yours.

对于setup.sh ,这是代码。 您只需要替换发送给您的电子邮件即可。

mkdir -p ~/.streamlit/
echo "[general]email = \"hugo@postcolour.com\"
" > ~/.streamlit/credentials.toml
echo "[server]
headless = true
port = $PORT
enableCORS = false
" > ~/.streamlit/config.toml

For Procfile, this is the one line code.

对于Procfile ,这是一行代码。

web: sh setup.sh && streamlit run your_app_name.py

For requirements.txt file, here comes the best practice for the beginner.The reason we need to have this file is becasue when we deploy our app, it will need to install all required packages and libraries. And pip install can use text document to automatically intall the necessary libraries. However, when we build our app, most of time I forget what I installed. Therefore, we need a tool called pipreqs. Here is the link for more information here.

对于requirements.txt文件,这是初学者的最佳实践。部署我们的应用程序是因为需要此文件,原因是它需要安装所有必需的软件包和库。 pip install可以使用文本文档自动安装必要的库。 但是,在构建应用程序时,大多数时候我会忘记安装的内容。 因此,我们需要一个名为pipreqs的工具。 这里是链接了解更多信息点击这里

pip install pipreqs

Once you installed, you just need to make sure you are at the root level of your project folder and run the code below.

安装后,只需确保您位于项目文件夹的根目录下,然后运行下面的代码即可。

# my_project pipreqs .

It automatically create a requirement.txt file in your folder with all libraries and the specific version you used. Here is what it looks like.

它会自动在您的文件夹中创建一个require.txt文件,其中包含所有库和您使用的特定版本。 这是它的样子。

pandas==1.1.1
requests==2.24.0
awesome_streamlit==20200728.1
streamlit==0.66.0

For .gitignore, this will help you to avoid the file you don’t want to upload to your Github, like API and Raw data. There is a proper practice which I will share in the later article.

对于.gitignore ,这将帮助您避免不想上传到Github的文件,例如API和Raw数据。 我将在后面的文章中分享一种适当的做法。

For runtime.txt, this is the most important thing motivate me to write this article. This file will help us to trouble shoot the Heroku deployment. The key point is that you need to make sure what python version you use to build the app, so that when you deploy on Heroku and system can install all required libraries successfully.

对于runtime.txt ,这是促使我写这篇文章的最重要的事情。 该文件将帮助我们排除故障Heroku部署的麻烦。 关键是您需要确定用于构建应用程序的python版本,以便在Heroku上部署时系统可以成功安装所有必需的库。

# my_project python --version
# my_project touch runtime.txt
# my_project echo python-3.7.7 > runtime.txt

Once you have all files in the folder, then you can push to the Github.

文件夹中保存所有文件后,即可推送到Github。

# git status
# git add .
# git commit -m "Your message here"
# git push origin master

Once you push all files, the final is almost there.

推送所有文件后,最终文件就差不多了。

Since I already addressed all possible issue and solution on the top, you won’t have too much issue at this stage I guess.

由于我已经在顶部解决了所有可能的问题和解决方案,因此我认为您在现阶段不会有太多问题。

Here are the code for deploying on Heroku. * If you don’t have Heroku CLI, then you might need to install first. Information here.

这是在Heroku上部署的代码。 *如果没有Heroku CLI,则可能需要先安装。 这里的信息。

# my_project heroku login
# my_project heroku create

When you create the heroku app, it will give you 2 URLs.

创建heroku应用程序时,它将为您提供2个URL。

https://random-name-12345.herokuapp.com/ | https://git.heroku.com/random-name-12345.git

Heroku automatically create 2 random names for your app which I guess it’s for the security purpose. This link will also automatically added into your Git remote whcih you can use the below link to check.

Heroku自动为您的应用程序创建2个随机名称,我猜这是出于安全目的。 该链接也会自动添加到您的Git远程服务器中,您可以使用以下链接进行检查。

# my_project git remote -v
heroku https://git.heroku.com/random-name-12345.git (fetch)
heroku https://git.heroku.com/random-name-12345.git (push)
origin git@github.com:username/app_name.git (fetch)
origin git@github.com:username/app_name.git (push)

If you see these 4 git remote in your list, then you can push to heroku master.

如果您在列表中看到这4个git remote,则可以推送到heroku master。

# my_project git push heroku master

Like I said before if you have a runtime.txt file to address your python version. The install process should be okay. Otherwise, you would see something like the picture below:

就像我之前说过的,如果您有一个runtime.txt文件来解决您的python版本。 安装过程应该可以。 否则,您将看到如下图所示的内容:

Image for post

My python version is 3.7.7, but I don’t know why the system installed python-3.6.12 which has the compatible issue with the latest pandas and awesome-streamlit. If you need more official documents, please check here.

我的python版本是3.7.7,但是我不知道为什么系统安装了python-3.6.12,它与最新的熊猫和awesome-streamlit兼容。 如果您需要更多正式文件,请在此处查看

When the app is successfully deployed, you should see something like “done”.

成功部署应用程序后,您应该会看到类似“完成”的内容。

remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 140.4M
remote: -----> Launching...
remote: Released v3
remote: https://random-name-12345.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/random-name-12345.git
* [new branch] master -> master

And then run the last 2 codes to finish the deployment.

然后运行最后2个代码以完成部署。

# my_project heroku ps:scale web=1
# my_project heroku open

The last code will automatically open the browser and you can start sharing your app online!

最后一个代码将自动打开浏览器,您可以开始在线共享应用了!

Note: You can change your app on Heroku. However, you would also need to update your git remote link. Here are the code for solving this issue.

注意:您可以在Heroku上更改您的应用程序。 但是,您还需要更新git远程链接。 这是解决此问题的代码。

# git remote rm heroku
# heroku git:remote -a newname
# git remote -v

The last line is for you to check if everything looks right to you.

最后一行是供您检查是否一切都适合您。

翻译自: https://medium.com/dataroad/the-workflow-of-heroku-deployment-c1bbe34e0750

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值