ci/cd heroku_在GitLab上设置CI / CD以在Heroku上部署Python Flask应用程序

ci/cd heroku

Recently I came across a challenge to deploy a Python Flask web application to Heroku. The code of the App was hosted in GitLab.

最近,我遇到了将Python Flask Web应用程序部署到Heroku的挑战。 该应用程序的代码托管在GitLab中。

Heroku supports deploying an App from GitHub, Dropbox along with the usual Heroku git. It had been quite a while since I used Heroku. I was wondering if I can deploy code directly from my GitLab repository instead of using any of the sources mentioned above.

Heroku支持从GitHub,Dropbox和常规的Heroku git部署应用程序。 自从我使用Heroku以来已经有一段时间了。 我想知道是否可以直接从我的GitLab存储库中部署代码,而不使用上面提到的任何源代码。

I couldn’t find any information or documentation around deploying applications hosted in a GitLab repository to Heroku. I browsed a bit on GitLab and it turned out that apart from helping test and build your project, GitLab CI can also help deploy you App to your hosting infrastructure. I was now intrigued.

在将GitLab存储库中托管的应用程序部署到Heroku方面,我找不到任何信息或文档。 我在GitLab上浏览了一下,结果发现,除了帮助测试和构建项目外,GitLab CI还可以帮助您将App 部署到托管基础架构中。 我现在很感兴趣。

Before I delve into how I deployed the App, I’d like to explain the benefits of using GitLab or GitHub when you can easily get things done with Heroku Git.

在深入研究如何部署该应用程序之前,我想解释一下当您可以轻松地使用Heroku Git完成工作时使用GitLab或GitHub的好处。

  1. Easier code maintenance - With code repository hosting services like GitHub and GitLab, maintenance of code is easy.

    简化代码维护 -使用GitHub和GitLab等代码存储库托管服务,代码维护变得容易。

  2. Customising pipelines - With GitLab, we can write our own yaml file and include the libraries required for running our application.

    自定义管道 -使用GitLab,我们可以编写自己的yaml文件,并包含运行应用程序所需的库。

  3. For better understanding of Continuous Integration and Continuous Development (CI/CD) - For beginners, this setup helps you understand the coding workflow of testing -> version control -> code maintenance -> application deployment.

    为了更好地理解持续集成和持续开发(CI / CD) -对于初学者,此设置可帮助您了解测试->版本控制->代码维护->应用程序部署的编码工作流程。

Here are the steps required to deploy your App hosted in GitLab to Heroku. The steps here assume you already have a good understanding of Python, Flask, version control, GitLab and Heroku. This write up is also helpful for someone who is just starting out. I have kept it as simple as possible to get things up and running.

这是将GitLab中托管的应用程序部署到Heroku所需的步骤。 此处的步骤假定您已经对Python,Flask,版本控制,GitLab和Heroku有了很好的了解。 这对于刚起步的人也很有帮助。 为了使一切正常运行,我将其保持尽可能简单。

将项目上传到GitLab (Uploading the project to GitLab)

  1. Create a Python virtual environment for us to use. Get into the virtual environment.

    创建一个供我们使用的Python虚拟环境。 进入虚拟环境。
  2. Create a sample Python Flask application on your machine.

    在您的计算机上创建一个示例Python Flask应用程序。
  3. Verify that everything is running fine.

    验证一切运行正常。
  4. Run the command pip freeze > requirements.txt from within the main application folder to catch all the requirements for running your application.

    运行命令pip freeze > requirements. 主应用程序文件夹中的txt文件,以捕获运行应用程序的所有要求。

  5. Create a Procfile which is used by Heroku to declare what commands are run by your application on the Heroku platform. Procfile usually consists of the web server used to run the application. In our case let us use Gunicorn, the default Python WSGI HTTP server. The content of your Procfile will be web: gunicorn <name of the app.py file>:<app-name> where app-name is usually “app”. Place this file within the main application folder.

    创建一个Procfile,供Heroku用来声明您的应用程序在Heroku平台上运行的命令。 Procfile通常由用于运行应用程序的Web服务器组成。 在我们的例子中,让我们使用Gunicorn(默认的Python WSGI HTTP服务器)。 Procfile的内容将是web: gunicorn <name of the app.py file>:<的名称> web: gunicorn <name of the app.py file>:< app-name>,其中app-name通常是“ app”。 将此文件放在主应用程序文件夹中。

  6. Now login (or signup) to GitLab and create a project. The moment you do this, you will get a standard set of instructions on how to “link” your code on your development machine to GitLab project. Just follow the commands, and after that you can do a git push or git pull to/from this project. This is a bit of an elaborate step and your last statement should look something like git push -u origin master. Once done, on refreshing the project page on GitLab, you should see all your code appear in GitLab.

    现在登录(或注册)到GitLab并创建一个项目。 完成此操作后,您将获得一套标准的说明,说明如何将开发计算机上的代码“链接”到GitLab项目。 只需遵循命令,然后您就可以在该项目中执行git push或git pull了。 这是一个复杂的步骤,您的最后一条语句应类似于git push -u origin master 。 完成后,在GitLab上刷新项目页面时,您应该看到所有代码都出现在GitLab中。

