Building a Grails CI pipeline with Jenkins declarative pipelines
Continuous integration gives a Grails team fast, repeatable feedback whenever code changes. Jenkins can compile the application, execute Spock and JUnit tests, publish quality reports, and package a deployable artefact without requiring a developer to repeat those steps manually.
Australian development teams often work across Sydney, Melbourne, Brisbane and Perth, so build times and notifications must account for Australian Eastern Standard Time and daylight-saving changes. A Jenkins controller hosted in an Australian region can also reduce latency when artefacts are stored in the AWS Sydney region, ap-southeast-2.
Jenkins declarative pipelines express this workflow through a pipeline block containing stages, steps, environment settings and post-build actions. Because the Jenkinsfile uses Groovy syntax, it fits naturally alongside a Grails application written in Groovy, while remaining readable for Java developers joining the project.
The pipeline should use the Gradle wrapper committed with the repository, a fixed JDK version and credentials supplied through Jenkins rather than embedded in source control. Start with build, test and package stages, then add deployment only after the build has proved reliable.
Model the build as repeatable stages
A useful Jenkinsfile defines a clear contract between the repository and the build agent. Each stage should have one purpose, produce an observable result and fail the build when its quality gate is not met.
pipeline {
agent any
tools {
jdk 'Java17'
}
options {
timestamps()
disableConcurrentBuilds()
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '20'))
}
environment {
CI = 'true'
APP_ENVIRONMENT = 'test'
}
stages {
stage('Check out source') {
steps {
checkout scm
sh './gradlew --version'
}
}
stage('Build and test') {
steps {
sh './gradlew clean test'
}
}
stage('Package application') {
steps {
sh './gradlew assemble'
archiveArtifacts artifacts: 'build/libs/*',
fingerprint: true,
allowEmptyArchive: true
}
}
}
post {
always {
junit testResults: 'build/test-results/test/*.xml',
allowEmptyResults: true
archiveArtifacts artifacts: 'build/reports/tests/test/**/*',
allowEmptyArchive: true
}
failure {
echo 'The build failed. Notify the owning team.'
}
}
}
The agent directive selects the machine that executes the pipeline, while tools ensures that every stage sees the configured Java installation. The named JDK must exist in Jenkins global tool configuration; otherwise, the build stops before it can produce a meaningful Grails compilation error.
The package stage stores one immutable build output rather than rebuilding separately for each environment. The fingerprint option lets Jenkins trace that artefact back to a commit. Release promotion should therefore move this same artefact through test, staging and production instead of running a fresh build on the target server.
Prepare Grails and Jenkins for automation
The Jenkins controller needs a stable integration with the Git repository and enough storage for both the Grails application and Jenkins metadata. A Linux build agent is a straightforward choice for Gradle-based Grails projects, although the same pipeline can run on Windows with bat steps instead of sh.
Application credentials belong in the Jenkins credential store. Database passwords, API tokens and signing keys can then be injected for the duration of a stage with the Credentials Binding plugin. This avoids exposing secrets in the Jenkinsfile, build logs or Git history.
Applications that select a database according to a tenant or environment need configuration that changes safely during CI. The guidance on dynamic data source configuration is useful when the pipeline must select test data sources without modifying committed configuration files.
A Gradle cache mounted on the agent can reduce dependency downloads, but it must not become a second source of application state. Keep build outputs and test reports inside the Jenkins workspace, while caching only downloaded dependencies. For older Grails releases that still use the grails command, replace the wrapper commands with the equivalent grails clean test and grails assemble operations.
Add testing and quality gates
Grails applications commonly combine unit tests written with Spock or JUnit with integration tests that exercise controllers, services and database access. The test stage should use the same Gradle tasks developers run locally so that CI results reflect an ordinary development workflow.
The JUnit publishing step makes failed specifications visible in Jenkins even when the console output is lengthy. Published reports should be retained as build artefacts, giving developers a downloadable record after a worker has been cleaned. Coverage can be added through Gradle plugins such as Jacoco, with a quality threshold enforced only after the team has agreed on a realistic baseline.
Static analysis, dependency checks and secret scanning can run as separate steps without obscuring the core test result. A dependency vulnerability should block a production release, while an informational audit report may simply be archived for review. Keeping these concerns visible in the declarative pipeline makes the release policy understandable to people who did not write the Jenkinsfile.
Route behaviour deserves a smoke test as well as a controller unit test. A deployed Grails application can pass domain tests while returning the wrong status for a public URL. Checking the application’s SEO-friendly route mappings helps catch broken links before search engines or Australian customers encounter them.
Use a practical Jenkinsfile checklist
Keep the file concise enough for code review, but include the operational details needed to reproduce a build. The following checks separate routine build requirements from controls that protect releases.
Build and test
- Use the committed Gradle wrapper and a Jenkins-configured JDK.
- Run unit and integration tasks in clearly separated stages.
- Publish JUnit results and retain the generated test report.
- Apply dependency caching without caching application state.
Release and recovery
- Protect the main branch and require successful status checks.
- Request manual approval before a production deployment.
- Promote one fingerprinted artefact between environments.
- Remove old workspaces while retaining the required build history.
The separation matters because a fast feedback loop and a controlled release process have different risks. Developers can merge a small change as soon as compilation and tests pass, while deployment remains subject to broader checks and human approval. The pipeline should make that distinction explicit rather than hiding it inside a long shell script.
Operate the pipeline and promote releases
A build server still needs maintenance. Review failed builds, distinguish an application defect from an unstable test or agent problem, and remove dependencies or caches that no longer receive updates. Jenkins timestamps should be displayed in UTC for unambiguous logs, while notifications to an Australian team can use AEST or AEDT as appropriate.
Artefacts destined for an AWS deployment can be published to an S3 bucket in ap-southeast-2, the Sydney region. The deployment job should verify the artefact fingerprint, use environment-specific credentials and retain a previous version for rollback. Australian English conventions may also influence names such as release-artefact and deployment-authorisation, although the Jenkins job identifier should match the configuration used by the platform team.
Access to production, artefact repositories and notification services should follow least-privilege permissions. Record who approved a release, which commit produced the artefact and which environment received it. This audit trail remains valuable when a Grails application serves customers across Australian states and international markets.
Create the pipeline in a feature branch today, configure the JDK and source credentials, and run one clean test build before enabling automatic deployment.