idea设置项目部署_如何以简单的方法在您的家庭项目中设置连续部署

idea设置项目部署

by Julius

通过朱利叶斯

如何以简单的方法在您的家庭项目中设置连续部署 (How to set up continuous deployment in your home project the easy way)

Continuous Deployment is a beautiful thing. Committing your project and seeing it being built and deployed without having to do anything is mesmerizing.

持续部署是一件美丽的事情。 提交项目并看到它在无需进行任何操作的情况下进行构建和部署令人着迷。

And in this article, I want to show you how to get it done in your home project with ease.

在本文中,我想向您展示如何轻松地在您的家庭项目中完成它。

To clear it up, here is a flowchart showing the differences between Continuous Delivery and Continuous Deployment.

为了解决这个问题,下面的流程图显示了持续交付和持续部署之间的区别。

Since most of the time no one but you depends on your home project, we’re going for a workflow with Continuous Deployment since you want to see your changes immediately deployed. If that’s not the case, you can change the workflow later.

由于在大多数情况下,除了您之外,没有人依赖您的主项目,因此我们要使用持续部署的工作流,因为您希望立即看到所做的更改。 如果不是这种情况,您可以稍后更改工作流程。

You will learn about the following:

您将了解以下内容:

  • How to make a Dockerfile

    如何制作一个Dockerfile
  • How to push your project to GitHub

    如何将您的项目推送到GitHub
  • Automatically building the docker image on Docker Hub

    在Docker Hub上自动构建Docker映像
  • Automatically downloading and running the image with Watchtower

    使用守望台自动下载并运行图像

Prerequisites:

先决条件:

  • Some knowledge about Docker and the Dockerfile, though I will explain some of it along the way

    关于Docker和Dockerfile的一些知识,尽管我会在其中进行一些解释
  • Have git installed

    安装了git

  • A Docker Hub account

    Docker Hub帐户

  • A (Linux) server (either physical or virtual) running Docker

    运行Docker的(Linux)服务器(物理或虚拟)

For reference, this is the example GitHub repository, and this is the example docker hub repository that I’ll be using.

作为参考, 是示例GitHub存储库, 也是我将使用的示例docker hub存储库。

Thus this tutorial will only be useful if you intend to run your software with Docker (which I recommend as Docker is fantastic).

因此,本教程仅在打算使用Docker运行软件时才有用(我建议这样做,因为Docker非常棒)。

为什么要使用Docker? (Why use Docker?)

Docker enables you to have the same environment for development and production which eliminates Heisenbugs and the “it works on my machine” problem. Also, containers are isolated which gives us security benefits.There’s more to it, but these two benefits make me always deliver my software in Docker containers.

Docker使您能够拥有相同的开发和生产环境,从而消除了Heisenbugs和“在我的机器上运行”的问题。 另外,容器是隔离的,这给我们带来了安全性好处,它还有更多好处,但是这两个好处使我始终可以在Docker容器中交付软件。

设置你的Dockerfile (Setting up your Dockerfile)

First, we will make a Dockerfile for the project. This special file is always called “Dockerfile” without an extension and sits at the top directory of your project.

首先,我们将为该项目创建一个Dockerfile。 这个特殊文件始终称为“ Dockerfile”,没有扩展名,位于项目的顶层目录中。

A Dockerfile starts with the FROM statement which tells Docker which base image you want to start with. You can imagine this as using a canvas with the background already drawn and only the central part (your program) missing.Most of the time the image you want to pull is the base image of your programming language, which you can find at the before mentioned Docker Hub.

Dockerfile以FROM语句开头,该语句告诉Docker您想从哪个基础映像开始。 您可以想像这是使用已绘制背景且仅缺少中心部分(您的程序)的画布。大多数情况下,您要提取的图像是编程语言的基础图像,您可以在之前找到它。提到Docker Hub

Next, we copy our project files into the docker container with the COPY.. command. What does this do?

接下来,我们使用COPY..命令将项目文件复制到docker容器中。 这是做什么的?

It takes the files from the first directory (the dot refers to the current directory of the file, which includes all your project files) and puts it in the current directory of your Docker container (remember your docker container is its own OS). Your files are now at the base directory there, which you may want to change.

它从第一个目录中获取文件(点表示文件的当前目录,其中包括您的所有项目文件),并将其放在Docker容器的当前目录中(请记住,您的Docker容器是其自己的OS)。 您的文件现在位于该目录的基本目录中。

Next, we need to install dependencies, which I will use python pip for, but any equivalent package management system depending on your language of choice will do. The critical thing to learn here is how to execute commands in the container with RUN.

接下来,我们需要安装依赖项,我将使用python pip来安装依赖项,但是取决于您选择的语言的任何等效程序包管理系统都可以。 这里要学习的关键是如何使用RUN在容器中执行命令。

From python:3.7COPY . .RUN pip install -r requirements.txt

Easy, isn’t it? Now we have to start our program in the container.

很简单,不是吗? 现在我们必须在容器中启动程序。

CMD ["python", "./my_script.py"]

