Jenkins error ImportError: No module named boto3 - jenkins

everyone.
There is Jenkins running on my ubunut host.
Trying to execute the creation of the "aws ECR" in Jenkins file.
but,Jenkins error ImportError: No module named boto3error
my jenkins file
node {
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = docker.build("xxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/xxx")
}
stage('Create ECR') {
steps {
'''
sh 'pip3 install boto3 --upgrade'
sh 'python3 ecr.py'
'''
}
}
stage('Push image') {
sh 'rm ~/.dockercfg || true'
sh 'rm ~/.docker/config.json || true'
docker.withRegistry('https://xxxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com', 'ecr:ap-northeast-2:xxx-aws-ecr') {
app.push("xxx")
app.push("${env.BUILD_NUMBER}")
app.push("latest")
}
}
}
Thanks you!!

Related

Transfer data between stages on Jenkins Pipeline

Am I able somehow to copy data from one stage for usage on another?
For example, I have one stage where I want to clone my repo, and on another run the Kaniko which will copy (on dockerfile) all data to container and build it
How to do this? Because
Stages are independent and I not able to operate via the same data on both
on Kaniko I not able to install the GIT to clone it there
Thanks in advance
Example of code :
pipeline {
agent none
stages {
stage('Clone repository') {
agent {
label 'builder'
}
steps {
sh 'git clone ssh://git#myrepo.com./repo.git'
sh 'cd repo'
}
}
stage('Build application') {
agent {
docker {
label 'builder'
image 'gcr.io/kaniko-project/executor:debug'
args '-u 0 --entrypoint=""'
}
}
steps {
sh '''#!/busybox/sh
/kaniko/executor -c `pwd` -f Dockerfile"
'''
}
}
}
}
P.S. On dockerfile I using such as
ADD . /
You can try to use stash:
stage('Clone repository') {
agent {
label 'builder'
}
steps {
sh 'git clone ssh://git#myrepo.com./repo.git'
script {
stash includes: 'repo/', name: 'myrepo'
}
}
}
stage('Build application') {
agent {
docker {
label 'builder'
image 'gcr.io/kaniko-project/executor:debug'
args '-u 0 --entrypoint=""'
}
}
steps {
script {
unstash 'myrepo'
}
sh '''#!/busybox/sh
/kaniko/executor -c `pwd` -f Dockerfile"
'''
}

Jenkins "No such DSL method 'steps' found among steps"

Jenkins output error..
[Checks API] No suitable checks publisher found.
java.lang.NoSuchMethodError: No such DSL method 'steps' found among steps
My jenkinsfile.
node {
stage('Clone repository') {
checkout scm
}
stage('Build packer') {
steps {
dir('packer') {
sh 'git clone https://github.com/changhyuni/packer'
sh 'packer build ec2.json'
}
}
}
stage('Build image') {
app = docker.build("475667265637.dkr.ecr.ap-northeast-2.amazonaws.com/chang")
}
stage('Create ECR') {
sh 'pip3 install boto3 --upgrade'
sh 'python3 ecr.py'
}
stage('Push image') {
sh 'rm ~/.dockercfg || true'
sh 'rm ~/.docker/config.json || true'
docker.withRegistry('https://475667265637.dkr.ecr.ap-northeast-2.amazonaws.com', 'ecr:ap-northeast-2:chang-aws-ecr') {
app.push("chang")
app.push("${env.BUILD_NUMBER}")
app.push("latest")
}
}
}
steps is a directive from declarative syntax https://www.jenkins.io/doc/book/pipeline/syntax/#steps
Your example is scripted syntax https://www.jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline

Jenkins piepleine with dir correct syntax

Hi guys I'm trying to write a jenkins pipeline with change dir, I can't get the syntax right, can anyone help me fix this?
the documentation is kinda unclear on where should I put this
pipeline {
agent {
docker {
image 'node'
args '-p 5000:5000'
}
}
stages {
dir("Backend") {
stage('build') {
steps {
sh 'pwd'
sh 'echo start build'
sh 'ls'
sh 'npm install'
}
}
}
dir("Backend") {
stage('Test') {
steps {
sh 'cd Backend'
sh 'echo start test'
sh 'ls'
sh 'npm run test'
}
}
}
}
}
dir is a step, therefore it must be contained within steps block:
stage('build') {
steps {
dir("Backend") {
sh 'pwd'
sh 'echo start build'
sh 'ls'
sh 'npm install'
}
}
}

jenkins pipeline passing variables

I have a pipeline and I'm building my image through a docker container and it output the image tag, I want to pass that image tag to next stage, when I echo it in the next stage it prints out. but when I use it in a shell it goes empty. here is my pipeline
pipeline {
agent any
stages {
stage('Cloning Git') {
steps {
git( url: 'https://xxx#bitbucket.org/xxx/xxx.git',
credentialsId: 'xxx',
branch: 'master')
}
}
stage('Building Image') {
steps{
script {
env.IMAGE_TAG = sh script: "docker run -e REPO_APP_BRANCH=master -e REPO_APP_NAME=exampleservice -e DOCKER_HUB_REPO_NAME=exampleservice --volume /var/run/docker.sock:/var/run/docker.sock registry.xxxx/build", returnStdout: true
}
}
}
stage('Integration'){
steps{
script{
echo "passed: ${env.IMAGE_TAG}"
sh """
helm upgrade exampleservice charts/exampleservice --set image.tag=${env.IMAGE_TAG}
"""
sh "sleep 5"
}
}
}
}
}
pipeline output
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integration)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
passed:
b79c3bf-b6eec4f
[Pipeline] sh
[test101] Running shell script
+ helm upgrade exampleservice charts/exampleservice --set image.tag=
getting empty image tag
You should override this by using the 'env'.
Replace your code with this one:
pipeline {
agent any
stages {
stage('Cloning Git') {
steps {
git( url: 'https://xxx#bitbucket.org/xxx/xxx.git',
credentialsId: 'xxx',
branch: 'master')
}
}
stage('Building Image') {
steps{
script {
env.IMAGE_TAG = sh script: "docker run -e REPO_APP_BRANCH=master -e REPO_APP_NAME=exampleservice -e DOCKER_HUB_REPO_NAME=exampleservice --volume /var/run/docker.sock:/var/run/docker.sock registry.xxxx/build", returnStdout: true
}
}
}
stage('Integration'){
steps{
script{
echo "passed: ${env.IMAGE_TAG}"
sh """
helm upgrade exampleservice charts/exampleservice\
--set image.tag="${env.IMAGE_TAG}"
"""
sh "sleep 5"
}
}
}
}
}

