Skip to content

Instantly share code, notes, and snippets.

View ansig's full-sized avatar

Anders Sigfridsson ansig

View GitHub Profile
@ansig
ansig / recursive_get.py
Created March 9, 2018 18:17
Recursively make get requests to a certain depth
# -*- coding: utf-8 -*-
import requests
import timeit
from multiprocessing import Pool
from functools import partial
def get_result(url, depth=1):
r = requests.get(url)
@ansig
ansig / findRootCauseBuild.groovy
Last active March 23, 2018 09:19
Find root cause of a Jenkins build with a trampoline closure
def findRootCauseRun
findRootCauseRun = { def currentRun ->
def upstreamRun = getUpstreamCause(currentRun)?.getUpstreamRun()
if (upstreamRun == null) return currentRun
findRootCauseRun.trampoline(upstreamRun)
}
findRootCauseRun = findRootCauseRun.trampoline()
def getUpstreamCause(def run) {
run.getAction(hudson.model.CauseAction.class)?.findCause(hudson.model.Cause$UpstreamCause.class)
@ansig
ansig / decorator.py
Created March 3, 2018 12:05
Python decorators
from functools import wraps
def wrap(element):
def decorator(func):
@wraps(func)
def inner(*args, **kwargs):
return "<{0}>{1}</{0}>".format(element, func(*args, **kwargs))
return inner
return decorator
@ansig
ansig / gist:9c4086ac2eac984e585ae4019ee493c0
Created February 1, 2018 09:15
How to forcibly cancel Jenkins jobs that are hanging and cannot be cancelled
# To send cancel signal
<JENKINS>/job/<JOB>/<BUILD>/stop
# To send terminate signal
<JENKINS>/job/<JOB>/<BUILD>/term
# To send kill signal (will forcibly kill threads)
<JENKINS>/job/<JOB>/<BUILD>/kill
@ansig
ansig / sendMail.groovy
Last active September 16, 2020 15:44
Send a mail from Jenkins in Groovy Script Console
import javax.mail.Session
import javax.mail.Message
import javax.mail.Transport
import javax.mail.internet.MimeMessage
import javax.mail.internet.InternetAddress
def descriptor = Jenkins.instance.getDescriptor("hudson.tasks.Mailer")
Session session = descriptor.createSession();
@ansig
ansig / show-process-information.sh
Created September 12, 2017 06:23
A few utility commands to extract information about a running Unix process
#!/usr/bin/env bash
# Show all processes with trees
ps axf | less
# Show all available information for a process
ls -la /proc/[PID]
@ansig
ansig / update-index.sh
Created September 5, 2017 06:26
Git: add execute permissions on file in repo
git update-index --chmod=+x foo.sh
@ansig
ansig / jobdsl-add-secure-groovy-script-sandbox-with-configure-block.groovy
Created August 21, 2017 09:11
JobDSL: add secure Groovy script in sandbox with configure blocks
Closure envVar(String key, String val) {
return {
it / 'buildWrappers' / 'EnvInjectBuildWrapper' / 'info' << {
propertiesContent("${key}=${val}")
}
}
}
Closure secureGroovyScript(String scriptText, boolean useSandbox) {
return {
@ansig
ansig / fetch_artifact_from_nexus.sh
Created July 28, 2017 07:44
This is a Bash script for fetching artifacts from Nexus via the command line
#!/usr/bin/env bash
########################################################################################################
# This is a utility script to fetch artifacts from a Nexus server. It is based on an example #
# from Sonartype Nexus: http://blog.sonatype.com/2011/01/downloading-artifacts-from-nexus-with-bash/ #
########################################################################################################
set -e
REST_PATH=/service/local
@ansig
ansig / MySpringAppWithProperties.java
Created July 27, 2017 06:11
Spring app annotation config to read properties from file on default path or from parameter
@Configuration
@ComponentScan(basePackages = "sen.ansig.spring")
@PropertySource("classpath:config.properties")
@PropertySource(value = "file:${config.path:config.properties}", ignoreResourceNotFound = true)
public class MySpringAppWithProperties {
}