Skip to content

Instantly share code, notes, and snippets.

@gavvvr
Last active November 20, 2023 12:00
Show Gist options
  • Save gavvvr/651e6915db3c5b37068cf2dadcae34bc to your computer and use it in GitHub Desktop.
Save gavvvr/651e6915db3c5b37068cf2dadcae34bc to your computer and use it in GitHub Desktop.
Simple text file templates using Jenkins pipeline
// Just an example, for Rails config better use embedded ERB
final DATABASE_YAML_TEMPLATE = '''
test:
adapter: oracle_enhanced
database: app_db
username: ${user}
password: ${password}
'''
final CFG_PATH = 'config/database.yml'
def renderTemplate(template, bindings) {
bindings.inject(template) { t, k, v -> t.replace("\${" + k + "}", v) }
}
pipeline {
agent any
environment {
APP_DB = credentials('app_db')
}
stages {
stage('Test') {
steps {
writeFile file: CFG_PATH,
text: renderTemplate(DATABASE_YAML_TEMPLATE, ['user': env.APP_DB_USR, 'password': env.APP_DB_PSW])
sh 'bundle exec rspec'
}
}
}
post {
always {
sh "rm -f ${CFG_PATH}"
}
}
}
@wizard-it
Copy link

Have saved my day! thanks

@fillanes
Copy link

fillanes commented Nov 1, 2023

Thanks a lot!! Really helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment