简体   繁体   English

如何在.gitlab-ci.yml中将application.properties中的休眠连接字符串包括在内

[英]How to include hibernate connection string in application.properties into .gitlab-ci.yml

I have been trying gitlab ci/cd for a spring-boot app that uses hibernate. 我一直在尝试gitlab ci / cd来使用Hibernate的spring-boot应用程序。 But unfortunately my gitlab ci/cd build phase gets failed. 但是不幸的是我的gitlab ci / cd构建阶段失败了。 I need to know how the hibernate connection string which is included in application.properties file to be included in .gitlab-ci.yml 我需要知道如何将application.properties文件中包含的休眠连接字符串包含在.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 这是我的.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. 我想知道如何将application.properties中的值包含在.gitlab-ci.yml中以连接数据库。

You need not add application properties to gitlab yml. 您无需将应用程序属性添加到gitlab yml。 if you really want, define like this in your gitlab yml (not suggested) 如果您确实需要,可以在gitlab yml中这样定义(不建议)

variables:
  SPRING_DATASOURCE_USERNAME: user1

Also , in your build step use mvn docker image rather than java docker image. 另外,在构建步骤中,请使用mvn docker image而不是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/ 在这里关注博客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. 在Gitlab中,您可以将明智的值存储在秘密变量中。

As documented , there are two types of variables supported by GitLab: 文档所述 ,GitLab支持两种类型的变量:

  • Variable : the Runner will create an environment variable named same as the variable key and set its value to the variable value. Variable :运行程序将创建一个与变量键相同名称的环境变量,并将其值设置为变量值。
  • 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. File :Runner将变量值写入一个临时文件,并将此文件的路径设置为与变量键相同的环境变量的值。

In your case, I suggest to use File type. 对于您的情况,我建议使用File类型。 In gitlab, go to your project > Settings > CI/CD > Variables and copy the content of your application.properties in a variable. 在gitlab中,转到项目>设置> CI / CD>变量,然后将application.properties的内容复制到变量中。 In my example, application_properties (Gitlab doesn't support . in variable key name) : 在我的示例中,application_properties(Gitlab在变量键名中不支持. ):

在此处输入图片说明

In your job, you can find the path to this file in the value of the application_properties environment variable. 在您的工作中,您可以在application_properties环境变量的值中找到此文件的路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在非 Spring 应用程序的 application.properties 中从 (.gitlab-ci.yml) 设置变量? - How to set variables from (.gitlab-ci.yml) in application.properties in non Spring application? ci yaml文件在application.properties中设置的不同连接字符串 - Different connection string in application.properties set by ci yaml file 我如何在gitlab-ci工作中添加计时器以使其在gitlab-ci.yml中停止1分钟 - How do I add a timer to my gitlab-ci job to make it stop within 1 minute in gitlab-ci.yml 为简单的java项目配置gitlab-ci.yml - Configuring gitlab-ci.yml for simple java project 从gitlab-ci.yml启动/停止远程服务器上的tomcat - Start/stop tomcat on remote server from gitlab-ci.yml 带有 maven 的 gitlab CI/CD 不会在 application.properties 中设置环境变量 - gitlab CI/CD with maven doesn't setup environment variables in application.properties 激活 Gitlab 以构建项目并运行测试的 .gitlab-ci.yml 文件应该是什么? - What should be the .gitlab-ci.yml file that activates Gitlab to build a project and run tests? 在 GitLab CICD 中注入 application.properties 值 - Inject application.properties value in GitLab CICD Spring Boot - 如何将 application.yml 属性定义为 application.properties - Spring Boot - How to define application.yml properties as application.properties 如何在spring application.yml或application.properties中使用特殊字符($)作为属性键 - How to use special character ($) in spring application.yml or application.properties as a property key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM