企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Using Dpl as deployment tool > 原文:[https://docs.gitlab.com/ee/ci/examples/deployment/README.html](https://docs.gitlab.com/ee/ci/examples/deployment/README.html) * [Requirements](#requirements) * [Basic usage](#basic-usage) * [Using Dpl with Docker](#using-dpl-with-docker) * [Usage in staging and production](#usage-in-staging-and-production) * [Storing API keys](#storing-api-keys) # Using Dpl as deployment tool[](#using-dpl-as-deployment-tool "Permalink") [Dpl](https://github.com/travis-ci/dpl) (发音为 DPL)是由 Travis CI 开发和使用的,用于连续部署的部署工具,但也可以与 GitLab CI / CD 一起使用. Dpl 可用于部署到任何[受支持的提供程序](https://github.com/travis-ci/dpl#supported-providers) . ## Requirements[](#requirements "Permalink") 要使用 Dpl,您至少需要具备安装 gem 的 Ruby 1.9.3. ## Basic usage[](#basic-usage "Permalink") 可以将 Dpl 安装在具有以下条件的任何计算机上: ``` gem install dpl ``` 这使您可以从本地终端测试所有命令,而不必在 CI 服务器上进行测试. 如果您没有安装 Ruby,则可以在兼容 Debian 的 Linux 上执行以下操作: ``` apt-get update apt-get install ruby-dev ``` Dpl 为大量服务提供支持,包括:Heroku,Cloud Foundry,AWS / S3 等. 要使用它,只需定义提供者和提供者所需的任何其他参数. 例如,如果要使用它将应用程序部署到 Heroku,则需要将`heroku`指定为提供者,并指定`api-key`和`app` . 所有可能的参数都可以在这里找到: [https](https://github.com/travis-ci/dpl#heroku-api) : [//github.com/travis-ci/dpl#heroku-api](https://github.com/travis-ci/dpl#heroku-api) . ``` staging: stage: deploy script: - gem install dpl - dpl --provider=heroku --app=my-app-staging --api-key=$HEROKU_STAGING_API_KEY ``` 在上面的示例中,我们使用 Dpl 通过存储在`HEROKU_STAGING_API_KEY`安全变量中的 API 密钥将`my-app-staging`部署到 Heroku 服务器. 要使用其他提供程序,请查看一整列[受支持的提供程序](https://github.com/travis-ci/dpl#supported-providers) . ## Using Dpl with Docker[](#using-dpl-with-docker "Permalink") 在大多数情况下,您将配置[GitLab Runner](https://docs.gitlab.com/runner/)以使用服务器的 Shell 命令. 这意味着所有命令都在本地用户的上下文中运行(例如`gitlab_runner`或`gitlab_ci_multi_runner` ). 这也意味着最有可能在 Docker 容器中没有安装 Ruby 运行时. 您将必须安装它: ``` staging: stage: deploy script: - apt-get update -yq - apt-get install -y ruby-dev - gem install dpl - dpl --provider=heroku --app=my-app-staging --api-key=$HEROKU_STAGING_API_KEY only: - master ``` 第一行`apt-get update -yq`更新可用软件包的列表,第二行`apt-get install -y ruby-dev`在系统上安装 Ruby 运行时. 上面的示例对所有与 Debian 兼容的系统均有效. ## Usage in staging and production[](#usage-in-staging-and-production "Permalink") 在开发工作流程中具有暂存(开发)和生产环境是很常见的 让我们考虑以下示例:我们希望将`master`分支部署到`staging`并将所有标签部署到`production`环境. 该设置的最终`.gitlab-ci.yml`如下所示: ``` staging: stage: deploy script: - gem install dpl - dpl --provider=heroku --app=my-app-staging --api-key=$HEROKU_STAGING_API_KEY only: - master production: stage: deploy script: - gem install dpl - dpl --provider=heroku --app=my-app-production --api-key=$HEROKU_PRODUCTION_API_KEY only: - tags ``` 我们创建了两个在不同事件上执行的部署作业: 1. `staging` is executed for all commits that were pushed to `master` branch, 2. 对所有推送的标签执行`production` . 我们还使用两个安全变量: 1. `HEROKU_STAGING_API_KEY` -Heroku API 密钥,用于部署登台应用程序, 2. `HEROKU_PRODUCTION_API_KEY` -Heroku API 密钥,用于部署生产应用程序. ## Storing API keys[](#storing-api-keys "Permalink") 可以通过在项目的**设置➔CI / CD➔变量中**添加安全**变量** . 项目设置中定义的变量与构建脚本一起发送到 Runner. 安全变量存储在存储库之外. 切勿将机密存储在项目的`.gitlab-ci.yml` . 将秘密的值隐藏在作业日志中也很重要. 您可以通过使用`$` (在非 Windows 运行程序上)或`%` (对于 Windows 批处理运行程序)前缀名称来访问添加的变量: 1. `$VARIABLE`用于非 Windows 运行器 2. `%VARIABLE%` -用于 Windows 批处理运行器 阅读有关[CI 变量的](../../variables/README.html)更多信息.