链接GitLab和Heroku (Linking GitLab and Heroku)

  1. Login to the Heroku web portal and create an application. Give it a nice name and runtime selection.

    登录到Heroku Web门户并创建一个应用程序。 给它一个好听的名字和运行时选择。

  2. Now within the my_app folder on your development machine, create a file called “.gitlab-ci.yaml” (note the ‘.’ in the beginning).

    现在,在开发计算机上的my_app文件夹中,创建一个名为“ .gitlab-ci.yaml”的文件(在开头请注意“。”)。
  3. This yaml file will have the following structure.

    该yaml文件将具有以下结构。
my_app_file_name:
 script:
 — apt-get update -qy
 — apt-get install -y python-dev python-pip
 — pip install -r requirements.txt
 — export MONGOHQ_URL=$MONGO_URL
 
production:
 type: deploy
 script:
 — apt-get update -qy
 — apt-get install -y ruby-dev
 — gem install dpl
 — dpl — provider=heroku — app=task-mgmt-app — api-key=$HEROKU_SECRET_KEY
 only:
 — master
  1. Change my_app_file_name to the file name of your flask application. You need to set the HEROKU_SECRET_KEY variable in the project variables. You will get this key in the Heroku dashboard. To set it in your GitLab project, go to Settings > CI/CD Pipelines and search for Secret Variables. While using these variables in the yaml, we need to prepend the variable with the ‘$’ sign. It is a good practice not to share secret keys with anyone and also restrict access to them in the project.

    将my_app_file_name更改为flask应用程序的文件名。 您需要在项目变量中设置HEROKU_SECRET_KEY变量。 您将在Heroku仪表板中获得此密钥。 要在您的GitLab项目中进行设置 ,请转至设置> CI / CD管道并搜索Secret Variables。 在Yaml中使用这些变量时,我们需要在变量前加上'$'符号。 最好不要与任何人共享密钥,也不要在项目中限制对密钥的访问。

  2. You are almost there. Run the command git add .gitlab-ci.yml and git commit -m <msg> and git push -u origin master. You will see the file in the GitLab repository now.

    你快到了。 运行命令git add .gitlab-ci.ymlgit commit -m <msg>git push -u origin master 。 您现在将在GitLab存储库中看到该文件。

  3. On the GitLab “My Dashboard” page, click on Pipelines > Jobs to see the job which has started running.

    在GitLab“我的仪表板”页面上,单击管道>作业以查看已开始运行的作业。

  4. In case you are using a database in your App, you might want to link it to the App by placing the details in the .gitlab-ci.yaml file. Please check here for an example. I have used MongDB in my application. Heroku provides adding a set of free libraries/apps to your application. There is an mLab link for adding MongoDB.

    如果您在应用程序中使用数据库,则可能需要通过将详细信息放在.gitlab-ci.yaml文件中来将其链接到应用程序。 请在此处查看示例。 我在应用程序中使用了MongDB。 Heroku提供了一组免费库/应用程序到您的应用程序中。 有一个用于添加MongoDB的mLab链接

Yay! You have now successfully integrated your GitLab with Heroku with CI/CD configuration. Make all code changes you want in your repository, push it to the GitLab project, and see a job start every time there is a code push. For the current setup, I used GitLab public runners available here. You can set up a custom GitLab runner and set appropriate configuration.

好极了! 现在,您已成功将具有GitLab的Heroku与具有CI / CD配置的Heroku集成。 在存储库中进行所需的所有代码更改,将其推送到GitLab项目,并在每次进行代码推送时看到作业开始。 对于当前设置,我使用了此处提供的GitLab公共运行程序。 您可以设置自定义的GitLab运行程序并设置适当的配置。

  1. Creating heroku remote

    创建heroku遥控器

  2. Setting up CI/CD from GitLab on Heroku

    在Heroku上从GitLab设置CI / CD

  3. A task management application - repository : GitLab, deployed on Heroku

    任务管理应用程序-仓库:GitLab,部署在Heroku上

翻译自: https://www.freecodecamp.org/news/setting-up-a-ci-cd-on-gitlab-for-deploying-a-python-flask-application-on-heroku-e154db93952b/

ci/cd heroku

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值