I use Jenkins to checkout source code and build image and push the image.
In Jenkins I configurate Docker just like this:
in my Jenkins Pipeline Script
pipeline {
agent none
environment {
registry = ""
registryCredential = ''
imageName = 'imageName'
dockerImage = ''
dockerHome = tool 'docker_latest'
PATH = "$dockerHome/bin:$PATH"
}
stages {
stage('Prepare') {
agent {
label "${config.job.agent}"
}
steps {
echo "CheckOut"
script {
checkout
}
}
}
stage('Building image') {
steps{
dir('jenkins-slave-savi'){
script {
dockerImage = docker.build imageName + ":$BUILD_NUMBER"
}
}
}
}
stage('Deploy Image'){
steps {
script {
docker.withRegistry(registry) {
dockerImage.push()
}
}
}
}
}
}
but when I run this script, I got this error:
ERROR: Failed to download pre-1.11.x URL https://get.docker.com/builds/Linux/x86_64/docker-latest from agent: java.net.ConnectException: Connection timed out (Connection timed out)
ERROR: Failed to download https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz from agent; will retry from master
any solutions???
Related
I tried all the possible solutions posted by other people but still I am struggling to resolve this issue. I believe in my case, it has got to do something with the agents. I will post 2 codes, the one code works but the other doesn't. Both the codes call the same groovy methods but still the second code snippet doesn't work.
The below code 1 works fine and executed the pipeline successfully:
pipeline{
agent { label 'docker-kitchensink-slave' }
stages{
stage('Checkout') {
steps{
checkout scm
}
}
//Build and Unit Tests
stage('Build and Unit Tests') {
steps{
script{
if (buildType.buildSystem == 'npm'){
buildNpm(configuration)
} else {
build(configuration)
}
}
}
}
// SonarQube Analysis
stage('SonarQube analysis') {
steps{
script{
if (buildType.buildSystem != 'npm'){
sonarQubeGating(configuration)
}
}
}
}
// Build Docker Image and Push to Artifactory
stage('Build Docker Image and Push to Artifactory') {
steps{
artifactoryImagePush(configuration)
}
}
// Approve DEV Deployment
stage('Approve Dev Deployment') {
agent none
when {
anyOf {
expression {
return (env.GIT_BRANCH.equals('master') || env.GIT_BRANCH.startsWith('hotfix-'))
}
}
}
steps{
approveDeployment()
}
}
}
}
The below code 2 doesn't work:
pipeline{
agent none
stages{
stage('Checkout') {
agent { label 'docker-kitchensink-slave' }
steps{
checkout scm
}
}
//Build and Unit Tests
stage('Build and Unit Tests') {
agent { label 'docker-kitchensink-slave' }
steps{
script{
if (buildType.buildSystem == 'npm'){
buildNpm(configuration)
} else {
build(configuration)
}
}
}
}
// SonarQube Analysis
stage('SonarQube analysis') {
agent { label 'docker-kitchensink-slave' }
steps{
script{
if (buildType.buildSystem != 'npm'){
sonarQubeGating(configuration)
}
}
}
}
// Build Docker Image and Push to Artifactory
stage('Build Docker Image and Push to Artifactory') {
agent { label 'docker-kitchensink-slave' }
steps{
unstash 'artifacts'
unstash 'artifacts'
artifactoryImagePush(configuration)
}
}
// Approve DEV Deployment
stage('Approve Dev Deployment') {
agent none
when {
anyOf {
expression {
return (env.GIT_BRANCH.equals('master') || env.GIT_BRANCH.startsWith('hotfix-'))
}
}
}
steps{
approveDeployment()
}
}
}
}
I get the error as below:
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project xyz-service: Your project contains .java files, please provide compiled classes with sonar.java.binaries property, or exclude them from the analysis with sonar.exclusions property. -> [Help 1]
Below is my sonar code:
void call(Map optionParams = [:]) {
script {
try {
String jacocoPath = optionParams.get('buildSystem').equals('gradle') ?
'build/JacocoReport/test/jacocoTestReport.xml' : 'target/site/jacoco/jacoco.xml'
glSonarMavenScan gitUserCredentialsId: 'sonar-key', javaVersionForSonar: '11.0', mavenVersion: '3.5.4',
additionalProps: ['sonar.coverage.jacoco.xmlReportPaths' : jacocoPath]
} catch (Exception e) {
echo "The following Sonar exception thrown ${e}"
//Stop build here, unless 'requireSonar' is set to False (String or Boolean)
if (!optionParams.get('requireSonar').toString().equalsIgnoreCase('false')) {
throw e
}
}
}
}
I'm a little confused about what you're trying to achieve here. You are showing the code that works. Are you trying to understand WHY the first block works, compared to the second, or are you just trying to get it working? If the latter, clearly you are already done.
If the former, I'm only familiar with scripted pipeline, not declarative pipeline, but it seems possible to me that if there is more than one build node that satisfies that label, then each of those "agent" lines could potentially select a build node, and each one could potentially select a different one. If the build step executes a different node than the sonarqube scan is run on, you will find yourself in a workspace without any compiled classes.
I want to build a jar out of my Gradle project and push it to the nexus repository. As part of this, I created a Jenkins file and added a task "publishing" task in build.gradle.
My Jenkins file:
pipeline {
agent any
environment {
NEXUS = credentials('nexus-user')
}
options {
ansiColor('xterm')
buildDiscarder logRotator(daysToKeepStr: '30', numToKeepStr: '100')
}
triggers { pollSCM('H/5 * * * *') }
stages {
stage('Checkout'){
steps { checkout scm }
}
stage('Build') {
steps { sh "./gradlew assemble" }
}
stage('deploy') {
steps {
sh "gradle -Duser.home=\"$WORKSPACE\" --gradle-user-home=\"$WORKSPACE/.gradle\" -PnexusUsername=$NEXUS_USR -PnexusPassword=$NEXUS_PSW publish"
}
}
}
}
And build.gradle
publishing {
publications{
maven(MavenPublication){
artifactId = "testApp"
from components.java
}
}
repositories {
maven {
url = "http://localhost:8081/nexus/content/repositories/${version.endsWith('-SNAPSHOT') ? "snapshots" : "releases"}"
credentials {
username = "Dont know how to pass the username here"
password = "Dont know how to pass the password here"
}
}
}
}
Could anyone tell me how to get the username and password from Gradle and set here for publishing the jar to nexus.
Answer is:
publishing {
publications{
maven(MavenPublication){
artifactId = "testApp"
from components.java
}
}
repositories {
maven {
url = "http://localhost:8081/nexus/content/repositories/${version.endsWith('-SNAPSHOT') ? "snapshots" : "releases"}"
credentials {
username = "nexusUsername"
password = "nexusPassword"
}
}
}
}
I need to check out the code into $GOPATH/src/dev-DIR folder. but due to skipDefaultCheckout true option checkout scm and other stages are not executing. Directly post actions are getting executing Please help me where am I going wrong?
pipeline {
agent {node {label 'project_a'}}
options {
skipDefaultCheckout true
}
environment {
PATH = "$PATH:/opt/jenkins/:/usr/local/go/bin/"
GIT_REPO = get_gitrepo()
GOPATH= "${env.WORKSPACE}"
PROJECT_WORKSPACE = "${env.WORKSPACE}/src/dev-DIR"
}
stages {
stage('checkout scm'){
steps{
dir ("${GOPATH}/src/dev-DIR"){
checkout scm
}
}
}
stage('Install Prerequisites') {
// go get -t
}
}
post{
//some actions
}
}
```
I am trying to setup a jenkins pipeline script that sends out an email when there is a job that have been running for more than 24 hours.
// Long running jobs
pipeline {
agent any
environment {
EMAIL_ALERT_TO = "address"
EMAIL_ALERT_CC = "address"
}
stages {
stage('def methods') {
steps {
script {
Jenkins.instance.getAllItems(Job).each(){ job -> job.isBuildable()
if (job.isBuilding()){
def myBuild= job.getLastBuild()
def runningSince= groovy.time.TimeCategory.minus( new Date(), myBuild.getTime() )
echo "myBuild = ${myBuild}"
echo "runningSince = ${runningSince}"
env.myBuild = myBuild
env.runningSince = runningSince
}
}
}
}
}
}
post {
// Email out the results
always {
script {
if (runningSince.hours >= 1){
mail to: "${env.EMAIL_ALERT_CC}",
cc: "${env.EMAIL_ALERT_CC}",
subject: "Long Running Jobs",
body: "Build: ${myBuild} ---- Has Been Running for ${runningSince.hours} hours:${runningSince.minutes} minutes"
}
}
}
}
}
I am seeing RejectedAccessException which appears to be related to arrays/list.
This is what I believe you are looking for
https://issues.jenkins-ci.org/browse/JENKINS-54952?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel
I need to fail one Jenkins pipeline stage when one file contains 'errors'
I do not know how to return an error from bash to Jenkins
stage('check if file continas error and exit if true') {
steps {
sh "grep 'error' filetocheck.txt"
}
}
}
reference Is it possible to capture the stdout from the sh DSL command in the pipeline
This worked for me,
def runShell(String command){
def responseCode = sh returnStatus: true, script: "${command} &> tmp.txt"
def output = readFile(file: "tmp.txt")
return (output != "")
}
pipeline {
agent any
stages {
stage('check shellcheck') {
steps {
script {
if (runShell('grep \'error\' file_to_parse.txt')) {
sh "exit 1"
}
}
}
}
}
}
you can try using String.count(charSequence) where String could be a file or string.
def file = 'path/to/file.txt'
if ( file.count('error') > 0 )
return stageResultMap.didB2Succeed = false