# Test and deploy a Scala application to Heroku

> 原文：[https://docs.gitlab.com/ee/ci/examples/test-scala-application.html](https://docs.gitlab.com/ee/ci/examples/test-scala-application.html)

*   [Add `.gitlab-ci.yml` file to project](#add-gitlab-ciyml-file-to-project)
*   [Display test coverage in job](#display-test-coverage-in-job)
*   [Heroku application](#heroku-application)
*   [Heroku API key](#heroku-api-key)

# Test and deploy a Scala application to Heroku[](#test-and-deploy-a-scala-application-to-heroku "Permalink")

该示例演示了使用 SBT 将 GitLab CI / CD 与 Scala 应用程序集成. 您可以查看或创建[示例项目，](https://gitlab.com/gitlab-examples/scala-sbt)并查看其过去的[CI 作业](https://gitlab.com/gitlab-examples/scala-sbt/-/jobs?scope=finished)的日志.

## Add `.gitlab-ci.yml` file to project[](#add-gitlab-ciyml-file-to-project "Permalink")

以下`.gitlab-ci.yml`应该添加到存储库的根目录中以触发 CI：

```
image: openjdk:8

stages:
  - test
  - deploy

before_script:
  - apt-get update -y
  - apt-get install apt-transport-https -y
  ## Install SBT
  - echo "deb http://dl.bintray.com/sbt/debian /" | tee -a /etc/apt/sources.list.d/sbt.list
  - apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 642AC823
  - apt-get update -y
  - apt-get install sbt -y
  - sbt sbtVersion

test:
  stage: test
  script:
    - sbt clean coverage test coverageReport

deploy:
  stage: deploy
  script:
    - apt-get update -yq
    - apt-get install rubygems ruby-dev -y
    - gem install dpl
    - dpl --provider=heroku --app=gitlab-play-sample-app --api-key=$HEROKU_API_KEY 
```

在以上配置中：

*   `before_script`将安装[SBT](https://www.scala-sbt.org/)并显示正在使用的版本.
*   `test`阶段执行 SBT 来编译和测试项目.
    *   [sbt-scoverage](https://github.com/scoverage/sbt-scoverage)用作 SBT 插件来测量测试覆盖率.
*   `deploy`阶段使用 dpl 将项目自动部署到 Heroku.

您可以通过在`build.sbt`定义其他版本的 Scala 和 SBT 来使用它们.

## Display test coverage in job[](#display-test-coverage-in-job "Permalink")

在**设置➔管道➔覆盖率报告**项目设置中，将`Coverage was \[\d+.\d+\%\]`添加`Coverage was \[\d+.\d+\%\]`正则表达式，以从构建跟踪中检索[测试覆盖率](../pipelines/settings.html#test-coverage-report-badge)并将其与作业一起显示.

必须启用**管道**才能显示此选项.

## Heroku application[](#heroku-application "Permalink")

需要 Heroku 应用程序. 您可以通过[仪表板](https://dashboard.heroku.com/)创建一个. 用您的应用程序名称`.gitlab-ci.yml`文件中的`gitlab-play-sample-app` .

## Heroku API key[](#heroku-api-key "Permalink")

您可以在[帐户中](https://dashboard.heroku.com/account)查找 Heroku API 密钥. 使用键`HEROKU_API_KEY`在**项目➔变量中**添加具有此值的[受保护变量](../variables/README.html#protect-a-custom-variable) .