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_URLproduction:type: deployscript:— apt-get update -qy— apt-get install -y ruby-dev— gem install dpl— dpl — provider=heroku — app=task-mgmt-app — api-key=$HEROKU_SECRET_KEYonly:— 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运行程序并设置适当的配置。

有用的链接: (Useful links :)

  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

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

  1. 如何使用Docker在GitLab上设置CI

    by Ying Kit Yuen 英杰苑 如何使用Docker在GitLab上设置CI (How to setup CI on GitLab using Docker) An example usin ...

  2. linux上设置mysql能远程连接不上,请高人指导,linux服务器上mysql怎么设置,才能远程连接...

    haolifengwang 于 2012-05-04 09:54:36发表: 第一步是要创建一个可以远程连接的 MySQL 用户 mysql>usemysql; mysql> GRANT ...

  3. vb编写脚本能让计算机屏幕黑屏,,win7上设置颜色黑屏

    当前位置:我的异常网» VB » ,win7上设置颜色黑屏 ,win7上设置颜色黑屏 www.myexceptions.net  网友分享于:2013-12-16  浏览:7次 求助,win7下设置颜 ...

  4. cardboard 效果_如何在iPhone上设置Google Cardboard

    cardboard 效果 Want to see what this VR thing is about, but don't want to plop down a bunch of cash? G ...

  5. heroku mysql_在Heroku上使用

    5.7 在 Heroku 上使用 PostgreSQL - 第五章.用户注册 5.7 在 Heroku 上使用 PostgreSQL 5.7 在 Heroku 上使用 PostgreSQL Herok ...

  6. ci/cd heroku_在Heroku上部署Dash或Flask Web应用程序。 简易CI / CD。

    ci/cd heroku First, we'll transform the animated scatter map built in my previous article into a bas ...

  7. CI/CD之Jenkins+Gitlab

    文章目录 一.Jenkinx+Gitlab持续集成环境概述 (1)什么是CI/CD (2)Jenkins概述 (3)Gitlab概述 (4)Gitlab和Github的区别 (5)Jenkins配合G ...

  8. GitLab 运行GitLab-Runner CI/CD发布

    本文主要演示使用A发布项目到 B上,和注册GitLab Runner,添加docker类型的Executor来执行构建,并以此为基础完成一个go源码示例项目从编译代码构建.docker镜像打包到应用部 ...

  9. 手把手教学借助CI做代码格式审查、编译审查 | CI/CD搭建流程 — GitLab篇

    本文分享自中移OneOS微信公众号< CI/CD搭建流程 - GitLab篇>,作者 Kisann. 让GitLab CI/CD做什么 嵌入式软件开发领域高频使用的开发语言是C语言,在大型 ...

最新文章

  1. 从产品经理到创业者如何拿到第一个1000万融资
  2. [网络安全自学篇] 四十二.DNS欺骗和钓鱼网站原理详解及防御机理
  3. java 杀掉 linux下进程和进程的子孙进程
  4. java中的udp丢包_UDP丢包问题
  5. 李开复:听AI大佬吐槽真实的人工智能
  6. redhat下的iptables和firewalld 笔记
  7. 在Mac电脑上用VMware Fusion在移动硬盘上安装Windows7虚拟机
  8. VS2005 解决应用程序配置不正确,程序无法启动问题(小问题,大思想)
  9. linux 切换到最左边,Ubuntu 18.04中使用Mac OS风格的Dock启动器替换左侧面板
  10. spring mvc 解决后台传递值乱码问题
  11. Linux系统挂载新硬盘
  12. 关于Facebook,Linkedin网的数据采集总结
  13. 分布式系统设计模式(荣耀典藏版)
  14. 搭建hadoop集群,从安装虚拟机开始直到hadoop成功搭建
  15. cmd快捷键和常用命令
  16. 网站备案后服务器到期,域名备案后服务器到期
  17. 大话微服务:(二)对于业务如何划分微服务,即微服务的颗粒度,又称业务边界
  18. Unity学习-Prinmatives原形
  19. 初中三年级数学可以用计算机吗,不到3分钟,这份初中数学攻略被家长和学生疯狂转发!太实用了!...
  20. c语言 老鼠乘法,c语言-老鼠走迷宫逐步理解

热门文章

  1. Collection的使用 对象 java
  2. 继承关系 c# 1613704854
  3. 爬虫-36kr-使用xpath爬取数据-part1-提取接口所需的6开头的数字-拼接下一个接口的路径
  4. 1910140852linux安装g
  5. linux-gzip压缩
  6. linux-文件的删除与创建
  7. css-模态对话框的制作
  8. mysql的优化-添加环境变量启动服务
  9. 【翻译】ANDROID KTX – 使用Kotlin进行Android开发
  10. 前后端分离的项目,如何解决登录问题