Skip to content

Commit

Permalink
Add customized/simpler site publish mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
jdillon committed Jul 18, 2018
1 parent c787f0b commit 70c35ce
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 24 deletions.
51 changes: 27 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@

<distributionManagement>
<site>
<id>github-project-site</id>
<url>gitsite:git@github.com/groovy/gmaven.git</url>
<id>github-site</id>
<url>scm:git:ssh://git@github.com/groovy/gmaven.git</url>
</site>
</distributionManagement>

Expand Down Expand Up @@ -276,27 +276,6 @@
</modules>

<build>
<extensions>
<!-- Configure gitsite wagon for site deployment https://github.com/stephenc/wagon-gitsite -->
<extension>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.8.1</version>
</extension>

<extension>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-manager-plexus</artifactId>
<version>1.8.1</version>
</extension>

<extension>
<groupId>com.github.stephenc.wagon</groupId>
<artifactId>wagon-gitsite</artifactId>
<version>0.4.1</version>
</extension>
</extensions>

<pluginManagement>
<plugins>
<plugin>
Expand Down Expand Up @@ -559,7 +538,31 @@
<id>site-stage</id>

<build>
<defaultGoal>site:site site:stage</defaultGoal>
<defaultGoal>clean install site:site site:stage</defaultGoal>
</build>
</profile>

<profile>
<id>site-publish</id>

<build>
<defaultGoal>groovy:execute</defaultGoal>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<configuration>
<source>${project.basedir}/src/site/publish.groovy</source>
<properties>
<!--<dryRun>true</dryRun>-->
<!--<gitBranch>gh-pages</gitBranch>-->
</properties>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
Expand Down
111 changes: 111 additions & 0 deletions src/site/publish.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// Helper to checkout, update, commit and push Maven-generated site content for GH-Pages.
//

// FIXME: ATM only supports github SSH repository urls
// TODO: allow configuration to be passed in from pom

boolean dryRun = Boolean.parseBoolean(properties.getProperty('dryRun', 'false'))
log.info "Dry run: $dryRun"

def siteUrl = project?.distributionManagement?.site?.url
log.info "Site URL: $siteUrl"
assert siteUrl

def gitRepository = siteUrl - 'scm:git:ssh://git@github.com/'
log.info "GIT repository: $gitRepository"

def gitUrl = "git@github.com:$gitRepository"
log.info "GIT URL: $gitUrl"

def gitBranch = properties.getProperty('gitBranch', 'gh-pages')
log.info "GIT Branch: $gitBranch"

def targetDir = new File(project.build.directory as String)
log.info "Target directory: $targetDir"

def stagingDir = new File(targetDir, 'staging')
log.info "Staging directory: $stagingDir"
assert stagingDir.exists()

def checkoutDir = new File(targetDir, 'publish-checkout')
log.info "Checkout directory: $checkoutDir"
if (checkoutDir.exists()) {
ant.delete(dir: checkoutDir)
}
ant.mkdir(dir: checkoutDir)

// helper to run a task surrounded by snippet markers
def snippet = { Closure task ->
println '----8<----'
task.run()
println '---->8----'
}

log.info "Cloning $gitUrl ($gitBranch branch) to: $checkoutDir"
snippet {
ant.exec(executable: 'git', dir: checkoutDir) {
arg(value: 'clone')
arg(value: '--branch')
arg(value: gitBranch)
arg(value: gitUrl)
arg(value: '.')
}
}

log.info "Removing existing content from $checkoutDir"
ant.exec(executable: 'git', dir: checkoutDir, failonerror: true) {
arg(value: 'rm')
arg(value: '-r')
arg(value: '--quiet')
arg(value: '.')
}

log.info "Copying generated content to: $checkoutDir"
ant.copy(todir: checkoutDir) {
fileset(dir: stagingDir) {
include(name: '**')
}
}

log.info 'Adding changed files'
ant.exec(executable: 'git', dir: checkoutDir, failonerror: true) {
arg(value: 'add')
arg(value: '.')
}

log.info 'Checking status'
ant.exec(executable: 'git', dir: checkoutDir, failonerror: true, outputproperty: 'git-status') {
arg(value: 'status')
arg(value: '--short')
}
def status = ant.project.properties.'git-status'.trim()

if (status) {
snippet { println status }

log.info 'Committing changes'
snippet {
ant.exec(executable: 'git', dir: checkoutDir, failonerror: true) {
arg(value: 'commit')
arg(value: '-m')
arg(value: 'Maven-generated site content refresh')
}
}

if (!dryRun) {
log.info 'Pushing changes'
snippet {
ant.exec(executable: 'git', dir: checkoutDir, failonerror: true) {
arg(value: 'push')
arg(value: 'origin')
arg(value: gitBranch)
}
}
}
}
else {
log.info 'No changes detected'
}

log.info 'Done'

0 comments on commit 70c35ce

Please sign in to comment.