I have created a Jenkins pipeline and shared the stages below. The Jenkins server is hosted on an EC2 instance and the MySQL database is hosted via AWS RDS.
stages {
stage("Build") {
steps {
sh 'php --version'
sh 'composer install'
sh 'composer --version'
}
}
stage("Unit test") {
steps {
sh "sed -i -e 's/\r//g' .env.example"
sh 'cp .env.example .env'
sh "sed -i 's/^DB_DATABASE=.*/&${DB_DATABASE}/' .env"
sh "sed -i 's/^DB_USERNAME=.*/&${DB_USERNAME}/' .env"
sh "sed -i 's|^DB_HOST=.*|&${DB_HOST}|' .env"
sh script: $/
sed -i 's/^\(DB_PASSWORD=*\).*$/\1"${DB_PASSWORD}"/' .env
sed -i 's/^\(APP_ENV=*\).*$/\1"testing"/' .env
/$
sh 'php artisan migrate'
sh 'php artisan test'
}
}
The problem is that while making a build getting the below error:
php artisan migrate
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection timed out (SQL: select * from information_schema.tables where table_schema = **** and table_name = migrations and table_type = 'BASE TABLE')
Reason for this is that Jenkins is picking up wrong previous .env value for DB_HOST but inside .env DB_HOST it has the correct value and php artisan migrate
also works from terminal.
Already executed commands to clear application cache, config:cache etc via terminal.
How to resolve this from Jenkins? Please help.
CodePudding user response:
finally found the solution.
Make sure that the variable name in Jenkinsfile is not similar to the key name of .env.
In my case it was picking up the Jenkinsfile variable name matching up with the .env key instead of the value that I passed.
So, change the DB_HOST
in Jenkinsfile to DIFF_DB_HOST
and it's working fine.