diff --git a/Jenkinsfile b/Jenkinsfile index 0a878bc..465f46c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -74,8 +74,8 @@ pipeline { stage('Backend') { steps { dir(path: 'src/backend') { - sh "APP_SECRET=`php8.0 -r 'echo md5(\"branch=${BRANCH_NAME};\");'` && sed -i \"s/APP_SECRET=.*/APP_SECRET=${APP_SECRET}/g\" .env.test" - sh "sed -i \"s+DATABASE_URL=.*+DATABASE_URL=${DatabaseUrlTesting}+g\" .env.test" + sh "echo `php8.0 -r 'echo md5(\"branch=${BRANCH_NAME};\");'` | php8.0 ../../tools/putenv/putenv.php .env.test APP_SECRET" + sh "php8.0 ../../tools/putenv/putenv.php .env.test DATABASE_URL \"${DatabaseUrlTesting}\"" sh 'php8.0 /usr/local/bin/composer dump-env test' sh 'php8.0 bin/console doctrine:schema:update --force'; } @@ -123,8 +123,8 @@ pipeline { stage('Backend') { steps { dir(path: 'src/backend') { - sh "APP_SECRET=`php8.0 -r 'echo md5(\"branch=${BRANCH_NAME};\");'` && sed -i \"s/APP_SECRET=.*/APP_SECRET=${APP_SECRET}/g\" .env" - sh "sed -i \"s+DATABASE_URL=.*+DATABASE_URL=${DatabaseUrl}+g\" .env" + sh "echo `php8.0 -r 'echo md5(\"branch=${BRANCH_NAME};\");'` | php8.0 ../../tools/putenv/putenv.php .env APP_SECRET" + sh "php8.0 ../../tools/putenv/putenv.php .env DATABASE_URL \"${DatabaseUrl}\"" sh 'php8.0 /usr/local/bin/composer dump-env prod' } @@ -189,6 +189,8 @@ pipeline { sh 'scp -r ./* web@fufle.net:${ProjectPath}/backend/' sh 'scp -r ./.env web@fufle.net:${ProjectPath}/backend/.env' sh 'ssh web@fufle.net chmod 777 ${ProjectPath}/backend/var -R' + sh 'ssh web@fufle.net "cd ${ProjectPath}/backend; php8.0 bin/console doctrine:schema:update --force"' + sh 'ssh web@fufle.net "cd ${ProjectPath}/backend; php8.0 bin/console lexik:jwt:generate-keypair --skip-if-exists"' } } diff --git a/tools/putenv/env b/tools/putenv/env new file mode 100644 index 0000000..d3123e4 --- /dev/null +++ b/tools/putenv/env @@ -0,0 +1,5 @@ +name=value +name2="value 2" +#name3=value3 +name4 = value4 +name5 = "value 5" \ No newline at end of file diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php new file mode 100644 index 0000000..1f0833b --- /dev/null +++ b/tools/putenv/putenv.php @@ -0,0 +1,85 @@ +file = $argv[1]; + $this->key = $argv[2]; + $this->value = $argv[3]; + break; + + case 3: + $this->file = $argv[1]; + $this->key = $argv[2]; + $this->value = fgets(STDIN); + break; + + default: + echo "usage: + php8.0 putenv.php filename key value + echo value | php8.0 putenv filename key"; + throw new Exception("invalid params"); + } + if (!file_exists($this->file)) { + throw new Exception('File not found'); + } + $changed = 0; + $lineWasEmpty = false; + $key = $this->key; + $value = $this->value; + if (strpos($value, ' ') !== false) { + $value = '"' . addslashes($value) . '"'; + } + $this->content = file($this->file); + foreach ($this->content as $l => $line) { + $this->content[$l] = $line = trim($line); + if (strlen($line) < 1) { + if ($lineWasEmpty) { + $this->content[$l] = null; + } else { + $lineWasEmpty = true; + } + continue; + } + $lineWasEmpty = false; + if (!str_starts_with($line, $this->key)) { + continue; + } + $next = trim(substr($line, strlen($this->key))); + if (strlen($next) === 0 || $next[0] !== '=') { + continue; + } + $this->content[$l] = $key . '=' . $value . \PHP_EOL; + $changed++; + } + if ($changed === 0) { + $this->content[] = $key . '=' . $value; + } else if ($changed > 1) { + echo "Key $key changed $changed times\n"; + } + $content = $this->content; + $this->content = []; + foreach ($content as $line) { + if ($line !== null) { + $this->content[] = $line; + } + } + $content = implode(PHP_EOL, $this->content); + file_put_contents($this->file, $content); + } +} + +new PutEnv();