我一直在为使用hibernate的spring-boot应用程序尝试gitlab ci / cd。但是不幸的是我的gitlab ci / cd构建阶段失败了。我需要知道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
这是我的.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
我想知道如何将application.properties中的值包含在.gitlab-ci.yml中以连接数据库。
答案 0 :(得分:0)
您无需将应用程序属性添加到gitlab yml。如果确实需要,可以在gitlab yml中定义这样(不建议)
variables:
SPRING_DATASOURCE_USERNAME: user1
此外,在构建步骤中,请使用mvn docker image而不是java docker image。确保使用您的应用程序支持的正确版本。
如果您仍然无法解决问题,则错误屏幕截图会有所帮助。
答案 1 :(得分:0)
在Gitlab中,您可以将明智的值存储在秘密变量中。
与documented一样,GitLab支持两种类型的变量:
File
:运行程序将创建一个与变量键相同的环境变量,并将其值设置为变量值。File
:运行程序将变量值写入临时文件,并将该文件的路径设置为与变量键相同的环境变量的值。对于您而言,我建议使用application.properties
类型。在gitlab中,转到项目>设置> CI / CD>变量,然后将.
的内容复制到变量中。在我的示例中,application_properties(Gitlab在变量键名中不支持application_properties
):
在工作中,您可以在class Question {
constructor(question, ansA, ansB, ansC, ansD, answer) {
this.question = question;
this.ansA = ansA;
this.ansB = ansB;
this.ansC = ansC;
this.ansD = ansD;
this.answer = answer;
};
checkAns(ansSelected, answer) {
if (ansSelected === answer) {
console.log('Well Done')
};
};
};
//Questions
var questionOne = new Question('Where is Creete?', 'Barcalona', 'Greece', 'Dubi', 'Ireland', 'Greece');
var questionTwo = new Question('How many times have Liverppool won the Champions Legue?', '1', '4', '6', '5', '6');
var questionThree = new Question('Where was the first Godfather in the mafia from?', 'Milan', 'Gunoa', 'Rome', 'Napoli', 'Napoli');
//Index of the array with the questions array
var i = 0;
const arrayQuestion = [questionOne, questionTwo, questionThree];
//Displaying the first index of the question array on load up
document.getElementById('question').innerHTML = arrayQuestion[i].question;
document.getElementById('rad1Label').innerHTML = arrayQuestion[i].ansA;
document.getElementById('rad2Label').innerHTML = arrayQuestion[i].ansB;
document.getElementById('rad3Label').innerHTML = arrayQuestion[i].ansC;
document.getElementById('rad4Label').innerHTML = arrayQuestion[i].ansD;
const submit = document.getElementById('submit');
submit.addEventListener("click", check);
function check() {
var rad1 = document.getElementById('rad1').checked;
var rad2 = document.getElementById('rad2').checked;
var rad3 = document.getElementById('rad3').checked;
var rad4 = document.getElementById('rad4').checked;
//Checks which button is selected
if (rad1 == true) {
alert('Bingo1');
}
else if(rad2 == true){
alert('Bingo2');
}
else if(rad3 == true){
alert('Bingo3');
}
else if(rad4 == true){
alert('Bingo4');
}
};
//Next button which cycles through the array and show the current question.
//With if statement to catch out of bound error
const n = document.getElementById('next');
n.addEventListener("click", next);
function next() {
i++;
if (i === arrayQuestion.length) {
i = 0;
};
document.getElementById('question').innerHTML = arrayQuestion[i].question;
document.getElementById('rad1Label').innerHTML = arrayQuestion[i].ansA;
document.getElementById('rad2Label').innerHTML = arrayQuestion[i].ansB;
document.getElementById('rad3Label').innerHTML = arrayQuestion[i].ansC;
document.getElementById('rad4Label').innerHTML = arrayQuestion[i].ansD;
};
//Function to go back to previouse question
//With if statement to catch out of bound error
const p = document.getElementById('previous')
p.addEventListener("click", prev)
function prev() {
i--;
if (i === -1) {
i = arrayQuestion.length - 1;
};
document.getElementById('question').innerHTML = arrayQuestion[i].question;
document.getElementById('rad1Label').innerHTML = arrayQuestion[i].ansA;
document.getElementById('rad2Label').innerHTML = arrayQuestion[i].ansB;
document.getElementById('rad3Label').innerHTML = arrayQuestion[i].ansC;
document.getElementById('rad4Label').innerHTML = arrayQuestion[i].ansD;
};
环境变量的值中找到此文件的路径。