Skip to content

Commit

Permalink
Merge pull request #132 from DontShaveTheYak/develop
Browse files Browse the repository at this point in the history
Release v0.10.0
  • Loading branch information
shadycuz authored Oct 4, 2021
2 parents 8f586e9 + 758552b commit 1cd5cde
Show file tree
Hide file tree
Showing 24 changed files with 1,424 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .groovylintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"extends": "recommended",
"rules": {
"BlockStartsWithBlankLine": {
"enabled": false
},
"CompileStatic": {
"enabled": false
},
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ repositories {
dependencies {
// Use the latest Groovy version for building this library
compileOnly('org.codehaus.groovy:groovy-all:3.0.9')
compileOnly('org.jenkins-ci.main:jenkins-core:2.312')
compileOnly('org.jenkins-ci.main:jenkins-core:2.313')
compileOnly('com.cloudbees:groovy-cps:1.32')
}

Expand Down
174 changes: 174 additions & 0 deletions jobs/system/os/path/path_example.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
@Library('jenkins-std-lib')

import org.dsty.system.os.Path

node() {

// Get a path to the current workspace.
Path workspace = Path.workspace()

println("Workspace Path is ${workspace}")

// Make sure the workspace is empty
workspace.deleteContents()

stage('Working with directories') {

// Create a directory to hold our tests
Path dirA = new Path("${workspace}/dirA")

// dirA does not exist yet, we need to create it
dirA.mkdirs()

// We can create a sub directory using the full path or using these methods
Path dirB = dirA.withSuffix('/dirB')
dirB.mkdirs()

Path dirB2 = dirA.child('dirB2')
dirB2.mkdirs()

// We can also create a temporary directory
dirB.withTempDir { Path tmpDir ->

// get tmpDir's parent
Path parent = tmpDir.getParent()

// Verify that dirB is the parent
if (parent != dirB) {
error("${dirB} is not the parent of ${tmpDir}.")
}

}

// Make sure the tmpDir was deleted
List<Path> contentsDirB = dirB.list()
if (contentsDirB) {
error("${dirB} should be empty but contains: ${contentsDirB}")
}

// delete() will fail because dirA is not empty
dirA.deleteRecursive() // deletes the contents and then deletes dirA

// Lets verify all of our directories were deleted
if (dirA.exists() || dirB.exists() || dirB2.exists()) {
error('Should have deleted all directories.')
}

String someDir = "${workspace}/someDir"

dir(someDir) {

// Get a Path to the current working directory
Path cwd = Path.cwd()

// Verify cwd is the directory we are in.
if (cwd != someDir) {
error("Should be in ${someDir} but acutally in ${cwd}")
}
}

}

stage('Working with files') {

// Create a Path for a future file inside of the workspace
Path testFile = workspace.child('hello_world.txt')

// We can create the file with .touch or just write to it
testFile.write(env.BUILD_ID)

// Read the contents back into a string
String contents = testFile.read()

//verify the contents
if (contents != env.BUILD_ID) {
error("${contents} doest no equal ${env.BUILD_ID}.")
}

// Create a new Path in the same directory as testFile
Path newFile = testFile.sibling('test.txt')

// Rename testFile to newFile
testFile.renameTo(newFile)

// Verify testFile is gone
if (testFile.exists()) {
error("${testFile} should not exist.")
}

// Verify newFile does exist
if (!newFile.exists()) {
error("${newFile} should exist.")
}

// Set the permissions on a file
int filePerms = 0755
newFile.chmod(filePerms)

// verify file permissions
if (newFile.mode() != filePerms) {
error("${newFile} should have ${filePerms} not ${newFile.mode()} permissions.")
}

// Get the size in bytes
println(newFile.length())

}



// All zip/unzip methods have a tar/untar equivalent
stage('Working with zip/tar') {

// We will use this later
Path archive

// We can choose the name of a tmpDir by passing a path
Path zipTest = workspace.child('zipTest')

// Create a directory to hold our tests
workspace.withTempDir(zipTest) { Path tmpDir ->

// Create a Path to our new file
Path testFile = tmpDir.child('test.txt')

testFile.touch()

// Create a Path to a zip file
archive = workspace.child('testFile.zip')

// zip up our tmpDir
tmpDir.zip(archive)
}

// verify our tmpDir is gone
if (zipTest.exists()) {
error("The path ${zipTest} should have been deleted.")
}

// verify our archive exists
if (!archive.exists()) {
error("The path ${archive} should have been created.")
}

// Unzip the archive into the workspace
archive.unzip(workspace)

// verify zipTest was archived correctly

if (!zipTest.exists()) {
error("${zipTest} should exist again.")
}

// get the test.txt file
Path txtFile = zipTest.list().find { Path childPath ->
childPath.getName() == 'test.txt'
}

// verify file was found
if (!txtFile || !txtFile.exists()) {
error("${txtFile} should exist.")
}
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* groovylint-disable DuplicateNumberLiteral, DuplicateStringLiteral */
@Library('jenkins-std-lib')
import org.dsty.bash.BashClient
import org.dsty.bash.ScriptError
import org.dsty.bash.Result
import org.dsty.system.os.shell.Bash
import org.dsty.system.os.shell.ExecutionException
import org.dsty.system.os.shell.Result

node() {
String msg
Result result
BashClient bash = new BashClient(this)
Bash bash = new Bash()

stage('Regular Bash') {
msg = 'Hello from Bash!'
Expand All @@ -33,11 +33,11 @@ node() {
// In the event of a non 0 exitCode an Exception
// is thrown. The exception will behave just like a normal Result.
// So it will also have stdOut, stdError, output and exitCode.
ScriptError exception = null
ExecutionException exception = null

try {
bash('RegularFakeCommand')
} catch (ScriptError e) {
} catch (ExecutionException e) {
exception = e
}

Expand Down Expand Up @@ -76,7 +76,7 @@ node() {
// ignoreErrors will not throw an exception.
// Instead is just returns the results after
// it encounters an error.
result = bash.ignoreErrors('fakecommand', true)
result = bash.ignoreErrors('fakecommand')

if ( !result.stdErr.contains('fakecommand: command not found') ) {
error('Command was found.')
Expand All @@ -90,31 +90,36 @@ node() {
error('Exited with wrong code.')
}

// By default ignoreErrors will ignore all errors and run
// the entire script before returning.

String script = '''\
fakeCommand
anotherFakeCommand
willNotRun
'''

// The false we are passing determines if the command output is to be
// displayed in the build console.
result = bash.ignoreErrors(script, false)
// The true we are passing determines if the command
// is run silently or not.
result = bash.ignoreErrors(script, true)

if ( !result.stdErr.contains('anotherFakeCommand') ) {
error('Should not stop on first error.')
// By default all bash commands will stop on
// the first error.
if ( result.stdErr.contains('willNotRun') ) {
error('Should have stopped on first error.')
}

// If you want to return on the first error,
// set failfast to true.
result = bash.ignoreErrors(script, false, true)
script = """\
moreFakeCommands
commandWillRun
"""

if ( result.stdErr.contains('anotherFakeCommand') ) {
error('Should stop on first error.')
// If you want to run all the commands, regardless of errors,
// Set failFast to false
bash.failFast = false
result = bash.ignoreErrors(script)

if ( !result.stdErr.contains('commandWillRun') ) {
error('Should not stop on first error.')
}

result = bash.ignoreErrors('exit 55', false)
result = bash.ignoreErrors('exit 55', true)

if ( result.exitCode != 55 ) {
error('Should capture the correct exitCode.')
Expand Down
39 changes: 39 additions & 0 deletions jobs/system/platform_example.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* groovylint-disable DuplicateMapLiteral, DuplicateStringLiteral, UnnecessaryGetter, UnusedVariable */
@Library('jenkins-std-lib')
import org.dsty.system.Platform
import org.dsty.system.os.shell.Shell

node() {
// Ignore this line its for catching CPS issues.
String cps = sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true)

// Get the current system type like WINDOWS or UNIX
String systemType = Platform.system()

List<String> validSystems = ['UNIX', 'WINDOWS', 'DARWIN']

// These tests are generic because I cant be certain where
// they will be run.
if (!validSystems.contains(systemType)) {
error("Should be a valid system but was ${systemType}")
}

// Get the current CPU architechture
String architecture = Platform.architecture()

List<String> validArchs = ['x86', 'x64', 'x86_x64', 'arm', 'arm64']

if (validArchs.contains(architecture)) {
error("Should be a valid architecture but was ${architecture}")
}

// Get a Shell for the current system
Shell shell = Platform.getShell()

// Make sure the shell works by writting a msg with it.
String msg = "Ran on ${architecture} ${systemType}"
shell("echo ${msg}")

// Ignore this line its for catching CPS issues.
cps = sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true)
}
2 changes: 2 additions & 0 deletions src/org/dsty/bash/BashClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import org.dsty.logging.LogClient

/**
* Bash Client
* @deprecated As of release 0.10.0, replaced by {@link org.dsty.system.os.shell.Bash org.dsty.system.os.shell.Bash}
*/
@Deprecated
class BashClient implements Serializable {

/**
Expand Down
2 changes: 2 additions & 0 deletions src/org/dsty/bash/Result.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import com.cloudbees.groovy.cps.NonCPS

/**
* Contains the results of a bash script execution.
* @deprecated As of release 0.10.0, replaced by {@link org.dsty.system.os.shell.Result org.dsty.system.os.shell.Result}
*/
@Deprecated
class Result implements Serializable {

/**
Expand Down
3 changes: 3 additions & 0 deletions src/org/dsty/bash/ScriptError.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package org.dsty.bash

/**
* Custom Exception thrown by the bash global variable.
* @deprecated As of release 0.10.0, replaced by
* {@link org.dsty.system.os.shell.ExecutionException org.dsty.system.os.shell.ExecutionException}
*/
@Deprecated
class ScriptError extends Exception {

/**
Expand Down
3 changes: 3 additions & 0 deletions src/org/dsty/bash/package-info.groovy
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/**
* Bash wrapper and helper utilities.
* @deprecated As of release 0.10.0, replaced by
* <a href="../system/os/shell/package-summary.html">org.dsty.system.os.shell</a>.
*/
@Deprecated
package org.dsty.bash
14 changes: 13 additions & 1 deletion src/org/dsty/jenkins/Build.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* groovylint-disable LineLength, UnnecessaryGetter */
package org.dsty.jenkins

import hudson.EnvVars

/**
* Returns the WorkFlowScript from the current build. This object has access
* to pipeline steps like <code>sh()</code>, <code>checkout()</code> and
Expand All @@ -19,7 +21,17 @@ Object getWorkFlowScript() {
* @return A Map of environment variables.
*/
Map<String, String> environmentVars() {
return getContext(hudson.EnvVars)
return getEnvironment()
}

/**
* Returns the {@link EnvVars} for the current build.
*
* @return The {@link EnvVars} object.
* @see hudson.EnvVars
*/
EnvVars getEnvironment() {
return getCurrentContext(EnvVars)
}

/**
Expand Down
Loading

0 comments on commit 1cd5cde

Please sign in to comment.