Skip to content

Instantly share code, notes, and snippets.

@swsnr
Last active September 2, 2019 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swsnr/3658ee94b9f373fbf223ea1014260169 to your computer and use it in GitHub Desktop.
Save swsnr/3658ee94b9f373fbf223ea1014260169 to your computer and use it in GitHub Desktop.
withYarn SBT Pipeline Step
#!/bin/bash
set -e
# The keyring we use to verify the signature of our download
keyring_file="$1"
# The version of yarn to install
version="$2"
# The directory install yarn versions to
basedir="$HOME/.yarn/versions/"
yarndir="$HOME/.yarn/versions/yarn-v$version"
# The URL to download yarn from
url="https://yarnpkg.com/downloads/$version/yarn-v$version.tar.gz"
# Check whether yarn is installed and working
actual_version="$("$yarndir/bin/yarn" --version 2>/dev/null || echo '')"
# Exit if that is the case; we don't need to install anything
[ "$actual_version" == "$version" ] && exit 0
# Working directories for download and installation
downloaddir="$(mktemp -d -t "yarn-v$version.tar.gzXXXXXX")"
installdir="$(mktemp -d -t "yarn-v${version}XXXXXX")"
function cleanup() {
rm -rf "$downloaddir"
rm -rf "$installdir"
}
trap cleanup EXIT
# Remove potential remains of partial installation
rm -rf "$yarndir"
# Download tarball and signature
tarball="${downloaddir}/yarn-v${version}.tar.gz"
signature="${downloaddir}/yarn-v${version}.tar.gz.asc"
curl --fail -L -o "$tarball" "$url"
curl --fail -L -o "$signature" "$url.asc"
# Verify the signature
gpgv --keyring "$keyring_file" "$signature" "$tarball"
# Extract tarball
tar -xzf "$tarball" -C "$installdir"
# And move to proper install dir
mkdir -p "$basedir"
mv -T "$installdir/yarn-v$version" "$yarndir"
#!/usr/bin/groovy
/**
* Use the given Yarn version in the contained block.
*
* @param version The version to use
* @param block The block to run
*/
def call(String version, Closure block) {
def install = "${pwd tmp: true}/install.bash"
writeFile file: install, text: (libraryResource('com/acme/yarn/install.bash'))
def keyring = "${pwd tmp: true}/keyring.gpg"
// Use
// wget -qO- https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --no-default-keyring --keyring ./keyring.gpg --import
// to generate this keyring file used to verify the yarn download.
def keyringdata = libraryResource resource: 'com/acme/yarn/keyring.gpg', encoding: "Base64"
writeFile file: keyring, text: keyringdata, encoding: "Base64"
sh label: 'install-yarn', script: "bash \"${install}\" \"${keyring}\" \"${version}\""
def path = sh label: 'yarn-path', returnStdout: true,
script: "echo -n \"\${HOME}/.yarn/versions/yarn-v${version}/bin/:\${PATH}\""
withEnv(["PATH=${path}"], block)
}
@swsnr
Copy link
Author

swsnr commented Aug 9, 2019

Use like this in a Jenkins file:

@Library('acme-library') _
pipeline {
    stages {
        stage('Compile') {
            steps {
                // Provided by the nodejs plugin and global tool configuration
                nodejs('Latest LTS') {
                    withYarn('1.17.3') {
                        sh label: 'yarn', script: 'yarn build'
                        sh label: 'yarn', script: 'yarn test'
                    }
                }
            }
        }
    }
}

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