|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# |
| 4 | +# ci-build.sh - A script to build and/or release SciJava-based projects |
| 5 | +# automatically using a continuous integration |
| 6 | +# service. |
| 7 | +# |
| 8 | +# Required environment variables: |
| 9 | +# BUILD_OS - the operating system running the current build (e.g. macOS) |
| 10 | +# BUILD_REPOSITORY - the repository slug (org/repo) running the current build |
| 11 | + |
| 12 | +dir="$(dirname "$0")" |
| 13 | + |
| 14 | +success=0 |
| 15 | +checkSuccess() { |
| 16 | + # Log non-zero exit code. |
| 17 | + test $1 -eq 0 || echo "==> FAILED: EXIT CODE $1" 1>&2 |
| 18 | + |
| 19 | + # Record the first non-zero exit code. |
| 20 | + test $success -eq 0 && success=$1 |
| 21 | +} |
| 22 | + |
| 23 | +# Build Maven projects. |
| 24 | +if [ -f pom.xml ]; then |
| 25 | + echo ::group::"= Maven build =" |
| 26 | + echo |
| 27 | + echo "== Configuring Maven ==" |
| 28 | + |
| 29 | + # NB: Suppress "Downloading/Downloaded" messages. |
| 30 | + # See: https://stackoverflow.com/a/35653426/1207769 |
| 31 | + export MAVEN_OPTS="$MAVEN_OPTS -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" |
| 32 | + |
| 33 | + # Populate the settings.xml configuration. |
| 34 | + mkdir -p "$HOME/.m2" |
| 35 | + settingsFile="$HOME/.m2/settings.xml" |
| 36 | + customSettings=.ci/settings.xml |
| 37 | + if [ -f "$customSettings" ]; then |
| 38 | + cp "$customSettings" "$settingsFile" |
| 39 | + else |
| 40 | + cat >"$settingsFile" <<EOL |
| 41 | +<settings> |
| 42 | + <servers> |
| 43 | + <server> |
| 44 | + <id>scijava.releases</id> |
| 45 | + <username>$MAVEN_USER</username> |
| 46 | + <password>$MAVEN_PASS</password> |
| 47 | + </server> |
| 48 | + <server> |
| 49 | + <id>scijava.snapshots</id> |
| 50 | + <username>$MAVEN_USER</username> |
| 51 | + <password>$MAVEN_PASS</password> |
| 52 | + </server> |
| 53 | + <server> |
| 54 | + <id>sonatype-nexus-releases</id> |
| 55 | + <username>scijava-ci</username> |
| 56 | + <password>$OSSRH_PASS</password> |
| 57 | + </server> |
| 58 | + </servers> |
| 59 | +EOL |
| 60 | + cat >>"$settingsFile" <<EOL |
| 61 | + <profiles> |
| 62 | + <profile> |
| 63 | + <id>gpg</id> |
| 64 | + <activation> |
| 65 | + <file> |
| 66 | + <exists>$HOME/.gnupg</exists> |
| 67 | + </file> |
| 68 | + </activation> |
| 69 | + <properties> |
| 70 | + <gpg.keyname>$GPG_KEY_NAME</gpg.keyname> |
| 71 | + <gpg.passphrase>$GPG_PASSPHRASE</gpg.passphrase> |
| 72 | + </properties> |
| 73 | + </profile> |
| 74 | + </profiles> |
| 75 | +</settings> |
| 76 | +EOL |
| 77 | + fi |
| 78 | + |
| 79 | + # Determine whether deploying will be possible. |
| 80 | + deployOK= |
| 81 | + ciURL=$(mvn -q -Denforcer.skip=true -Dexec.executable=echo -Dexec.args='${project.ciManagement.url}' --non-recursive validate exec:exec 2>&1) |
| 82 | + |
| 83 | + if [ $? -ne 0 ]; then |
| 84 | + echo "No deploy -- could not extract ciManagement URL" |
| 85 | + echo "Output of failed attempt follows:" |
| 86 | + echo "$ciURL" |
| 87 | + else |
| 88 | + ciRepo=${ciURL##*/} |
| 89 | + ciPrefix=${ciURL%/*} |
| 90 | + ciOrg=${ciPrefix##*/} |
| 91 | + if [ ! "$SIGNING_ASC" ] || [ ! "$GPG_KEY_NAME" ] || [ ! "$GPG_PASSPHRASE" ] || [ ! "$MAVEN_PASS" ] || [ ! "$OSSRH_PASS" ]; then |
| 92 | + echo "No deploy -- secure environment variables not available" |
| 93 | + elif [ "$BUILD_REPOSITORY" != "$ciOrg/$ciRepo" ]; then |
| 94 | + echo "No deploy -- repository fork: $BUILD_REPOSITORY != $ciOrg/$ciRepo" |
| 95 | + else |
| 96 | + echo "All checks passed for artifact deployment" |
| 97 | + deployOK=1 |
| 98 | + fi |
| 99 | + fi |
| 100 | + |
| 101 | + # Install GPG on OSX/macOS |
| 102 | + if [ $BUILD_OS = 'macOS' ]; then |
| 103 | + HOMEBREW_NO_AUTO_UPDATE=1 brew install gnupg2 |
| 104 | + fi |
| 105 | + |
| 106 | + # Import the GPG signing key. |
| 107 | + keyFile=.ci/signingkey.asc |
| 108 | + if [ "$deployOK" ]; then |
| 109 | + echo "== Importing GPG keypair ==" |
| 110 | + mkdir -p .ci |
| 111 | + echo "$SIGNING_ASC" > "$keyFile" |
| 112 | + ls -la "$keyFile" |
| 113 | + gpg --batch --fast-import "$keyFile" |
| 114 | + checkSuccess $? |
| 115 | + fi |
| 116 | + |
| 117 | + # Run the build. |
| 118 | + BUILD_ARGS='-B -Djdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2"' |
| 119 | + if [ "$deployOK" ]; then |
| 120 | + echo |
| 121 | + echo "== Building and deploying master SNAPSHOT ==" |
| 122 | + mvn -Pdeploy-to-scijava $BUILD_ARGS deploy |
| 123 | + checkSuccess $? |
| 124 | + elif [ "$deployOK" -a -f release.properties ]; then |
| 125 | + echo |
| 126 | + echo "== Cutting and deploying release version ==" |
| 127 | + mvn -B $BUILD_ARGS release:perform |
| 128 | + checkSuccess $? |
| 129 | + echo "== Invalidating SciJava Maven repository cache ==" |
| 130 | + curl -fsLO https://raw.githubusercontent.com/scijava/scijava-scripts/master/maven-helper.sh && |
| 131 | + gav=$(sh maven-helper.sh gav-from-pom pom.xml) && |
| 132 | + ga=${gav%:*} && |
| 133 | + echo "--> Artifact to invalidate = $ga" && |
| 134 | + echo "machine maven.scijava.org" >"$HOME/.netrc" && |
| 135 | + echo " login $MAVEN_USER" >>"$HOME/.netrc" && |
| 136 | + echo " password $MAVEN_PASS" >>"$HOME/.netrc" && |
| 137 | + sh maven-helper.sh invalidate-cache "$ga" |
| 138 | + checkSuccess $? |
| 139 | + else |
| 140 | + echo |
| 141 | + echo "== Building the artifact locally only ==" |
| 142 | + mvn $BUILD_ARGS install javadoc:javadoc |
| 143 | + checkSuccess $? |
| 144 | + fi |
| 145 | + echo ::endgroup:: |
| 146 | +fi |
| 147 | + |
| 148 | +# Configure conda environment, if one is needed. |
| 149 | +if [ -f environment.yml ]; then |
| 150 | + echo ::group::"= Conda setup =" |
| 151 | + |
| 152 | + condaDir=$HOME/miniconda |
| 153 | + condaSh=$condaDir/etc/profile.d/conda.sh |
| 154 | + if [ ! -f "$condaSh" ]; then |
| 155 | + echo |
| 156 | + echo "== Installing conda ==" |
| 157 | + python_version=${python -V} |
| 158 | + python_version=${python_version:7:3} # get python version in the format like '2.7' |
| 159 | + if [ "${python_version}" = "2.7" ]; then |
| 160 | + wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh |
| 161 | + else |
| 162 | + wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh |
| 163 | + fi |
| 164 | + rm -rf "$condaDir" |
| 165 | + bash miniconda.sh -b -p "$condaDir" |
| 166 | + checkSuccess $? |
| 167 | + fi |
| 168 | + |
| 169 | + echo |
| 170 | + echo "== Updating conda ==" |
| 171 | + . "$condaSh" && |
| 172 | + conda config --set always_yes yes --set changeps1 no && |
| 173 | + conda update -q conda && |
| 174 | + conda info -a |
| 175 | + checkSuccess $? |
| 176 | + |
| 177 | + echo |
| 178 | + echo "== Configuring environment ==" |
| 179 | + condaEnv=ci-scijava |
| 180 | + test -d "$condaDir/envs/$condaEnv" && condaAction=update || condaAction=create |
| 181 | + conda env "$condaAction" -n "$condaEnv" -f environment.yml && |
| 182 | + conda activate "$condaEnv" |
| 183 | + checkSuccess $? |
| 184 | + |
| 185 | + echo ::endgroup:: |
| 186 | +fi |
| 187 | + |
| 188 | +# Execute Jupyter notebooks. |
| 189 | +if which jupyter >/dev/null 2>/dev/null; then |
| 190 | + echo ::group::"= Jupyter notebooks =" |
| 191 | + # NB: This part is fiddly. We want to loop over files even with spaces, |
| 192 | + # so we use the "find ... -print0 | while read $'\0' ..." idiom. |
| 193 | + # However, that runs the piped expression in a subshell, which means |
| 194 | + # that any updates to the success variable will not persist outside |
| 195 | + # the loop. So we suppress all stdout inside the loop, echoing only |
| 196 | + # the final value of success upon completion, and then capture the |
| 197 | + # echoed value back into the parent shell's success variable. |
| 198 | + success=$(find . -name '*.ipynb' -print0 | { |
| 199 | + while read -d $'\0' nbf; do |
| 200 | + echo 1>&2 |
| 201 | + echo "== $nbf ==" 1>&2 |
| 202 | + jupyter nbconvert --execute --stdout "$nbf" >/dev/null |
| 203 | + checkSuccess $? |
| 204 | + done |
| 205 | + echo $success |
| 206 | + }) |
| 207 | + echo ::endgroup:: |
| 208 | +fi |
| 209 | + |
| 210 | +exit $success |
0 commit comments