2019-06-11 12:39:26 -04:00
---
2020-05-29 14:08:26 -04:00
stage: Verify
group: Continuous Integration
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2019-06-11 12:39:26 -04:00
type: tutorial
---
2019-03-07 22:34:28 -05:00
# Test and deploy a Scala application to Heroku
2016-02-26 06:31:35 -05:00
2020-03-26 23:07:56 -04:00
This example demonstrates the integration of GitLab CI/CD with Scala
2019-06-11 12:39:26 -04:00
applications using SBT. You can view or fork the [example project ](https://gitlab.com/gitlab-examples/scala-sbt )
and view the logs of its past [CI jobs ](https://gitlab.com/gitlab-examples/scala-sbt/-/jobs?scope=finished ).
2016-02-26 06:31:35 -05:00
2016-11-18 10:56:25 -05:00
## Add `.gitlab-ci.yml` file to project
2016-02-26 06:31:35 -05:00
2016-06-18 03:23:31 -04:00
The following `.gitlab-ci.yml` should be added in the root of your
repository to trigger CI:
2016-02-26 06:31:35 -05:00
2016-06-18 03:23:31 -04:00
``` yaml
2020-05-12 08:09:47 -04:00
image: openjdk:8
2016-03-10 11:32:37 -05:00
2016-11-18 10:56:25 -05:00
stages:
- test
- deploy
2016-02-26 06:31:35 -05:00
before_script:
2016-06-18 03:23:31 -04:00
- apt-get update -y
- apt-get install apt-transport-https -y
2016-11-18 10:56:25 -05:00
## Install SBT
2016-02-26 06:31:35 -05:00
- 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
2018-10-22 04:33:01 -04:00
- sbt sbtVersion
2016-02-26 06:31:35 -05:00
test:
2016-11-18 10:56:25 -05:00
stage: test
2016-02-26 06:31:35 -05:00
script:
- sbt clean coverage test coverageReport
2016-11-18 10:56:25 -05:00
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
2016-02-26 06:31:35 -05:00
```
2019-06-11 12:39:26 -04:00
In the above configuration:
2019-07-07 20:41:33 -04:00
- The `before_script` installs [SBT ](https://www.scala-sbt.org/ ) and
2019-06-25 00:34:55 -04:00
displays the version that is being used.
2019-06-11 12:39:26 -04:00
- The `test` stage executes SBT to compile and test the project.
2019-06-25 00:34:55 -04:00
- [sbt-scoverage ](https://github.com/scoverage/sbt-scoverage ) is used as an SBT
plugin to measure test coverage.
2019-06-11 12:39:26 -04:00
- The `deploy` stage automatically deploys the project to Heroku using dpl.
2016-02-26 06:31:35 -05:00
2016-06-18 03:23:31 -04:00
You can use other versions of Scala and SBT by defining them in
`build.sbt` .
2016-02-26 06:31:35 -05:00
2017-02-13 11:59:57 -05:00
## Display test coverage in job
2016-06-18 03:23:31 -04:00
Add the `Coverage was \[\d+.\d+\%\]` regular expression in the
2020-12-01 13:09:42 -05:00
**Settings > Pipelines > Coverage report** project setting to
2020-03-13 11:09:21 -04:00
retrieve the [test coverage ](../pipelines/settings.md#test-coverage-report-badge )
2019-03-10 21:47:01 -04:00
rate from the build trace and have it displayed with your jobs.
2016-06-18 03:23:31 -04:00
2017-02-13 11:59:57 -05:00
**Pipelines** must be enabled for this option to appear.
2016-11-18 10:56:25 -05:00
## Heroku application
A Heroku application is required. You can create one through the
[Dashboard ](https://dashboard.heroku.com/ ). Substitute `gitlab-play-sample-app`
in the `.gitlab-ci.yml` file with your application's name.
## Heroku API key
You can look up your Heroku API key in your
2020-05-05 14:09:43 -04:00
[account ](https://dashboard.heroku.com/account ). Add a [protected variable ](../variables/README.md#protect-a-custom-variable ) with
2016-11-18 10:56:25 -05:00
this value in **Project ➔ Variables** with key `HEROKU_API_KEY` .