Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Make build notification in chat show failed build command #1672

Closed
peterzhuamazon opened this issue Feb 25, 2022 · 5 comments
Closed
Assignees
Labels
enhancement New Enhancement

Comments

@peterzhuamazon
Copy link
Member

It would be easier for us to debug the failure of build pipelines without the need to log dive.

If the build bot is able to print the failed build command in notification chat, it would be easier to reproduce the issue and save time.

Ex:

BUILD FAILED in 26s
1 actionable task: 1 executed
2022-02-25 17:17:28 ERROR    Error building security, retry with: ./build.sh manifests/1.3.0/opensearch-1.3.0.yml --component security --snapshot

Thanks.

@peterzhuamazon peterzhuamazon added the enhancement New Enhancement label Feb 25, 2022
@peterzhuamazon
Copy link
Member Author

@prudhvigodithi
Copy link
Member

prudhvigodithi commented Mar 15, 2022

We can achieve this in the following ways:

1)Consume the logs of the current job and parse for the error, this is little bit complex as each build fail for different error.
Can come up with a shared library as

def BuildFailureCause() {
             
  def cause = "Caused:"
  def error = "ERROR: "
  build.getLog().eachLine { line ->if (line =~ /$cause/ || line =~ /$error/ ) {println "error: $line"  }}
 }

  1. The another way is to consume the current running build logs and dump them as readable message
    Example as def log = currentBuild.rawBuild.getLog(100).join('\n')

  2. We can use try catch block for each stage and get catch the error to convert to toString() format to read the error, but with this we need to add try/catch block to every stage out there in jenkinsfile

        stage('build') {
            steps {
                node('Agent') {
                    script {
                        FAILED_STAGE=env.STAGE_NAME
                        try {
                            echo "test"
                        }
                        catch (Exception e) {
                            ERROR=e.toString()
                            echo 'Exception occurred: ' + e.toString()
                            sh "exit 1"
                        }
                        
                    
                    }
                }
            }

  1. We can combine try/catch block with post script to catch the failed stage and dump the error.

@peterzhuamazon
Copy link
Member Author

Thanks @prudhvigodithi

If you can show some examples of each it would be helpful for us to make a decision on which is suits our needs well.

Thanks.

@prudhvigodithi
Copy link
Member

prudhvigodithi commented Mar 28, 2022

Following Jenkins unit test breaks for the library created for this enhancement.
Lib created: https://github.com/opensearch-project/opensearch-build/blob/c46a0026d1dcd57899a23426238dc13569aa6e43/vars/buildFailureMessage.groovy
Groovy tests failing: https://github.com/opensearch-project/opensearch-build/runs/5713505668?check_suite_focus=true

Background: Created a shared library that a pipeline can inherit to parse the log for actual failure message and pass as input to slack publish shared library, once published a user can view the slack channel with following message
MESSAGE=[2022-03-25 13:20:10 ERROR Error building job-scheduler, retry with: ./build.sh manifests/2.0.0/opensearch-2.0.0.yml --component job-scheduler --snapshot, 2022-03-25 13:21:18 ERROR Error building job-scheduler, retry with: ./build.sh manifests/2.0.0/opensearch-2.0.0.yml --component job-scheduler --snapshot]

Limitation: with this above setup we need to use the method currentBuild.getRawBuild().getLogReader() to run on the same pipeline to parse the existing pipeline build logs as post() action.
The current jenkins unit test frame work has limitation with approach, not able to create an exact mock the currentBuild.getRawBuild().getLogReader() with test pipeline, hence the groovy workflow fails with testing the library.

So far able to mock currentBuild.getRawBuild() by following hack as

def buildMock = [
                    fullProjectName: 'testProjectName',
                    displayName: 'testDisplayName',
                    result: 'SUCCESS',
                    getPreviousBuild: {
                        return null
                    },
                    getRawBuild: { cnt -> return ['Setting http proxy: proxy.domain.com:8080',
                                ' > git fetch --no-tags --progress https://github.com/test/jenkins-library.git +refs/heads/*:refs/remotes/origin/*',
                                'Checking out Revision myUniqueCommitId (master)',
                                ' > git config core.sparsecheckout # timeout=10',
                                ' > git checkout -f myUniqueCommitId',
                                'Commit message: "Merge pull request #147 from marcusholl/pr/useGitRevParseForInsideGitRepoCheck"',
                                ' > git rev-list --no-walk myUniqueCommitId # timeout=10',
                                '[Pipeline] node',
                                'Running on Jenkins in /var/jenkins_home/workspace/Test/UserId/ECHO',
                                '[Pipeline] {',
                                '[Pipeline] stage',
                                '[Pipeline] { (A)',
                                '[Pipeline] script',
                                '[Pipeline] {']}
        ]
        currentBuild = buildMock
        binding.setVariable("currentBuild", currentBuild

Bu this still fails groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.getLogReader()

@prudhvigodithi
Copy link
Member

Fixed, added detailed error message to publish to slack channel
MESSAGE=[2022-03-29 20:11:36 ERROR Error building job-scheduler, retry with: ./build.sh manifests/1.4.0/opensearch-1.4.0.yml --component job-scheduler, 2022-03-29 20:12:32 ERROR Error building job-scheduler, retry with: ./build.sh manifests/1.4.0/opensearch-1.4.0.yml --component job-scheduler, 2022-03-29 20:12:37 ERROR Error building job-scheduler, retry with: ./build.sh manifests/1.4.0/opensearch-1.4.0.yml --component job-scheduler --snapshot, 2022-03-29 20:21:57 ERROR Error building job-scheduler, retry with: ./build.sh manifests/1.4.0/opensearch-1.4.0.yml --component job-scheduler --snapshot]
#1819

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants