Skip to content

Commit

Permalink
Direct gradle output to log file if debug flag used (#76)
Browse files Browse the repository at this point in the history
* Dependency update

* Not generic working

* Direct gradle output to log file if debug flag used

* Left outs

* typos

* Fix ci

* Fix CI

Co-authored-by: Tamas Papik <[email protected]>
  • Loading branch information
adborbas and trapacska authored Jul 23, 2020
1 parent 202f792 commit 90953f9
Show file tree
Hide file tree
Showing 142 changed files with 52,317 additions and 24 deletions.
98 changes: 93 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ workflows:
- GRADLE_FILE: ./build.gradle
- GRADLE_TASK: assembleDebug
- GRADLEW_PATH: ./gradlew
- GRADLE_OPTIONS: "--debug"
- APK_FILE_EXCLUDE_FILTER: "*-unaligned.apk"
- MAPPING_FILE_INCLUDE_FILTER: "*/release/mapping.txt"
after_run:
- _common
- _check_output_apk
- _check_log_output

test-aab:
description: "Use app filter input"
Expand All @@ -53,6 +55,7 @@ workflows:
- GRADLE_FILE: ./build.gradle
- GRADLE_TASK: bundle
- GRADLEW_PATH: ./gradlew
- GRADLE_OPTIONS: ""
- APP_FILE_EXCLUDE_FILTER: "*-unaligned.apk"
- MAPPING_FILE_INCLUDE_FILTER: "*/release/mapping.txt"
after_run:
Expand All @@ -66,6 +69,7 @@ workflows:
- GRADLE_FILE: ./build.gradle
- GRADLE_TASK: assembleDebug
- GRADLEW_PATH: gradlew
- GRADLE_OPTIONS: ""
- APK_FILE_EXCLUDE_FILTER: "*-unaligned.apk"
- MAPPING_FILE_INCLUDE_FILTER: "*/release/mapping.txt"
after_run:
Expand All @@ -78,6 +82,7 @@ workflows:
- GRADLE_FILE: ./build.gradle
- GRADLE_TASK: assembleDebug assembleRelease
- GRADLEW_PATH: ./gradlew
- GRADLE_OPTIONS: ""
- APK_FILE_EXCLUDE_FILTER: "*-unaligned.apk"
- MAPPING_FILE_INCLUDE_FILTER: "*/release/mapping.txt"
after_run:
Expand All @@ -90,6 +95,7 @@ workflows:
- GRADLE_FILE: ./src/build.gradle
- GRADLE_TASK: assembleDebug
- GRADLEW_PATH: ./src/gradlew
- GRADLE_OPTIONS: ""
- APK_FILE_EXCLUDE_FILTER: "*-unaligned.apk"
- MAPPING_FILE_INCLUDE_FILTER: "*/release/mapping.txt"
after_run:
Expand All @@ -102,6 +108,7 @@ workflows:
- GRADLE_FILE: ./build.gradle
- GRADLE_TASK: assembleDebug
- GRADLEW_PATH: ""
- GRADLE_OPTIONS: ""
- APK_FILE_EXCLUDE_FILTER: "*-unaligned.apk"
- MAPPING_FILE_INCLUDE_FILTER: "*/release/mapping.txt"
after_run:
Expand All @@ -114,6 +121,7 @@ workflows:
- GRADLE_FILE: ""
- GRADLE_TASK: assembleDebug
- GRADLEW_PATH: "./gradlew"
- GRADLE_OPTIONS: ""
- APK_FILE_EXCLUDE_FILTER: "*-unaligned.apk"
- MAPPING_FILE_INCLUDE_FILTER: "*/release/mapping.txt"
after_run:
Expand All @@ -126,6 +134,7 @@ workflows:
- GRADLE_FILE: ""
- GRADLE_TASK: assembleDebug
- GRADLEW_PATH: "./gradlew"
- GRADLE_OPTIONS: ""
- APK_FILE_EXCLUDE_FILTER: "*-unaligned.apk"
- MAPPING_FILE_INCLUDE_FILTER: "*/release/mapping.txt"
after_run:
Expand Down Expand Up @@ -172,11 +181,15 @@ workflows:
git remote add origin "${SAMPLE_APP_URL}"
git fetch || exit 1
[[ -n "${COMMIT}" ]] && git checkout "${COMMIT}" || git checkout "${BRANCH}"
- install-missing-android-tools:
inputs:
- gradlew_path: $GRADLEW_PATH
- path::./:
inputs:
- gradle_file: $GRADLE_FILE
- gradle_task: $GRADLE_TASK
- gradlew_path: $GRADLEW_PATH
- gradle_options: $GRADLE_OPTIONS
- apk_file_include_filter: $APK_FILE_INCLUDE_FILTER
- apk_file_exclude_filter: $APK_FILE_EXCLUDE_FILTER
- mapping_file_include_filter: $MAPPING_FILE_INCLUDE_FILTER
Expand Down Expand Up @@ -218,6 +231,25 @@ workflows:
exit 1
fi
echo "APK found at ${BITRISE_APK_PATH}"
_check_log_output:
steps:
- script:
title: Check if log file exists and environment variable is set
inputs:
- content: |-
if [ -z "$BITRISE_GRADLE_RAW_RESULT_TEXT_PATH" ] ; then
echo "BITRISE_GRADLE_RAW_RESULT_TEXT_PATH env is empty"
exit 1
fi
if [ ! -f $BITRISE_GRADLE_RAW_RESULT_TEXT_PATH ]; then
echo "Log file not found at ${BITRISE_GRADLE_RAW_RESULT_TEXT_PATH}"
exit 1
fi
echo "Log file found at ${BITRISE_GRADLE_RAW_RESULT_TEXT_PATH}"
envman add --key BITRISE_GRADLE_RAW_RESULT_TEXT_PATH --value ""
# ----------------------------------------------------------------
# --- Utility workflows
Expand Down
61 changes: 42 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/bitrise-io/go-android/cache"
"github.com/bitrise-io/go-steputils/commandhelper"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
Expand All @@ -19,19 +20,23 @@ import (
"github.com/kballard/go-shellquote"
)

const failedToFindTargetWithHashString = `Failed to find target with hash string `
const failedToFindBuildToolRevision = `Failed to find Build Tools revision `
const failedToFindPlatformSDKWithPath = `Failed to find Platform SDK with path: `
const couldNotHEAD = `Could not HEAD `
const connectionTimedOut = `Connection timed out`
const couldNotRead = `Could not read `
const couldNotGetResource = `Could not get resource `
const couldNotGET = `Could not GET `
const couldNotDownload = `Could not download `
const receivedStatusCode503 = `Received status code 503 from server: Service Temporarily Unavailable`
const causeErrorInOpeningZipFile = `Cause: error in opening zip file.`
const failedToDownloadResource = `Failed to download resource`
const failedToDownloadSHA1ForResource = `Failed to download SHA1 for resource`
const (
failedToFindTargetWithHashString = `Failed to find target with hash string `
failedToFindBuildToolRevision = `Failed to find Build Tools revision `
failedToFindPlatformSDKWithPath = `Failed to find Platform SDK with path: `
couldNotHEAD = `Could not HEAD `
connectionTimedOut = `Connection timed out`
couldNotRead = `Could not read `
couldNotGetResource = `Could not get resource `
couldNotGET = `Could not GET `
couldNotDownload = `Could not download `
receivedStatusCode503 = `Received status code 503 from server: Service Temporarily Unavailable`
causeErrorInOpeningZipFile = `Cause: error in opening zip file.`
failedToDownloadResource = `Failed to download resource`
failedToDownloadSHA1ForResource = `Failed to download SHA1 for resource`
bitriseGradleResultsTextEnvKey = "BITRISE_GRADLE_RAW_RESULT_TEXT_PATH"
rawGradleResultFileName = "raw-gradle-output.log"
)

var automaticRetryReasonPatterns = []string{
failedToFindTargetWithHashString,
Expand Down Expand Up @@ -98,7 +103,7 @@ func shouldRetry(outputToSearchIn string) (bool, string) {
return isCouldNotFindInOutput(outputToSearchIn)
}

func runGradleTask(gradleTool, buildFile, tasks, options string, isAutomaticRetryOnReason bool) error {
func runGradleTask(gradleTool, buildFile, tasks, options string, isAutomaticRetryOnReason bool, destDir string) error {
optionSlice, err := shellquote.Split(options)
if err != nil {
return err
Expand All @@ -123,20 +128,38 @@ func runGradleTask(gradleTool, buildFile, tasks, options string, isAutomaticRetr
outWriter := io.MultiWriter(os.Stdout, &outBuffer)

cmd := command.New(cmdSlice[0], cmdSlice[1:]...)
cmd.SetStdout(outWriter)
cmd.SetStderr(outWriter)
if err := cmd.Run(); err != nil {

if shouldSaveOutputToLogFile(optionSlice) {
rawOutputLogPath := filepath.Join(destDir, rawGradleResultFileName)
err = commandhelper.RunAndExportOutput(*cmd, rawOutputLogPath, bitriseGradleResultsTextEnvKey, 20)
} else {
cmd.SetStdout(outWriter)
cmd.SetStderr(outWriter)
err = cmd.Run()
}

if err != nil {
if isAutomaticRetryOnReason {
if isRetry, retryReasonPattern := shouldRetry(outBuffer.String()); isRetry {
log.Warnf("Automatic retry reason found in log: %s - retrying...", retryReasonPattern)
return runGradleTask(gradleTool, buildFile, tasks, options, false)
return runGradleTask(gradleTool, buildFile, tasks, options, false, destDir)
}
}
return err
}
return nil
}

func shouldSaveOutputToLogFile(options []string) bool {
for _, option := range options {
if option == "--debug" || option == "-d" {
return true
}
}

return false
}

func filterEmpty(in []string) (out []string) {
for _, item := range in {
if strings.TrimSpace(item) != "" {
Expand Down Expand Up @@ -247,7 +270,7 @@ func main() {
gradleStarted := time.Now()

log.Infof("Running gradle task...")
if err := runGradleTask(gradlewPath, configs.GradleFile, configs.GradleTasks, configs.GradleOptions, configs.RetryOnFailure); err != nil {
if err := runGradleTask(gradlewPath, configs.GradleFile, configs.GradleTasks, configs.GradleOptions, configs.RetryOnFailure, configs.DeployDir); err != nil {
failf("Gradle task failed, error: %s", err)
}

Expand Down
3 changes: 3 additions & 0 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ inputs:
You can use multiple options, separated by a space
character. Example: `--stacktrace --debug`
If `--debug` or `-d` options are set then only the last 20 lines of the raw gradle output will be visible in the build log.
The full raw output will be exported to the $BITRISE_GRADLE_RAW_RESULT_TEXT_PATH variable and will be added as an artifact.
- retry_on_failure: "yes"
opts:
category: Debug
Expand Down
Loading

0 comments on commit 90953f9

Please sign in to comment.