I am working with buctbucket version 4.4.1. When i am trying to run by custom pre-commit hook with stash it gives me error stating that failed to push some references. the hook is written in shell format placed inside pre-receive.d directory of repository. the script when placed inside this directory is executing file but at the end getting failed with below error
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'http://#/scm/ht/pre-commit.git'
here is the debug log of my scriptenter code here
http://mishrav#vklnld1156:7990/scm/ht/pre-commit.git ! [remote
rejected] master -> master (pre-receive hook declined) error: failed
to push some refs to
'http://mishrav#vklnld1156:7990/scm/ht/pre-commit.git'
#!/bin/bash
#set -x
OSType=$(uname -s)
echo $OSType
if [ $OSType == "Linux" ]; then
exec < /dev/tty
echo -n "Enter Project Name : "
read PName
echo -n "Enter Fix Version : "
read Version
FVersion=$(echo $Version | sed -e 's/^/\%22/'| sed -e 's/$/\%22/')
echo -n "Enter Jira issue Number : "
read JNumber
elif [ $OSType != "Linux" ]; then
PName=$(git gui--askpass "Enter Project Name : ")
echo $PName
Version=$(git gui--askpass "Enter Fix Version : ")
echo $Version
FVersion=$(echo $Version | sed -e 's/^/\%22/'| sed -e 's/$/\%22/')
JNumber=$(git gui--askpass "Enter Jira Issue Number : ")
echo $JNumber
fi
checkVersion(){
if [[ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD) | awk -F "/" '{print $1}') == master ]]; then
echo "On master branch "
return 1
elif [[ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD) | awk -F "/" '{print $1}') != master ]]; then
echo "On feature branch"
branchVersion=$(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD) | awk -F "/" '{print $2}')
else
echo "Branch not present "
fi
}
pomVersion(){
if [ $OSType == "Linux" ]; then
PomVersion=$(git show :pom.xml | xml_grep 'version' --text_only)
elif [ $OSType != "Linux" ]; then
PomVersion=$(git show :pom.xml | sed -n 's:.*<version>\(.*\)</version>.*:\1:p')
fi
}
checkTotal(){
checkVersion
pomVersion
if [ $OSType == "Linux" ]; then
TotalIssueLabel=$(curl -u gituser:gituser http://jira:9090/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | jq '.total' | sed 's/null/0/' )
TotalIssuesFix=$(curl -u gituser:gituser http://jira:9090/rest/api/2/search?jql=project=$PName+AND+fixVersion=$FVersion+AND+Key=$JNumber | python -mjson.tool | jq '.total' | sed 's/null/0/')
if [ $TotalIssueLabel -gt 0 ]
then
JiraValue=$(curl -u gituser:gituser http://xxxxxx/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | jq '.issues[].fields.labels[]'| sed 's/\"//g')
if [ "$PomVersion" == "$JiraValue" ] && [ "$branchVersion" == "$JiraValue" ]; then
echo "Commit Successfull on $branchVersion 1"
return 1
elif [ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD)| awk -F "/" '{print $1}' ) == master ]; then
echo "commet successful on master branch 1"
fi
elif [ $TotalIssue -gt 0 ]
then
JiraValue=$(curl -u gituser:gituser http://xxxxx/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | jq '.issues[].fields.fixVersions[]' | sed 's/\"//g')
if [ "$PomVersion" == "$JiraValue" ] && [ "$branchVersion" == "$JiraValue" ]; then
echo "Commit Successfull on $branchVersion 2"
return 1
elif [ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD)| awk -F "/" '{print $1}' ) == master ]; then
echo "commet successful on feature branch 2"
return 1
else
echo "No vaules mached 2"
return 1
fi
fi
elif [ $OSType != "Linux" ]; then
TotalIssueLabel=$(curl -u gituser:gituser http://xxxxxxx/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | C:/jq/jq.exe '.total' | sed 's/null/0/' )
TotalIssueFix=$(curl -u gituser:gituser http://jira:9090/rest/api/2/search?jql=project=$PName+AND+fixVersion=$FVersion+AND+Key=$JNumber | python -mjson.tool | C:/jq/jq.exe '.total' | sed 's/null/0/')
if [ $TotalIssueLabel -gt 0 ]
then
JiraValue=$(curl -u gituser:gituser http://xxxxxx/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | C:/jq/jq.exe '.issues[].fields.labels[]'| sed 's/\"//g')
if [ "$PomVersion" == "$JiraValue" ] && [ "$branchVersion" == "$JiraValue" ]; then
echo "Commit Successfull on $branchVersion 1"
exit 1
elif [ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD)| awk -F "/" '{print $1}') == master ]; then
echo "commet successful on master branch 1"
fi
elif [ $TotalIssueFix -gt 0 ]
then
JiraValue=$(curl -u gituser:gituser http://jira:9090/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | C:/jq/jq.exe '.issues[].fields.fixVersions[]' | sed 's/\"//g')
if [ "$PomVersion" == "$JiraValue" ] && [ "$branchVersion" == "$JiraValue" ]; then
echo "Commit Successfull on $branchVersion 2"
exit 1
elif [ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD)| awk -F "/" '{print $1}') == master ]; then
echo "commet successful on feature branch 2"
exit 1
else
echo "No vaules mached 2"
exit 1
fi
else
echo "no issues mached"
exit 1
fi
fi
}
checkTotal
#def get_git_revision_hash():
# return subprocess.check_output(['git', 'rev-parse', 'HEAD'])
# In *nix systems, an "exit code" of 0 means
# that execution happened sucessfully, whereas a nonzero
# exit code indicates an error.
#
# In python, you can return an exit code with sys.exit(CODE)
#sys.exit(-1)
Your hook is wrong and it exits with status 1, that causes the push to fail. Please refer to the docs. The hook receives in stdin <old-value> <new-value> <ref-name>, so you script inserts these into your variables PName, Version and JNumber here
if [ $OSType == "Linux" ]; then
exec < /dev/tty
echo -n "Enter Project Name : "
read PName
echo -n "Enter Fix Version : "
read Version
FVersion=$(echo $Version | sed -e 's/^/\%22/'| sed -e 's/$/\%22/')
echo -n "Enter Jira issue Number : "
read JNumber
You need to change your hook to etxract the issues from the commit message
Related
I'm new to Jenkins.
I have a job with an "Execute Shell" Build trigger, and in that shell script i initiate some variables with values i take from some source.
I need to pass these values to a downstream job.
The values i want to pass are $IMG_NAME and $IMG_PATH from this shell script:
#!/bin/bash -x
whoami
echo "BASE_PATH: $BASE_PATH"
declare -A BRANCHES
for i in $(echo $BRANCHES_LIST | tr ',' '\n'); do BRANCHES[$i]= ; done
echo 'user' | sudo -S umount -rf /mnt/tlv-s-comp-prod/drops/
echo 'user' | sudo -S mount.nfs -o nolock,nfsvers=3 tlv-s-comp-prod:/export/drops /mnt/tlv-s-comp-prod/drops
ls /mnt/tlv-s-comp-prod/drops/
echo "cleanup workspace"
rm ${WORKSPACE}/*.txt &> /dev/null
i="0"
while [ $i -lt 6 ]
do
if [[ ${BASE_PATH} == *"Baseline"* ]]; then
unset BRANCHES[#]
declare -A BRANCHES
BRANCHES[Baseline]=
fi
for BRANCH in "${!BRANCHES[#]}"; do
echo "BRANCH: $BRANCH"
if [ $BRANCH == "Baseline" ]; then BRANCH=; fi
img_dir=$(ls -td -- ${BASE_PATH}/${BRANCH}/*/ | head -n 1)
echo "img_dir: $img_dir"
IMG_PATH=$(ls $img_dir*.rpm)
echo "IMG_PATH: $IMG_PATH"
cd $img_dir
IMG_NAME=$(ls *.rpm) > env.properties
if [ ! -z "$IMG_NAME" ]; then
if [ $(( $(date +%s) - $(stat -c %Z $IMG_PATH) )) -lt 10000800 ]; then
echo "IMG_NAME: ${IMG_NAME}"
#BRANCHES[$BRANCH]=$IMG_PATH
#echo "REG_OSA_SOFTSYNC_BUILD_IMG_FULL_PATH=${BRANCHES[$BRANCH]}" >> ${WORKSPACE}/$BRANCH.txt
echo "BRANCH_NAME=$BRANCH" >> ${WORKSPACE}/${BRANCH}_branch.txt
echo "REG_OSA_SOFTSYNC_BUILD_NAME=$BRANCH-$IMG_NAME" >> ${WORKSPACE}/${BRANCH}_branch.txt
else
echo "$IMG_NAME is out dated"
fi
else
echo "IMG_NAME is empty"
fi
BRANCH_NAME=""
done
$TEMP=$BRANCH_NAME
echo "TEMP: $TEMP"
if [ $(ls ${WORKSPACE}/*_branch.txt | wc -l) == $(echo ${#BRANCHES[#]}) ]; then break; fi
#for i in $(ls *_branch.txt); do i=$(echo $i | awk -F '_branch.txt' '{print $1}'); if [ $(echo ${!BRANCHES[#]} | grep $i | wc -l) == 0 ]; then state=1 break; fi done
i=$[$i+1]
sleep 1800
done
This is the "Trigger parameterized build on other projects" configuration:
I am implementing the SonarQube to my project. SonarQube server is up but when i am try to run the run-sonar-swift.sh script i got console error and i can't see the project on the console. SonarQube and SonarScanner added under the Application folder.
Here is my run-sonar-swift.sh script:
#!/bin/bash
#
# backelite-sonar-swift-plugin - Enables analysis of Swift and Objective-C projects into SonarQube.
# Copyright © 2015 Backelite (${email})
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
## INSTALLATION: Copy this script somewhere in your PATH
## USAGE: ./run-sonar-swift.sh
## DEBUG: ./run-sonar-swift.sh -v
## WARNING: edit your project parameters in sonar-project.properties rather than modifying this script
#
# Global parameters
SLATHER_CMD=slather
SWIFTLINT_CMD=swiftlint
TAILOR_CMD=tailor
XCPRETTY_CMD=xcpretty
LIZARD_CMD=lizard
XCODEBUILD_CMD=xcodebuild
trap "echo 'Script interrupted by Ctrl+C'; stopProgress; exit 1" SIGHUP SIGINT SIGTERM
function startProgress() {
while true
do
echo -n "."
sleep 5
done
}
function stopProgress() {
if [ "$vflag" = "" -a "$nflag" = "" ]; then
kill $PROGRESS_PID &>/dev/null
fi
}
function testIsInstalled() {
hash $1 2>/dev/null
if [ $? -eq 1 ]; then
echo >&2 "ERROR - $1 is not installed or not in your PATH"; exit 1;
fi
}
function readParameter() {
variable=$1
shift
parameter=$1
shift
eval $variable="\"$(sed '/^\#/d' sonar-project.properties | grep $parameter | tail -n 1 | cut -d '=' -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')\""
}
# Run a set of commands with logging and error handling
function runCommand() {
# 1st arg: redirect stdout
# 2nd arg: command to run
# 3rd..nth arg: args
redirect=$1
shift
command=$1
shift
if [ "$nflag" = "on" ]; then
# don't execute command, just echo it
echo
if [ "$redirect" = "/dev/stdout" ]; then
if [ "$vflag" = "on" ]; then
echo "+" $command "$#"
else
echo "+" $command "$#" "> /dev/null"
fi
elif [ "$redirect" != "no" ]; then
echo "+" $command "$#" "> $redirect"
else
echo "+" $command "$#"
fi
elif [ "$vflag" = "on" ]; then
echo
if [ "$redirect" = "/dev/stdout" ]; then
set -x #echo on
$command "$#"
returnValue=$?
set +x #echo off
elif [ "$redirect" != "no" ]; then
set -x #echo on
$command "$#" > $redirect
returnValue=$?
set +x #echo off
else
set -x #echo on
$command "$#"
returnValue=$?
set +x #echo off
fi
if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
stopProgress
echo "ERROR - Command '$command $#' failed with error code: $returnValue"
exit $returnValue
fi
else
if [ "$redirect" = "/dev/stdout" ]; then
$command "$#" > /dev/null
elif [ "$redirect" != "no" ]; then
$command "$#" > $redirect
else
$command "$#"
fi
returnValue=$?
if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
stopProgress
echo "ERROR - Command '$command $#' failed with error code: $returnValue"
exit $returnValue
fi
echo
fi
}
## COMMAND LINE OPTIONS
vflag=""
nflag=""
unittests="on"
swiftlint="on"
tailor="on"
lizard="on"
oclint="on"
fauxpas="on"
sonarscanner=""
while [ $# -gt 0 ]
do
case "$1" in
-v) vflag=on;;
-n) nflag=on;;
-nounittests) unittests="";;
-noswiftlint) swiftlint="";;
-notailor) tailor="";;
-usesonarscanner) sonarscanner="on";;
--) shift; break;;
-*)
echo >&2 "Usage: $0 [-v]"
exit 1;;
*) break;; # terminate while loop
esac
shift
done
# Usage OK
echo "Running run-sonar-swift.sh..."
## CHECK PREREQUISITES
# sonar-project.properties in current directory
if [ ! -f sonar-project.properties ]; then
echo >&2 "ERROR - No sonar-project.properties in current directory"; exit 1;
fi
## READ PARAMETERS from sonar-project.properties
#.xcodeproj filename
projectFile=''; readParameter projectFile 'sonar.swift.project'
workspaceFile=''; readParameter workspaceFile 'sonar.swift.workspace'
# Count projects
if [[ ! -z "$projectFile" ]]; then
projectCount=$(echo $projectFile | sed -n 1'p' | tr ',' '\n' | wc -l | tr -d '[[:space:]]')
if [ "$vflag" = "on" ]; then
echo "Project count is [$projectCount]"
fi
fi
# Source directories for .swift files
srcDirs=''; readParameter srcDirs 'sonar.sources'
# The name of your application scheme in Xcode
appScheme=''; readParameter appScheme 'sonar.swift.appScheme'
# The app configuration to use for the build
appConfiguration=''; readParameter appConfiguration 'sonar.swift.appConfiguration'
# The name of your test scheme in Xcode
testScheme=''; readParameter testScheme 'sonar.swift.testScheme'
# The name of your binary file (application)
binaryName=''; readParameter binaryName 'sonar.swift.appName'
# Get the path of plist file
plistFile=`xcodebuild -showBuildSettings -project "${projectFile}" | grep -i 'PRODUCT_SETTINGS_PATH' -m 1 | sed 's/[ ]*PRODUCT_SETTINGS_PATH = //'`
# Number version from plist if no sonar.projectVersion
numVerionFromPlist=`defaults read ${plistFile} CFBundleShortVersionString`
# Read destination simulator
destinationSimulator=''; readParameter destinationSimulator 'sonar.swift.simulator'
# Read tailor configuration
tailorConfiguration=''; readParameter tailorConfiguration 'sonar.swift.tailor.config'
# The file patterns to exclude from coverage report
excludedPathsFromCoverage=''; readParameter excludedPathsFromCoverage 'sonar.swift.excludedPathsFromCoverage'
# Check for mandatory parameters
if [ -z "$projectFile" -o "$projectFile" = " " ] && [ -z "$workspaceFile" -o "$workspaceFile" = " " ]; then
echo >&2 "ERROR - sonar.swift.project or/and sonar.swift.workspace parameter is missing in sonar-project.properties. You must specify which projects (comma-separated list) are application code or which workspace and project to use."
exit 1
elif [ ! -z "$workspaceFile" ] && [ -z "$projectFile" ]; then
echo >&2 "ERROR - sonar.swift.workspace parameter is present in sonar-project.properties but sonar.swift.project and is not. You must specify which projects (comma-separated list) are application code or which workspace and project to use."
exit 1
fi
if [ -z "$srcDirs" -o "$srcDirs" = " " ]; then
echo >&2 "ERROR - sonar.sources parameter is missing in sonar-project.properties. You must specify which directories contain your .swift source files (comma-separated list)."
exit 1
fi
if [ -z "$appScheme" -o "$appScheme" = " " ]; then
echo >&2 "ERROR - sonar.swift.appScheme parameter is missing in sonar-project.properties. You must specify which scheme is used to build your application."
exit 1
fi
if [ "$unittests" = "on" ]; then
if [ -z "$destinationSimulator" -o "$destinationSimulator" = " " ]; then
echo >&2 "ERROR - sonar.swift.simulator parameter is missing in sonar-project.properties. You must specify which simulator to use."
exit 1
fi
fi
# if the appConfiguration is not specified then set to Debug
if [ -z "$appConfiguration" -o "$appConfiguration" = " " ]; then
appConfiguration="Debug"
fi
if [ "$vflag" = "on" ]; then
echo "Xcode project file is: $projectFile"
echo "Xcode workspace file is: $workspaceFile"
echo "Xcode application scheme is: $appScheme"
echo "Number version from plist is: $numVerionFromPlist"
if [ -n "$unittests" ]; then
echo "Destination simulator is: $destinationSimulator"
echo "Excluded paths from coverage are: $excludedPathsFromCoverage"
else
echo "Unit surefire are disabled"
fi
fi
## SCRIPT
# Start progress indicator in the background
if [ "$vflag" = "" -a "$nflag" = "" ]; then
startProgress &
# Save PID
PROGRESS_PID=$!
fi
# Create sonar-reports/ for reports output
if [ "$vflag" = "on" ]; then
echo 'Creating directory sonar-reports/'
fi
rm -rf sonar-reports
mkdir sonar-reports
# Extracting project information needed later
echo -n 'Extracting Xcode project information'
if [[ "$workspaceFile" != "" ]] ; then
buildCmdPrefix="-workspace $workspaceFile"
else
buildCmdPrefix="-project $projectFile"
fi
buildCmd=($XCODEBUILD_CMD clean build $buildCmdPrefix -scheme $appScheme)
if [[ ! -z "$destinationSimulator" ]]; then
buildCmd+=(-destination "$destinationSimulator" -destination-timeout 360 COMPILER_INDEX_STORE_ENABLE=NO)
fi
runCommand xcodebuild.log "${buildCmd[#]}"
#oclint-xcodebuild # Transform the xcodebuild.log file into a compile_command.json file
cat xcodebuild.log | $XCPRETTY_CMD -r json-compilation-database -o compile_commands.json
# Objective-C code detection
hasObjC="no"
compileCmdFile=compile_commands.json
minimumSize=3
actualSize=$(stat -f%z "$compileCmdFile")
echo "actual = $actualSize, min = $minimumSize"
if [ $actualSize -ge $minimumSize ]; then
hasObjC="yes"
fi
# Unit surefire and coverage
if [ "$unittests" = "on" ]; then
# Put default xml files with no surefire and no coverage...
echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><testsuites name='AllTestUnits'></testsuites>" > sonar-reports/TEST-report.xml
echo "<?xml version='1.0' ?><!DOCTYPE coverage SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-03.dtd'><coverage><sources></sources><packages></packages></coverage>" > sonar-reports/coverage-swift.xml
echo -n 'Running surefire'
buildCmd=($XCODEBUILD_CMD clean build test)
if [[ ! -z "$workspaceFile" ]]; then
buildCmd+=(-workspace "$workspaceFile")
elif [[ ! -z "$projectFile" ]]; then
buildCmd+=(-project "$projectFile")
fi
buildCmd+=( -scheme "$appScheme" -configuration "$appConfiguration" -enableCodeCoverage YES)
if [[ ! -z "$destinationSimulator" ]]; then
buildCmd+=(-destination "$destinationSimulator" -destination-timeout 60)
fi
runCommand sonar-reports/xcodebuild.log "${buildCmd[#]}"
cat sonar-reports/xcodebuild.log | $XCPRETTY_CMD -t --report junit
mv build/reports/junit.xml sonar-reports/TEST-report.xml
echo '\nComputing coverage report\n'
# Build the --exclude flags
excludedCommandLineFlags=""
if [ ! -z "$excludedPathsFromCoverage" -a "$excludedPathsFromCoverage" != " " ]; then
echo $excludedPathsFromCoverage | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh2
while read word; do
excludedCommandLineFlags+=" -i $word"
done < tmpFileRunSonarSh2
rm -rf tmpFileRunSonarSh2
fi
if [ "$vflag" = "on" ]; then
echo "Command line exclusion flags for slather is:$excludedCommandLineFlags"
fi
firstProject=$(echo $projectFile | sed -n 1'p' | tr ',' '\n' | head -n 1)
slatherCmd=($SLATHER_CMD coverage)
if [[ ! -z "$binaryName" ]]; then
slatherCmd+=( --binary-basename "$binaryName")
fi
slatherCmd+=( --input-format profdata $excludedCommandLineFlags --cobertura-xml --output-directory sonar-reports)
if [[ ! -z "$workspaceFile" ]]; then
slatherCmd+=( --workspace "$workspaceFile")
fi
slatherCmd+=( --scheme "$appScheme" "$firstProject")
echo "${slatherCmd[#]}"
runCommand /dev/stdout "${slatherCmd[#]}"
mv sonar-reports/cobertura.xml sonar-reports/coverage-swift.xml
fi
# SwiftLint
if [ "$swiftlint" = "on" ]; then
if hash $SWIFTLINT_CMD 2>/dev/null; then
echo -n 'Running SwiftLint...'
# Build the --include flags
currentDirectory=${PWD##*/}
echo "$srcDirs" | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh
while read word; do
# Run SwiftLint command
$SWIFTLINT_CMD lint --path "$word" > sonar-reports/"$appScheme"-swiftlint.txt
done < tmpFileRunSonarSh
rm -rf tmpFileRunSonarSh
else
echo "Skipping SwiftLint (not installed!)"
fi
else
echo 'Skipping SwiftLint (test purposes only!)'
fi
# Tailor
if [ "$tailor" = "on" ]; then
if hash $TAILOR_CMD 2>/dev/null; then
echo -n 'Running Tailor...'
# Build the --include flags
currentDirectory=${PWD##*/}
echo "$srcDirs" | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh
while read word; do
# Run tailor command
$TAILOR_CMD $tailorConfiguration "$word" > sonar-reports/"$appScheme"-tailor.txt
done < tmpFileRunSonarSh
rm -rf tmpFileRunSonarSh
else
echo "Skipping Tailor (not installed!)"
fi
else
echo 'Skipping Tailor!'
fi
if [ "$oclint" = "on" ] && [ "$hasObjC" = "yes" ]; then
echo -n 'Running OCLint...'
# Options
maxPriority=10000
longLineThreshold=250
# Build the --include flags
currentDirectory=${PWD##*/}
echo "$srcDirs" | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh
while read word; do
includedCommandLineFlags=" --include .*/${currentDirectory}/${word}/*"
if [ "$vflag" = "on" ]; then
echo
echo -n "Path included in oclint analysis is:$includedCommandLineFlags"
fi
# Run OCLint with the right set of compiler options
runCommand no oclint-json-compilation-database -v $includedCommandLineFlags -- -rc LONG_LINE=$longLineThreshold -max-priority-1 $maxPriority -max-priority-2 $maxPriority -max-priority-3 $maxPriority -report-type pmd -o sonar-reports/$appScheme-oclint.xml
done < tmpFileRunSonarSh
rm -rf tmpFileRunSonarSh
else
echo 'Skipping OCLint (test purposes only!)'
fi
#FauxPas
if [ "$fauxpas" = "on" ] && [ "$hasObjC" = "yes" ]; then
hash fauxpas 2>/dev/null
if [ $? -eq 0 ]; then
echo -n 'Running FauxPas...'
if [ "$projectCount" = "1" ]
then
fauxpas -o json check $projectFile --workspace $workspaceFile --scheme $appScheme > sonar-reports/fauxpas.json
else
echo $projectFile | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh
while read projectName; do
$XCODEBUILD_CMD -list -project $projectName | sed -n '/Schemes/,$p' | while read scheme
do
if [ "$scheme" = "" ]
then
exit
fi
if [ "$scheme" == "${scheme/Schemes/}" ]
then
if [ "$scheme" != "$testScheme" ]
then
projectBaseDir=$(dirname $projectName)
workspaceRelativePath=$(python -c "import os.path; print os.path.relpath('$workspaceFile', '$projectBaseDir')")
fauxpas -o json check $projectName --workspace $workspaceRelativePath --scheme $scheme > sonar-reports/$(basename $projectName .xcodeproj)-$scheme-fauxpas.json
fi
fi
done
done < tmpFileRunSonarSh
rm -rf tmpFileRunSonarSh
fi
else
echo 'Skipping FauxPas (not installed)'
fi
else
echo 'Skipping FauxPas'
fi
# Lizard Complexity
if [ "$lizard" = "on" ]; then
if hash $LIZARD_CMD 2>/dev/null; then
echo -n 'Running Lizard...'
$LIZARD_CMD --xml "$srcDirs" > sonar-reports/lizard-report.xml
else
echo 'Skipping Lizard (not installed!)'
fi
else
echo 'Skipping Lizard (test purposes only!)'
fi
# The project version from properties file
numVersionSonarRunner=''; readParameter numVersionSonarRunner 'sonar.projectVersion'
if [ -z "$numVersionSonarRunner" -o "$numVersionSonarRunner" = " " ]; then
numVersionSonarRunner=" --define sonar.projectVersion=$numVerionFromPlist"
else
#if we have version number in properties file, we don't overide numVersion for sonar-runner/sonar-scanner command
numVersionSonarRunner='';
fi
# SonarQube
if [ "$sonarscanner" = "on" ]; then
echo -n 'Running SonarQube using SonarQube Scanner'
if hash /dev/stdout sonar-scanner 2>/dev/null; then
runCommand /dev/stdout sonar-scanner $numVersionSonarRunner
else
echo 'Skipping sonar-scanner (not installed!)'
fi
else
echo -n 'Running SonarQube using SonarQube Runner'
if hash /dev/stdout sonar-runner 2>/dev/null; then
runCommand /dev/stdout sonar-runner $numVersionSonarRunner
else
runCommand /dev/stdout sonar-scanner $numVersionSonarRunner
fi
fi
# Kill progress indicator
stopProgress
exit 0
Here is the terminal output:
Running run-sonar-swift.sh...
Project count is [1]
2021-09-07 14:48:13.089 defaults[49259:620007]
The domain/default pair of (/Users/ebubekirsezer/Desktop/Projects/SonarDemo/SonarDemo/Info.plist, CFBundleShortVersionString) does not exist
Xcode project file is: SonarDemo.xcodeproj
Xcode workspace file is:
Xcode application scheme is: SonarDemo
Number version from plist is:
Destination simulator is: platform=iOS Simulator,name=iPhone 8,OS=14.5
Excluded paths from coverage are:
Creating directory sonar-reports/
Extracting Xcode project information
+ xcodebuild clean build -project SonarDemo.xcodeproj -scheme SonarDemo -destination 'platform=iOS Simulator,name=iPhone 8,OS=14.5' -destination-timeout 360 COMPILER_INDEX_STORE_ENABLE=NO
+ returnValue=0
+ set +x
./run-sonar-swift.sh: line 291: xcpretty: command not found
stat: compile_commands.json: stat: No such file or directory
actual = , min = 3
./run-sonar-swift.sh: line 299: [: -ge: unary operator expected
Running surefire
+ xcodebuild clean build test -project SonarDemo.xcodeproj -scheme SonarDemo -configuration Debug -enableCodeCoverage YES -destination 'platform=iOS Simulator,name=iPhone 8,OS=14.5' -destination-timeout 60
2021-09-07 14:48:26.363 xcodebuild[49322:621312] [MT] DVTAssertions: Warning in /Library/Caches/com.apple.xbs/Sources/IDEFrameworks/IDEFrameworks-18212/IDEFoundation/Testing/IDETestRunSpecificationBuilder.m:678
Details: Failed to compute path to baseline file during test run spec construction: <XCTHTestRunSpecification: 0x7fced2696a60>
Object: <IDETestRunSpecificationBuilder>
Method: +testRunSpecificationsForTestingSpecifiers:scheme:buildables:withBuildParameters:additionalEnvironmentVariables:additionalCommandLineArguments:testRerunPolicy:includeClangProfileParameters:shouldDebugAppExtensions:error:
Thread: <NSThread: 0x7fcf0241a660>{number = 1, name = main}
Please file a bug at https://feedbackassistant.apple.com with this warning message and any useful information you can provide.
2021-09-07 14:48:26.365 xcodebuild[49322:621312] [MT] DVTAssertions: Warning in /Library/Caches/com.apple.xbs/Sources/IDEFrameworks/IDEFrameworks-18212/IDEFoundation/Testing/IDETestRunSpecificationBuilder.m:678
Details: Failed to compute path to baseline file during test run spec construction: <XCTHTestRunSpecification: 0x7fceb25809f0>
Object: <IDETestRunSpecificationBuilder>
Method: +testRunSpecificationsForTestingSpecifiers:scheme:buildables:withBuildParameters:additionalEnvironmentVariables:additionalCommandLineArguments:testRerunPolicy:includeClangProfileParameters:shouldDebugAppExtensions:error:
Thread: <NSThread: 0x7fcf0241a660>{number = 1, name = main}
Please file a bug at https://feedbackassistant.apple.com with this warning message and any useful information you can provide.
2021-09-07 14:50:17.771 xcodebuild[49322:621312] [MT] IDETestOperationsObserverDebug: 110.166 elapsed -- Testing started completed.
2021-09-07 14:50:17.772 xcodebuild[49322:621312] [MT] IDETestOperationsObserverDebug: 0.000 sec, +0.000 sec -- start
2021-09-07 14:50:17.772 xcodebuild[49322:621312] [MT] IDETestOperationsObserverDebug: 110.166 sec, +110.166 sec -- end
+ returnValue=0
+ set +x
./run-sonar-swift.sh: line 323: xcpretty: command not found
mv: build/reports/junit.xml: No such file or directory
\nComputing coverage report\n
Command line exclusion flags for slather is:
slather coverage --binary-basename SonarDemo --input-format profdata --cobertura-xml --output-directory sonar-reports --scheme SonarDemo SonarDemo.xcodeproj
+ slather coverage --binary-basename SonarDemo --input-format profdata --cobertura-xml --output-directory sonar-reports --scheme SonarDemo SonarDemo.xcodeproj
./run-sonar-swift.sh: line 101: slather: command not found
+ returnValue=127
+ set +x
ERROR - Command 'slather coverage --binary-basename SonarDemo --input-format profdata --cobertura-xml --output-directory sonar-reports --scheme SonarDemo SonarDemo.xcodeproj' failed with error code: 127
Doing Jenkins Declarative Pipeline,
So I'm looking for "Post" action that will send e-mail only if there is 1 file which exists and is biggger then 0kb.
Very similiar to bash function : if [-s]
I can do that in a bash script, but how is done in jenkins for POST BUILD:
See how is made in bash:
if [ -s "$file" ]
then
/usr/bin/mutt -s "Master Failed 3Days" $emails -a $file < /dev/null
exit 1
else
echo "$file is empty."
/usr/bin/mutt -s "Master Success 3Days" $emails_success -a $file < /dev/null
exit 0
fi
This approach is working:
1. You are doing a global variable with your shell script in stage
result = sh(script: '''
if [ -s "$file" ]
then
/usr/bin/mutt -s "Master Failed 3Days" $emails -a $file < /dev/null
echo 'found'
else
echo "$file is empty."
/usr/bin/mutt -s "Master Success 3Days" $emails_success -a $file < /dev/null
echo 'not'
fi
''', returnStdout: true).trim()
You are adding post definitions
post {
always {
script {
if (result.contains('found')) {
<your logic to sending email>
}
}
}
}
Here is my groovy file
timestamps{
node('cf_slave'){
checkout scm
stage('Read the file') {
def PWD = pwd()
withEnv(["prj_option=${params.project}"]) {
def response =sh(returnStdout: true, script: 'sh \'jenkins/security/get_values.sh\'')
}
}
This is my get_values.sh file
echo "The project option is:" $prj_option
prj_name=$(echo "$prj_option" | tr '[:upper:]' '[:lower:]')
file_name="va_input_file_$prj_name.txt"
echo "The project option is:" $file_name
ls -la
chmod 775 jenkins/security/$file_name
ls -la
get_input_values() {
file=$1
IFS=''
while read line
do
if [ `echo ${line} | grep -E -c -w "NAME_SPACE" ` -gt 0 ]; then
NAME_SPACE=$(echo " ${line}" | cut -d'=' -f2)
echo "The name space value is $NAME_SPACE"
elif [ `echo ${line} | grep -E -c -w "IMAGE_NAMES" ` -gt 0 ]; then
values=$(echo " ${line}" | cut -d'=' -f2)
echo "THE DOCKERIMAGES are $DOCKER_IMAGES_NAMES"
else
echo "Please provide input for namespace and docker images to be scanned by VA_TOOl"
fi
done < ${file}
}
images=$(get_input_values ${file_name})
so here my text file is under jenkins/security folder of gitrepo but unfortunately I am getting this error:
16:05:28 + sh jenkins/security/get_values.sh
16:05:28 jenkins/security/get_values.sh: 16: jenkins/security/get_values.sh: cannot open va_input_file_icp.txt: No such file```
Unfortunately, there is a ticket for this in Jenkins (https://issues.jenkins-ci.org/browse/JENKINS-51245) which was closed as a duplicate of this ticket: (https://issues.jenkins-ci.org/browse/JENKINS-27413)
JENKINS-27413 was raised in 2015, and is still open. The File Parameter appears not to work in Jenkins Pipeline. It does however work when used in a Freestyle project. While not ideal, I would recommend changing your job to be a Freestyle project if that's feasible.
I want my Jenkins build to fail if the code doesn't have 90% test coverage. For that, I have installed the Quality Gates plugin, which should check the SonarQube analysis.
I have the following configuration in Jenkins, under Quality Gates:
Name: SonarQubeServer
SonarQube Server URL: http://my-server.com:9000
SonarQube account login: admin
SonarQube account password: ****
SonarQube displays: Quality Gate Failed
Jenkins displays: SonarQube analysis completed: SUCCESS and the build passes.
Any idea why Jenkins doesn't get that the quality gate failed?
Eventually I realised that I should have added Quality Gates as a Post Build Action for every job I was using it on.
You can do that with the Shell commands: sharing this info if someone needs it
To mark build as failure when Quality gate is not passed using Sonar Rest api. Add “Execute Shell” after Sonar Step and use below code
Tip : Introduce sleep time of 10s before this step , just to ensure that Sonar site is updated with task result status.
Fetching TASKURL from report-task.txt in workspace
url=$(cat $WORKSPACE/.sonar/report-task.txt | grep ceTaskUrl | cut -c11- )
Fetching Task attributes from Sonar Server
curl -u admin:${admin_pwd} -L $url | python -m json.tool
Setting up task status to check if sonar scan is completed successfully.
curl -u admin:${admin_pwd} -L $url -o task.json
status=$(python -m json.tool < task.json | grep -i "status" | cut -c20- | sed 's/.(.)$/\1/'| sed 's/.$//' )
echo ${status}
If SonarScan is completed successfully then set analysis ID & URLS.
if [ $status = SUCCESS ]; then
analysisID=$(python -m json.tool < task.json | grep -i "analysisId" | cut -c24- | sed 's/.(.)$/\1/'| sed 's/.$//')
analysisUrl="https://sonar.net/api/qualitygates/project_status?analysisId=${analysisID}
echo ${analysisID}
echo ${analysisUrl}
else
echo "Sonnar run was not sucess"
exit 1
fi
Fetching SonarGate details using analysis URL
curl -u admin:$admin_pwd ${analysisUrl} | python -m json.tool
curl -u admin:$admin_pwd ${analysisUrl} | python -m json.tool | grep -i "status" | cut -c28- | sed 's/.$//' >> tmp.txt
cat tmp.txt
sed -n '/ERROR/p' tmp.txt >> error.txt
cat error.txt
if [ $(cat error.txt | wc -l) -eq 0 ]; then
echo "Quality Gate Passed ! Setting up SonarQube Job Status to Success ! "
else
exit 1
echo "Quality Gate Failed ! Setting up SonarQube Job Status to Failure ! "
fi
Cleaning up the files
unset url
unset status
unset analysisID
unset analysisUrl
task.json
tmp.txt
error.txt
In response to Sri who has some type/errors in his solution.
This is sonar4.5.5 building using sonar-scanner
if [ -e tmp.txt ];
then
rm tmp.txt
rm error.txt
rm task.json
fi
sleep 5
cat $WORKSPACE/.scannerwork/report-task.txt
url=$(cat $WORKSPACE/.scannerwork/report-task.txt | grep ceTaskUrl | cut -c11- )
echo $url
curl -u admin:pswd -L $url | python -m json.tool
curl -u admin:pswd -L $url -o task.json
status=$(python -m json.tool < task.json | grep -i "status" | cut --delimiter=: --fields=2 | sed 's/"//g' | sed 's/,//g' )
echo ${status}
if [ $status = SUCCESS ]; then
analysisID=$(python -m json.tool < task.json | grep -i "analysisId" | cut -c24- | sed 's/"//g' | sed 's/,//g')
analysisUrl="http://sonarserver/sonarqube/api/qualitygates/project_status?analysisId=${analysisID}"
echo ${analysisID}
echo ${analysisUrl}
else
echo "Sonar run was not success"
exit 1
fi
curl -u admin:pswd ${analysisUrl} | python -m json.tool
curl -u admin:pswd ${analysisUrl} | python -m json.tool | grep -i "status" | cut -c28- | sed 's/.$//' >> tmp.txt
cat tmp.txt
sed -n '/ERROR/p' tmp.txt >> error.txt
cat error.txt
if [ $(cat error.txt | wc -l) -eq 0 ]; then
echo "Quality Gate Passed ! Setting up SonarQube Job Status to Success ! "
else
echo "Quality Gate Failed ! Setting up SonarQube Job Status to Failure ! "
exit 1
fi
the plugin quality gates return just status :passed or failed , so you can build other job from jenkins from the result of those two flags . but if you want to make flag passed if the coverage resulat >90 % you have to configure it from sonarqube not jenkins . in this situation you can imagine this scenario :
test coverage <90 -> flag :failed . jenkins don't call other job .
test coverage >90 -> flag :passed. jenkins call other job .
i think this can help you somehow .