The CMD statement is unique. Every Dockerfile has to have it as its last line because it starts the primary process in the container.

CMD语句是唯一的。 每个Dockerfile都必须将其作为最后一行,因为它在容器中启动了主进程。

You have finished your Dockerfile! You can now manually build your image and container, but we’re going to skip that for now.

您已完成Dockerfile! 您现在可以手动构建图像和容器,但是现在我们将略过它们。

Now, we’ll create our repository on GitHub, but remember to leave “Initialize this repository with a README” unticked.

现在,我们将在GitHub上创建我们的存储库,但请记住不要选中“使用README初始化此存储库”。

Then you’d need to copy the remote URL.

然后,您需要复制远程URL。

Open a cmd/shell in the root directory of your project.

在您的项目的根目录中打开一个cmd / shell。

You need to initialize your git repository, add your files, configure the remote, commit the files and push your project to GitHub.

您需要初始化git存储库,添加文件,配置远程服务器,提交文件并将项目推送到GitHub。

git initgit add *git remote add origin https://github.com/<user>/<repository>.gitgit commit -a -m "Make Dockerfile ready for CD"git push -u origin master

Now, your GitHub Repository should look like this:

现在,您的GitHub存储库应如下所示:

Congratulations, you’re about halfway done!

恭喜,您已完成一半!

The next step is to connect GitHub to Docker Hub. For this, you go to the account settings.

下一步是将GitHub连接到Docker Hub。 为此,您转到帐户设置。

Scroll down and connect your git host.

向下滚动并连接您的git主机。

Create your repository on docker hub now.

现在在Docker Hub上创建您的存储库。

Give the repo a name and click the GitHub icon (or Bitbucket, if that’s your thing). Now choose your organization (usually your username) and your project’s name. If you want to use your master image for the build and always push to latest, you can now click “Create & Build” and watch your image being built for you. Otherwise, you have to edit the build settings.

给仓库指定一个名称,然后单击GitHub图标(或Bitbucket,如果那是你的事)。 现在选择您的组织(通常是您的用户名)和项目的名称。 如果您要使用主映像进行构建并始终推送到最新版本,则现在可以单击“创建并构建”并观看为您构建的映像。 否则,您必须编辑构建设置。

Last steps! Now you need Watchtower on your target machine.Watchtower is a program that pulls your running docker images and checks for updates. If there are any updates, it gracefully shuts down the original container and creates a container from the new image with the same settings.

最后一步! 现在,您需要在目标计算机上使用Watchtower.Watchtower是一个程序,可以拉出正在运行的Docker映像并检查更新。 如果有任何更新,它会正常关闭原始容器,并使用相同的设置从新映像创建一个容器。

The best thing is that we can also install Watchtower with Docker!

最好的是,我们还可以在Docker上安装Watchtower!

Enter the following into your terminal:

在您的终端中输入以下内容:

docker run -d --name watchtower -v /var/run/docker.sock:/var/run/docker.sock v2tec/watchtower

Then you need to run the Docker container for your project!

然后,您需要为您的项目运行Docker容器!

docker run -d --name <my-project> <username>/<my-project>

The “-d” option makes your program run in the background, so the program doesn’t shut down if you close the terminal.

“ -d”选项使您的程序在后台运行,因此如果您关闭终端,则该程序不会关闭。

So to summarize, if you push a commit to your GitHub repository, Docker hub will automatically build a Docker image for you. This image then gets pulled by WatchTower and is run with all original options.

综上所述,如果将提交推送到GitHub存储库,则Docker Hub将自动为您构建Docker映像。 然后,此图像由WatchTower提取,并与所有原始选项一起运行。

If you need help at any point don’t be afraid to ask, I’m happy to help.If it is a technical problem, an issue on the GitHub project would be awesome!

如果您在任何时候都需要帮助,不要害怕问我,我们很高兴为您提供帮助。如果是技术问题,那么GitHub项目上的问题将非常好!

但是测试呢? (But what about tests?)

Good question!You can use Travis CI to run your tests at the same time.You can read about this here, but the gist of it is, that you add another file to your repository which has instructions for an external server to execute unit tests or any other instructions.

很好的问题!您可以同时使用Travis CI来运行测试。您可以在此处阅读有关内容,但要点是,您向存储库中添加了另一个文件,该文件包含有关外部服务器执行单元测试的说明或任何其他说明。

But what if I only want my docker image to build if the tests pass?
但是,如果仅通过测试,我只希望构建我的docker映像怎么办?

This breaks our workflow a bit.We now can’t rely on docker hub to build our images anymore. Instead, it’s also going to be Travis CI that produces the image and then pushes it to your Docker Hub repository. Read about this here.

这有点中断了我们的工作流程。我们现在不能再依赖docker hub来构建我们的映像了。 取而代之的是,将由Travis CI生成映像,然后将其推送到您的Docker Hub存储库。 在这里阅读有关内容。

翻译自: https://www.freecodecamp.org/news/how-to-set-up-continuous-deployment-in-your-home-project-the-easy-way-41b84a467eed/

idea设置项目部署

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值