liquibase / liquibase-gradle-plugin

A Gradle plugin for Liquibase
Other
197 stars 57 forks source link

Run liquibase gradle command from console passing parameters #98

Closed agustin-tecso closed 2 years ago

agustin-tecso commented 2 years ago

Hi! I want to run update command from the console for update production database. I need to pass password with enviroment variable for security concerns. Gradle don't recognize any parameters of liquidbase, I have tried:

gradle -Dliquibase.changeLogFile=liquibase/config/changelog-master.xml update gradle --changeLogFile=liquibase/config/changelog-master.xml update gradle --changelog-file=liquibase/config/changelog-master.xml update

¿Is there any way to pass parameters from gradle to liquibase plugin? Put a production password in a file is not an option.

Thanks in advance!

stevesaliman commented 2 years ago

The simplest way to do this is with Gradle properties. The first part is to put properties in your build.gradle's liquibase configuration like this:

liquibase {
  activities {
    main {
      changeLogFile "${dbDir}/changelog.groovy"
      url project.ext.jdbcUrl
      username project.ext.jdbcUsername
      password project.ext.jdbcPassword
      logLevel 'info'
    }
  }
}

There are several ways to specify the values for those variables at runtime, including -P jdbcPassword=mypass. Gradle even has a way to set properties from environment variables, using specially named variables like ORG_GRADLE_PROJECT_jdbcPassword=mypass.

I can't resist a bit of shameless self promotion - the different ways of setting properties, and the order of precedence, is explained my Gradle Properties Plugin. You probably don't actually need the plugin itself, since all but 2 of the methods described in the documentation is built into Gradle itself, but it can come in handy in setups like this.

When I run Liquibase I use the plugin. The username for all environments is the same, so I put it in the gradle.properties file. The database URLs are different for each environment, so I put those in the gradle-.properties files. Passwords remain unset so we can specify them on the command line.