JenkinsFile for Sonar Quality Gates doesn't work

I have read the documentation of Jenkins and Sonar to write and configure my pipeline and everything works except the Sonar Quality Gate which remains for the response from the Sonar without success. This is my code :
pipeline {
agent master
tools {
jdk 'JAVA'
maven 'mvn_3_5_2'
}
stages {
stage('test Java & Maven installation') {
steps {
sh 'java -version'
sh 'which java'
sh 'mvn -version'
sh 'which mvn'
}
}
stage('Clean stage') {
steps {
sh 'mvn -f project/pom.xml clean'
}
}
stage("build & SonarQube analysis") {
node {
withSonarQubeEnv('Sonar') {
sh 'mvn -f project/pom.xml org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
}
}
}
stage("Quality Gate"){
timeout(time: 2, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
stage('Test stage') {
steps {
sh 'mvn -f project/pom.xml test'
}
}
stage('Build stage') {
steps {
sh 'mvn -f project/pom.xml install'
}
}
stage('Deploy stage') {
steps {
echo 'Deployment...'
// sh 'mvn -f project/pom.xml appengine:deploy -Dapp.deploy.project=acn-shelf-check -Dapp.deploy.version=v1'
}
}
}
}
In the stage "Quality Gate" I never received the result. I have configured the webhook on the sonar with correct URL of Jenkins.
Do I miss any step?

Resources