I have been trying gitlab ci/cd for a spring-boot app that uses hibernate. But unfortunately my gitlab ci/cd build phase gets failed. I need to know how the hibernate connection string which is included in application.properties file to be included in .gitlab-ci.yml
spring.datasource.url= jdbc:mysql://my_ip:3306/db_test?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username= db_user
spring.datasource.password= password
And this is my .gitlab-ci.yml
image: docker:latest
services:
- docker:dind
stages:
- build
- package
- deploy
build:
image: java:8
stage: build
script:
- ./mvnw package
artifacts:
paths:
- target/unecast-0.0.1-SNAPSHOT.jar
only:
- development
package:
stage: package
script:
- docker build -t registry.gitlab.com/username/application .
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker push registry.gitlab.com/username/application
only:
- development
deploy_staging:
stage: deploy
script:
- apk upgrade && apk update
- apk add openssh-client
- apk add sshpass
- sshpass -p "$STAGING_SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $STAGING_SERVER_USER@$STAGING_SERVER docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- sshpass -p "$STAGING_SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $STAGING_SERVER_USER@$STAGING_SERVER docker pull registry.gitlab.com/username/application
- sshpass -p "$STAGING_SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $STAGING_SERVER_USER@$STAGING_SERVER "docker container stop unecast_dev_api && docker container rm unecast_dev_api || true"
- sshpass -p "$STAGING_SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $STAGING_SERVER_USER@$STAGING_SERVER docker run --name unecast_dev_api -p 8080:8080 -d registry.gitlab.com/username/application
only:
- development
I want to know how the values in application.properties could be included in .gitlab-ci.yml in order to connect my database.
You need not add application properties to gitlab yml. if you really want, define like this in your gitlab yml (not suggested)
variables:
SPRING_DATASOURCE_USERNAME: user1
Also , in your build step use mvn docker image rather than java docker image. Make sure to use correct version which your application supports.
Follow the blog here https://about.gitlab.com/2016/12/14/continuous-delivery-of-a-spring-boot-application-with-gitlab-ci-and-kubernetes/
Error Screenshot would help if you are still not able to resolve the issue.
In Gitlab, you can store your sensible value in secret variable.
As documented , there are two types of variables supported by GitLab:
Variable
: the Runner will create an environment variable named same as the variable key and set its value to the variable value. File
: the Runner will write the variable value to a temporary file and set the path to this file as the value of an environment variable named same as the variable key. In your case, I suggest to use File
type. In gitlab, go to your project > Settings > CI/CD > Variables and copy the content of your application.properties
in a variable. In my example, application_properties (Gitlab doesn't support .
in variable key name) :
In your job, you can find the path to this file in the value of the application_properties
environment variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.