Skip to content

Instantly share code, notes, and snippets.

View marcosborges's full-sized avatar

Marcos Borges marcosborges

View GitHub Profile
@Grapes(
@Grab(group='org.yaml', module='snakeyaml', version='1.23')
)
import org.yaml.snakeyaml.Yaml as Snakeyaml
import groovy.json.JsonSlurper
import groovy.json.JsonSlurperClassic
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.JsonParserType
@marcosborges
marcosborges / jenkins_simple_credentials.groovy
Created January 20, 2019 23:39
Exemplos de como trabalhar com os tipos básicos de credenciais do jenkins
import jenkins.model.Jenkins;
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.plugins.credentials.impl.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*
import org.jenkinsci.plugins.plaincredentials.*
import org.jenkinsci.plugins.plaincredentials.impl.*
import org.apache.commons.io.IOUtils;
import hudson.plugins.sshslaves.*
withCredentials([
string(
credentialsId: "SECRET_TEXT_EXEMPLE",
variable: "SECRET_TEXT"
),
usernamePassword(
credentialsId: "SECRET_USERNAME_PASSWORD_EXEMPLE",
usernameVariable: 'SECRET_USERNAME',
passwordVariable: 'SECRET_PASSWORD'
),
def slackfy(msg){
def hookURL = "https://hooks.slack.com/services/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"
def payload ="""{"text":"${msg}","username":"Jenkins","icon_emoji":":jenkins:"}"""
sh "curl -s -X POST --data-urlencode \'payload=${payload}\' ${hookURL}"
}
URL_API=http://www.minhaapi.com.br/meu-endpoint
TOKEN_NAME=access_token
@marcosborges
marcosborges / example_package.json
Created January 28, 2019 15:31
Angular - Trabalhando com criação dinâmica das configurações com base nas variáveis de ambiente
...
"scripts": {
"ng": "ng",
"config": "ts-node ./src/set-env.ts",
"start": "npm run config -- --environment=dev && ng serve",
"build": "npm run config && ng build --output-path=dist",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
import jenkins.model.Jenkins;
import com.cloudbees.plugins.credentials.*
import org.jenkinsci.plugins.plaincredentials.*
import org.jenkinsci.plugins.plaincredentials.impl.*
def _cred = CredentialsProvider.lookupCredentials(
Credentials.class,
Jenkins.getInstance(),
null,
null
def name = "NOME_DA_NAMESPACE"
def solution = "NOME_DA_SOLUÇÃO"
def existsK8sNamespace = sh (script: """kubectl get namespace --field-selector="metadata.name==${name}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def existsK8sService = sh (script: """kubectl get service -n ${name} --field-selector="metadata.name==${solution}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def existsK8sIngress = sh (script: """kubectl get ingress -n ${name} --field-selector="metadata.name==${name}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def existsK8sDeployment = sh (script: """kubectl get deployment -n ${name} --field-selector="metadata.name==${solution}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def existsK8sHPA = sh (script: """kubectl get hpa -n ${name} --field-selector="metadata.name==${solution}" -o jsonpath="{.items[*]
def folders = Jenkins.getInstance().getAllItems(com.cloudbees.hudson.plugins.folder.Folder.class)
for(f in folders){
println "FOLDER: ${f.name}"
def stores = com.cloudbees.plugins.credentials.CredentialsProvider.lookupStores(f);
for (s in stores) {
switch(s.getContext().getClass()){
case com.cloudbees.hudson.plugins.folder.Folder:
println " STORE: ${s.getContext().name}";
def _jenkins = Jenkins.getActiveInstance();
def _store = _jenkins.getExtensionList(
org.jenkinsci.plugins.configfiles.GlobalConfigFiles.class
).get(
org.jenkinsci.plugins.configfiles.GlobalConfigFiles.class
);
println "CONFIGURAÇÕES GLOBAIS"
def _configs = _store.getConfigs()
for(c in _configs){