From 71d1a3945e651436d5de344447c0988dd0e824fc Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 29 May 2018 14:27:17 +0200 Subject: [PATCH] [jenkins] Fix scripts to be shellcheck-happy. A few general categories of fixes: * Sprinkle lots of quotes everywhere. * Don't use environment variables in the format string to printf, instead pass them as arguments. * Don't use backticks to execute commands (it's deprecated), use the new "$(...)" syntax instead. --- jenkins/Makefile | 2 ++ jenkins/build-api-diff.sh | 8 +++++--- jenkins/build.sh | 20 ++++++++++++-------- jenkins/compare.sh | 24 ++++++++++++++---------- jenkins/fetch-pr-labels.sh | 2 ++ jenkins/provision-deps.sh | 8 +++++--- jenkins/run-tests.sh | 26 +++++++++++++++----------- 7 files changed, 55 insertions(+), 35 deletions(-) create mode 100644 jenkins/Makefile diff --git a/jenkins/Makefile b/jenkins/Makefile new file mode 100644 index 000000000000..fdbbe9f05b2d --- /dev/null +++ b/jenkins/Makefile @@ -0,0 +1,2 @@ +all check: + shellcheck *.sh diff --git a/jenkins/build-api-diff.sh b/jenkins/build-api-diff.sh index fd3b9a4f5ff4..7e73b98b227b 100755 --- a/jenkins/build-api-diff.sh +++ b/jenkins/build-api-diff.sh @@ -1,13 +1,15 @@ #!/bin/bash -e +cd "$(dirname "${BASH_SOURCE[0]}")/.." +WORKSPACE=$(pwd) + report_error () { - printf "🔥 [Failed to create API Diff]($BUILD_URL/console) 🔥\\n" >> $WORKSPACE/jenkins/pr-comments.md + printf "🔥 [Failed to create API Diff](%s/console) 🔥\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" } trap report_error ERR -cd $WORKSPACE export BUILD_REVISION=jenkins make -j8 -C tools/apidiff jenkins-api-diff -printf "✅ [API Diff (from stable)]($BUILD_URL/API_20diff_20_28from_20stable_29)\\n" >> $WORKSPACE/jenkins/pr-comments.md +printf "✅ [API Diff (from stable)](%s/API_20diff_20_28from_20stable_29)\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" diff --git a/jenkins/build.sh b/jenkins/build.sh index 1c5dd47f8f19..9872e0e17fa0 100755 --- a/jenkins/build.sh +++ b/jenkins/build.sh @@ -1,27 +1,31 @@ #!/bin/bash -e +cd "$(dirname "${BASH_SOURCE[0]}")/.." +WORKSPACE=$(pwd) + report_error () { - printf "🔥 [Build failed]($BUILD_URL/console) 🔥\\n" >> $WORKSPACE/jenkins/pr-comments.md + printf "🔥 [Build failed](%s/console) 🔥\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" } trap report_error ERR -ls -la $WORKSPACE/jenkins +ls -la "$WORKSPACE/jenkins" echo "$WORKSPACE/jenkins/pr-comments.md:" -cat $WORKSPACE/jenkins/pr-comments.md +cat "$WORKSPACE/jenkins/pr-comments.md" -cd $WORKSPACE export BUILD_REVISION=jenkins ENABLE_DEVICE_BUILD= -if test -z $ghprbPullId; then +# SC2154: ghprbPullId is referenced but not assigned. +# shellcheck disable=SC2154 +if test -z "$ghprbPullId"; then echo "Could not find the environment variable ghprbPullId, so won't check if we're doing a device build." else echo "Listing modified files for pull request #$ghprbPullId..." if git diff-tree --no-commit-id --name-only -r "origin/pr/$ghprbPullId/merge^..origin/pr/$ghprbPullId/merge" > .tmp-files; then echo "Modified files found": - cat .tmp-files | sed 's/^/ /' || true + sed 's/^/ /' .tmp-files || true if grep 'external/mono' .tmp-files > /dev/null; then echo "Enabling device build because mono was bumped." elif grep 'external/llvm' .tmp-files > /dev/null; then @@ -32,7 +36,7 @@ else fi rm -f .tmp-files - if test -z $ENABLE_DEVICE_BUILD; then + if test -z "$ENABLE_DEVICE_BUILD"; then if ./jenkins/fetch-pr-labels.sh --check=enable-device-build; then ENABLE_DEVICE_BUILD=1 echo "Enabling device build because the label 'enable-device-build' was found." @@ -50,4 +54,4 @@ fi time make world -printf "✅ [Build succeeded]($BUILD_URL/console)\\n" >> $WORKSPACE/jenkins/pr-comments.md +printf "✅ [Build succeeded](%s/console)\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" diff --git a/jenkins/compare.sh b/jenkins/compare.sh index f3be6573b0b8..a2f560325d25 100755 --- a/jenkins/compare.sh +++ b/jenkins/compare.sh @@ -1,28 +1,32 @@ #!/bin/bash -e +cd "$(dirname "${BASH_SOURCE[0]}")/.." +WORKSPACE=$(pwd) + report_error () { - printf "🔥 [Failed to compare API and create generator diff]($BUILD_URL/console) 🔥\\n" >> $WORKSPACE/jenkins/pr-comments.md - touch $WORKSPACE/jenkins/failure-stamp + printf "🔥 [Failed to compare API and create generator diff](%s/console) 🔥\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" + touch "$WORKSPACE/jenkins/failure-stamp" exit 0 } trap report_error ERR -cd $WORKSPACE if ./jenkins/fetch-pr-labels.sh --check=skip-api-comparison; then - printf "❎ Skipped API comparison because the PR has the label 'skip-api-comparison'\\n" >> $WORKSPACE/jenkins/pr-comments.md + printf "❎ Skipped API comparison because the PR has the label 'skip-api-comparison'\\n" >> "$WORKSPACE/jenkins/pr-comments.md" exit 0 fi -BASE=origin/pr/$ghprbPullId/merge -if ! git rev-parse $BASE >/dev/null 2>&1; then +# SC2154: ghprbPullId is referenced but not assigned. +# shellcheck disable=SC2154 +BASE="origin/pr/$ghprbPullId/merge" +if ! git rev-parse "$BASE" >/dev/null 2>&1; then echo "Can't compare API and create generator diff because the pull request has conflicts that must be resolved first (the branch '$BASE' doesn't exist)." - printf "🔥 [Failed to compare API and create generator diff because the pull request has conflicts that must be resolved first]($BUILD_URL/console) 🔥\\n" >> $WORKSPACE/jenkins/pr-comments.md + printf "🔥 [Failed to compare API and create generator diff because the pull request has conflicts that must be resolved first](%s/console) 🔥\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" exit 0 fi -./tools/compare-commits.sh --base=$BASE^1 +./tools/compare-commits.sh --base="$BASE^1" mkdir -p jenkins-results/apicomparison @@ -30,5 +34,5 @@ cp -R tools/comparison/apidiff/diff jenkins-results/apicomparison/ cp tools/comparison/apidiff/*.html jenkins-results/apicomparison/ cp -R tools/comparison/generator-diff jenkins-results/generator-diff -printf "✅ [API Diff (from PR only)]($BUILD_URL/API_20diff_20_28PR_20only_29)\\n" >> $WORKSPACE/jenkins/pr-comments.md -printf "✅ [Generator Diff]($BUILD_URL/Generator_20Diff)\\n" >> $WORKSPACE/jenkins/pr-comments.md +printf "✅ [API Diff (from PR only)](%s/API_20diff_20_28PR_20only_29)\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" +printf "✅ [Generator Diff](%s/Generator_20Diff)\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" diff --git a/jenkins/fetch-pr-labels.sh b/jenkins/fetch-pr-labels.sh index 7130809d9871..95c1c738711a 100755 --- a/jenkins/fetch-pr-labels.sh +++ b/jenkins/fetch-pr-labels.sh @@ -1,5 +1,7 @@ #!/bin/bash -e +# SC2154: ghprbPullId is referenced but not assigned. +# shellcheck disable=SC2154 if test -z "$ghprbPullId"; then echo "Could not find the environment variable ghprbPullId, so it's not possible to fetch the labels for any pull request." exit 1 diff --git a/jenkins/provision-deps.sh b/jenkins/provision-deps.sh index 9665773e34e9..fabac52a060a 100755 --- a/jenkins/provision-deps.sh +++ b/jenkins/provision-deps.sh @@ -1,12 +1,14 @@ #!/bin/bash -e +cd "$(dirname "${BASH_SOURCE[0]}")/.." +WORKSPACE=$(pwd) + report_error () { - echo "🔥 [Provisioning failed]($BUILD_URL/console) 🔥" >> $WORKSPACE/jenkins/pr-comments.md + echo "🔥 [Provisioning failed]($BUILD_URL/console) 🔥" >> "$WORKSPACE/jenkins/pr-comments.md" } trap report_error ERR -cd $WORKSPACE ./system-dependencies.sh --provision-all -echo "✅ [Provisioning succeeded]($BUILD_URL/console)" >> $WORKSPACE/jenkins/pr-comments.md +echo "✅ [Provisioning succeeded]($BUILD_URL/console)" >> "$WORKSPACE/jenkins/pr-comments.md" diff --git a/jenkins/run-tests.sh b/jenkins/run-tests.sh index 2848a4368340..75e5b9e8c9b5 100755 --- a/jenkins/run-tests.sh +++ b/jenkins/run-tests.sh @@ -1,31 +1,35 @@ #!/bin/bash -e +cd "$(dirname "${BASH_SOURCE[0]}")/.." +WORKSPACE=$(pwd) + report_error () { - printf "🔥 [Test run failed]($BUILD_URL/Test_20Report/) 🔥\\n" >> $WORKSPACE/jenkins/pr-comments.md + printf "🔥 [Test run failed](%s/Test_20Report) 🔥\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" - if test -f $WORKSPACE/tests/TestSummary.md; then - printf "\\n" >> $WORKSPACE/jenkins/pr-comments.md - cat $WORKSPACE/tests/TestSummary.md >> $WORKSPACE/jenkins/pr-comments.md + if test -f "$WORKSPACE/tests/TestSummary.md"; then + printf "\\n" >> "$WORKSPACE/jenkins/pr-comments.md" + cat "$WORKSPACE/tests/TestSummary.md" >> "$WORKSPACE/jenkins/pr-comments.md" fi - touch $WORKSPACE/jenkins/failure-stamp + touch "$WORKSPACE/jenkins/failure-stamp" } trap report_error ERR export BUILD_REVISION=jenkins -cd $WORKSPACE + # Unlock security default-keychain -s builder.keychain security list-keychains -s builder.keychain echo "Unlock keychain" -security unlock-keychain -p `cat ~/.config/keychain` +security unlock-keychain -p "$(cat ~/.config/"$KEYCHAIN"-keychain)" echo "Increase keychain unlock timeout" security set-keychain-settings -lut 7200 # Prevent dialogs from asking for permissions. # http://stackoverflow.com/a/40039594/183422 -security set-key-partition-list -S apple-tool:,apple: -s -k `cat ~/.config/keychain` builder.keychain +# Discard output since there can be a *lot* of it. +security set-key-partition-list -S apple-tool:,apple: -s -k "$(cat ~/.config/keychain)" builder.keychain >/dev/null 2>&1 # clean mono keypairs (used in tests) rm -rf ~/.config/.mono/keypairs/ @@ -33,10 +37,10 @@ rm -rf ~/.config/.mono/keypairs/ # Run tests make -C tests jenkins -printf "✅ [Test run succeeded]($BUILD_URL/Test_20Report/)\\n" >> $WORKSPACE/jenkins/pr-comments.md +printf "✅ [Test run succeeded](%s/Test_20Report/)\\n" "$BUILD_URL" >> "$WORKSPACE/jenkins/pr-comments.md" -if test -f $WORKSPACE/jenkins/failure-stamp; then +if test -f "$WORKSPACE/jenkins/failure-stamp"; then echo "Something went wrong:" - cat $WORKSPACE/jenkins/pr-comments.md + cat "$WORKSPACE/jenkins/pr-comments.md" exit 1 fi