docker github
There are plenty of continuous integration and delivery tools that can be used to run CI/CD pipelines, such as Jenkins, Travis CI, and CircleCI. When it comes to projects hosted on GitHub, there is another native option available, which is GitHub workflows.
有很多连续集成和交付工具可用于运行CI / CD管道,例如Jenkins,Travis CI和CircleCI。 对于托管在GitHub上的项目,还有另一个本机选项可用,即GitHub工作流程 。
GitHub workflows can be used to achieve several goals, such as building Docker images, running unit tests, and even deploying to a remote server. The workflows can be triggered by GitHub events as well as external events. For instance, it is possible to create a workflow that will be executed once you create a new release on the repository. In addition, the same workflow can be configured to be triggered by an external API call.
GitHub工作流可用于实现多个目标,例如构建Docker映像,运行单元测试,甚至部署到远程服务器。 工作流可以由GitHub事件以及外部事件触发。 例如,可以创建一个工作流,一旦在存储库上创建新版本,该工作流将被执行。 此外,可以将同一工作流配置为由外部API调用触发。
Because GitHub workflows can be triggered using native GitHub events and without defining webhooks, it is a much easier tool to use and define than other tools and options.
由于可以使用本地GitHub事件触发GitHub工作流,而无需定义webhooks,因此它是比其他工具和选项更易于使用和定义的工具。
GitHub workflows are defined in YAML format files that describe which actions or steps need to be executed during the workflow. You can find more about the internals of GitHub workflows and GitHub actions in the article “Build GitHub Actions Using Docker Containers”.
GitHub工作流以YAML格式文件定义,该文件描述了在工作流期间需要执行哪些操作或步骤。 您可以在文章“ 使用Docker容器构建GitHub操作 ”中找到有关GitHub工作流内部和GitHub操作的更多信息。
Since GitHub's actions can be used to replace other CI tools and provide an easy and straightforward way for defining CI tasks, I decided to use them for managing and defining CI/CD pipelines for my projects hosted on GitHub.
由于GitHub的动作可用于替换其他CI工具并提供定义CI任务的简便方法,因此我决定使用它们来管理和定义GitHub上托管的项目的CI / CD管道。
One of the pipelines that I wanted to build is a pipeline to deploy Docker services to a remote server using Docker commands. Unfortunately, I could not find any GitHub actions in the marketplace that could suit my needs and help me perform Docker deployments to remote servers. As a result, I decided to build my own GitHub action for deploying Docker services.
我要构建的管道之一是使用Docker命令将Docker服务部署到远程服务器的管道。 不幸的是,我在市场上找不到适合我的需求并帮助我执行将Docker部署到远程服务器的GitHub操作。 结果,我决定构建自己的GitHub操作来部署Docker服务。
In this article, I will go step by step to explain the internals and details of the Github action that I built to manage docker service deployment.
在本文中,我将逐步说明为管理docker服务部署而构建的Github操作的内部细节。
行动要求 (Action Requirements)
As I stated before, I looked first in the GitHub actions to find an existing action that I can use for deploying Docker services, but I could not find one that meets my needs. Below are the needed features that I was looking for to be able to implement my CI/CD pipelines.
如前所述,我首先在GitHub操作中查找了可用于部署Docker服务的现有操作,但找不到满足我需求的操作。 以下是我正在寻找的能够实现CI / CD管道所需的功能。
- Support two different deployment modes: 支持两种不同的部署模式:
→ Docker swarm: Support deploying Docker swarm services from predefined Docker stack files.
→Docker swarm:支持从预定义的Docker堆栈文件中部署Docker swarm服务。
→ Docker Compose: Support deploying Docker container using Docker Compose.
→Docker Compose:支持使用Docker Compose部署Docker容器。
- Deploy the Docker services using the Docker API remotely from the GitHub workers without copying any files to the remote servers. 从GitHub工作人员使用Docker API远程部署Docker服务,而无需将任何文件复制到远程服务器。
- Support deployment without the Docker API: In this case, the Docker Compose files will be copied to the remote server and Docker commands will be executed on the servers using SSH protocol. 支持不使用Docker API的部署:在这种情况下,Docker Compose文件将被复制到远程服务器,并且将使用SSH协议在服务器上执行Docker命令。
- Allow pulling Docker images to the remote servers before deploying the Docker containers. 允许在部署Docker容器之前将Docker映像拉到远程服务器。
- Execute pre-deployment tasks before rolling out the new versions of the containers. For instance, running database schema migrations to update the application database before creating the new Docker containers. 在推出新版本的容器之前,请执行部署前任务。 例如,在创建新的Docker容器之前,运行数据库架构迁移以更新应用程序数据库。
使用GitHub动作 (Using a GitHub Action)
The implemented GitHub action can be used in the same way as all other actions. Actions support a number of required parameters as well as a number of optional parameters that can be used to influence the behavior of the action or the deployment process. Below is the list of all required parameters:
可以以与所有其他操作相同的方式使用已实现的GitHub操作。 动作支持许多必需参数以及许多可选参数,这些参数可用于影响动作或部署过程的行为。 以下是所有必需参数的列表:
remote_docker_host
: The remote server address. The values of this parameter must follow the following format: user@host. And the host needs to be publically accessible, i.e., it must have a public IP. The GitHub action will be communicating to the remote server using the SSH protocol.remote_docker_host
:远程服务器地址。 此参数的值必须遵循以下格式:user @ host。 并且主机需要可公开访问,即它必须具有公共IP。 GitHub动作将使用SSH协议与远程服务器通信。ssh_public_key
: The public key of the remote server. This is the same key that will be added to theknown_hosts
files after a successful SSH connection to the server.ssh_public_key
:远程服务器的公共密钥。 这是与服务器成功建立SSH连接后将添加到known_hosts
文件中的密钥。ssh_private_key
: The private key used to connect to the remote server. The public part of this key needs to be added to theauthorized_hosts
files on the remote server.ssh_private_key
:用于连接到远程服务器的私钥。 该密钥的公共部分需要添加到远程服务器上的authorized_hosts
文件中。args
: The arguments to thedocker
ordocker-compose
command that will be used to start the deployment.args
:用于启动部署的docker
或docker-compose
命令的参数。
In addition to the above mandatory parameters, the action supports the below optional parameters. Each of these parameters has a default value that will be used in case there is no value provided:
除上述必需参数外,该操作还支持以下可选参数。 这些参数中的每一个都有一个默认值,如果没有提供任何值,将使用该默认值:
pre_deployment_command_args
: The args for the pre-deployment command. The default value is an empty string.pre_deployment_command_args
:预部署命令的参数。 默认值为空字符串。docker_prune
: A boolean input to trigger thedocker-prune
command. The default value isfalse
.docker_prune
:触发docker_prune
docker-prune
命令的布尔输入。 默认值为false
。pull_images_first
: Pulldocker images
before deploying. The default value isfalse
.pull_images_first
:在部署之前拉pull_images_first
docker images
。 默认值为false
。deployment_mode
: Deployment mode eitherdocker-swarm
ordocker-compose
. Default isdocker-compose
.deployment_mode
:部署模式docker-swarm
或docker-compose
。 默认是docker-compose
。copy_stack_file
: Copy stack file to remote server and deploy from the server. The default isfalse
.copy_stack_file
:将堆栈文件复制到远程服务器并从服务器部署。 默认值为false
。deploy_path
: The path where the stack files will be copied to. Default~/docker-deployment
.deploy_path
:将堆栈文件复制到的路径。 默认~/docker-deployment
。stack_file_name
: Docker stack file used. Default isdocker-compose.yaml
.stack_file_name
:使用的Docker堆栈文件。 默认值为docker-compose.yaml
。keep_files
: Number of files to be kept on the server. The default is three.keep_files
:要保留在服务器上的文件数。 默认值为三。
使用动作的例子 (Examples of Using an Action)
The developed action can be used in several ways and to achieve different goals. Below are brief descriptions of four use cases that the action can be used in.
可以以多种方式使用已制定的措施并实现不同的目标。 以下是可以在其中使用操作的四个用例的简要说明。
Case One: Deploy a swarm service to a remote Docker swarm cluster using Docker API.
案例一:使用Docker API将集群服务部署到远程Docker集群集群。
- name: Deploy to Docker swarm
uses: wshihadeh/docker-deployment-action@v1
with:
remote_docker_host: [email protected]
ssh_private_key: ${{ secrets.DOCKER_SSH_PRIVATE_KEY }}
ssh_public_key: ${{ secrets.DOCKER_SSH_PUBLIC_KEY }}
deployment_mode: docker-swarm
args: my_applicaion_stack_name
Case Two: Deploy a docker-compose
stack to a remote Docker server using Docker API.
案例二:使用Docker API将docker-compose
堆栈部署到远程Docker服务器。
- name: Deploy to Docker Host
uses: wshihadeh/docker-deployment-action@v1
with:
remote_docker_host: [email protected]
ssh_private_key: ${{ secrets.DOCKER_SSH_PRIVATE_KEY }}
ssh_public_key: ${{ secrets.DOCKER_SSH_PUBLIC_KEY }}
deployment_mode: docker-compose
args: up -d
pre_deployment_command_args: 'bundle exec rake db:migrate'
docker_prune: 'true'
pull_images_first: 'true'
Case Three: Deploy a swarm service to a remote Docker swarm cluster by running deployment commands on the swarm cluster.
案例三:通过在群集集群上运行部署命令,将群集服务部署到远程Docker群集集群。
- name: Deploy to Docker swarm
uses: wshihadeh/docker-deployment-action@v1
with:
remote_docker_host: [email protected]
ssh_private_key: ${{ secrets.DOCKER_SSH_PRIVATE_KEY }}
ssh_public_key: ${{ secrets.DOCKER_SSH_PUBLIC_KEY }}
deployment_mode: docker-swarm
copy_stack_file: true
deploy_path: /root/my-deployment
stack_file_name: docker-stack.yaml
keep_files: 5
args: my_applicaion_stack_name
Case Four: Deploy a docker-compose
stack to a remote Docker server by running the command on the Docker node.
案例四:通过在Docker节点上运行命令,将docker docker-compose
堆栈部署到远程Docker服务器。
- name: Deploy to Docker Host
uses: wshihadeh/docker-deployment-action@v1
with:
remote_docker_host: [email protected]
ssh_private_key: ${{ secrets.DOCKER_SSH_PRIVATE_KEY }}
ssh_public_key: ${{ secrets.DOCKER_SSH_PUBLIC_KEY }}
deployment_mode: docker-compose
copy_stack_file: true
deploy_path: /root/my-deployment
stack_file_name: docker-compose.yaml
keep_files: 5
args: up -d
docker_prune: 'false'
pull_images_first: 'false'
结论 (Conclusion)
GitHub actions are one of the best tools for implementing native CI/CD pipelines on GitHub. The marketplace contains hundreds, if not thousands, of actions that can be used to build the desired pipelines. In addition, building new actions and publishing them in the marketplace to support new CI/CD use cases is pretty easy and straightforward.
GitHub动作是在GitHub上实现本机CI / CD管道的最佳工具之一。 市场包含数百个(甚至数千个)动作,可用于构建所需的管道。 此外,建立新的动作并将其发布在市场上以支持新的CI / CD用例非常容易和直接。
This article illustrated and explained a GitHub action that I built to help in deploying Docker services and containers to docker swarm and docker nodes.
本文说明并解释了我为帮助将Docker服务和容器部署到docker swarm和docker节点而构建的GitHub操作 。
翻译自: https://medium.com/better-programming/docker-deployments-with-github-actions-7e59bb532505
docker github