debian-mirror-gitlab/doc/ci/examples/deploy_spring_boot_to_cloud_foundry/index.md

144 lines
5.3 KiB
Markdown
Raw Normal View History

2018-11-08 19:23:39 +05:30
---
2020-06-23 00:09:42 +05:30
stage: Release
2021-02-22 17:27:13 +05:30
group: Release
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-09-04 21:01:54 +05:30
type: tutorial
2018-11-08 19:23:39 +05:30
---
# Deploy a Spring Boot application to Cloud Foundry with GitLab CI/CD
## Introduction
In this article, we'll demonstrate how to deploy a [Spring
Boot](https://projects.spring.io/spring-boot/) application to [Cloud
Foundry (CF)](https://www.cloudfoundry.org/) with GitLab CI/CD using the [Continuous
2019-12-21 20:55:43 +05:30
Deployment](https://about.gitlab.com/blog/2016/08/05/continuous-integration-delivery-and-deployment-with-gitlab/#continuous-deployment)
2018-11-08 19:23:39 +05:30
method.
All the code for this project can be found in this [GitLab
2021-02-22 17:27:13 +05:30
repository](https://gitlab.com/gitlab-examples/spring-gitlab-cf-deploy-demo).
2018-11-08 19:23:39 +05:30
In case you're interested in deploying Spring Boot applications to Kubernetes
2019-12-21 20:55:43 +05:30
using GitLab CI/CD, read through the blog post [Continuous Delivery of a Spring Boot application with GitLab CI and Kubernetes](https://about.gitlab.com/blog/2016/12/14/continuous-delivery-of-a-spring-boot-application-with-gitlab-ci-and-kubernetes/).
2018-11-08 19:23:39 +05:30
## Requirements
2019-09-04 21:01:54 +05:30
This tutorial assumes you are familiar with Java, GitLab, Cloud Foundry, and GitLab CI/CD.
2018-11-08 19:23:39 +05:30
2019-09-04 21:01:54 +05:30
To follow along, you will need:
2018-11-08 19:23:39 +05:30
- An account on [Pivotal Web Services (PWS)](https://run.pivotal.io/) or any
2019-09-04 21:01:54 +05:30
other Cloud Foundry (CF) instance.
- An account on GitLab.
2018-11-08 19:23:39 +05:30
2021-02-22 17:27:13 +05:30
NOTE:
2018-11-08 19:23:39 +05:30
You will need to replace the `api.run.pivotal.io` URL in the all below
commands with the [API
URL](https://docs.cloudfoundry.org/running/cf-api-endpoint.html) of your CF
instance if you're not deploying to PWS.
## Create your project
To create your Spring Boot application you can use the Spring template in
GitLab when creating a new project:
![New Project From Template](img/create_from_template.png)
## Configure the deployment to Cloud Foundry
To deploy to Cloud Foundry we need to add a `manifest.yml` file. This
is the configuration for the CF CLI we will use to deploy the application. We
will create this in the root directory of our project with the following
content:
```yaml
---
applications:
2020-07-28 23:09:34 +05:30
- name: gitlab-hello-world
random-route: true
memory: 1G
path: target/demo-0.0.1-SNAPSHOT.jar
2018-11-08 19:23:39 +05:30
```
## Configure GitLab CI/CD to deploy your application
2019-02-15 15:39:39 +05:30
Now we need to add the GitLab CI/CD configuration file
2018-11-08 19:23:39 +05:30
([`.gitlab-ci.yml`](../../yaml/README.md)) to our
project's root. This is how GitLab figures out what commands need to be run whenever
code is pushed to our repository. We will add the following `.gitlab-ci.yml`
file to the root directory of the repository, GitLab will detect it
automatically and run the steps defined once we push our code:
```yaml
image: java:8
stages:
- build
- deploy
2019-09-30 21:07:59 +05:30
2019-09-04 21:01:54 +05:30
before_script:
- chmod +x mvnw
2019-09-30 21:07:59 +05:30
2018-11-08 19:23:39 +05:30
build:
stage: build
script: ./mvnw package
artifacts:
paths:
- target/demo-0.0.1-SNAPSHOT.jar
production:
stage: deploy
script:
2020-07-28 23:09:34 +05:30
- curl --location "https://cli.run.pivotal.io/stable?release=linux64-binary&source=github" | tar zx
- ./cf login -u $CF_USERNAME -p $CF_PASSWORD -a api.run.pivotal.io
- ./cf push
2018-11-08 19:23:39 +05:30
only:
2020-07-28 23:09:34 +05:30
- master
2018-11-08 19:23:39 +05:30
```
2020-06-23 00:09:42 +05:30
We've used the `java:8` [Docker
2018-11-08 19:23:39 +05:30
image](../../docker/using_docker_images.md) to build
our application as it provides the up-to-date Java 8 JDK on [Docker
Hub](https://hub.docker.com/). We've also added the [`only`
2019-07-07 11:18:12 +05:30
clause](../../yaml/README.md#onlyexcept-basic)
2018-11-08 19:23:39 +05:30
to ensure our deployments only happen when we push to the master branch.
2021-01-29 00:20:46 +05:30
Because the steps defined in `.gitlab-ci.yml` require credentials to sign in to
CF, you'll need to add your CF credentials as
[environment variables](../../variables/README.md#predefined-environment-variables)
in GitLab CI/CD. To set the environment variables, navigate to your project's
**Settings > CI/CD**, and then expand **Variables**. Name the variables
2018-11-08 19:23:39 +05:30
`CF_USERNAME` and `CF_PASSWORD` and set them to the correct values.
2019-02-15 15:39:39 +05:30
![Variable Settings in GitLab](img/cloud_foundry_variables.png)
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
After set up, GitLab CI/CD deploys your app to CF at every push to your
repository's default branch. To review the build logs or watch your builds
running live, navigate to **CI/CD > Pipelines**.
2018-11-08 19:23:39 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
2021-01-29 00:20:46 +05:30
It's considered best practice for security to create a separate deploy user for
your application and add its credentials to GitLab instead of using a
developer's credentials.
2018-11-08 19:23:39 +05:30
To start a manual deployment in GitLab go to **CI/CD > Pipelines** then click
2021-01-29 00:20:46 +05:30
on **Run Pipeline**. After the app is finished deploying, it will display the
URL of your application in the logs for the `production` job like:
2018-11-08 19:23:39 +05:30
```shell
requested state: started
instances: 1/1
usage: 1G x 1 instances
urls: gitlab-hello-world-undissembling-hotchpot.cfapps.io
last uploaded: Mon Nov 6 10:02:25 UTC 2017
stack: cflinuxfs2
buildpack: client-certificate-mapper=1.2.0_RELEASE container-security-provider=1.8.0_RELEASE java-buildpack=v4.5-offline-https://github.com/cloudfoundry/java-buildpack.git#ffeefb9 java-main java-opts jvmkill-agent=1.10.0_RELEASE open-jdk-like-jre=1.8.0_1...
state since cpu memory disk details
#0 running 2017-11-06 09:03:22 PM 120.4% 291.9M of 1G 137.6M of 1G
```
You can then visit your deployed application (for this example,
2019-03-02 22:35:43 +05:30
`https://gitlab-hello-world-undissembling-hotchpot.cfapps.io/`) and you should
2018-11-08 19:23:39 +05:30
see the "Spring is here!" message.