jenkinsci / jobcacher-plugin

Jenkins plugin that improves build performance for transient agents by caching files
https://plugins.jenkins.io/jobcacher/
MIT License
32 stars 32 forks source link

NullPointerException #70

Closed viceice closed 2 years ago

viceice commented 2 years ago

Jenkins and plugins versions report

Environment ```text Jenkins: 2.346.2 OS: Linux - 5.13.0-1023-azure --- Office-365-Connector:4.17.0 ace-editor:1.1 additional-identities-plugin:1.1 analysis-model-api:10.13.0 ansicolor:1.0.1 antisamy-markup-formatter:2.7 apache-httpcomponents-client-4-api:4.5.13-1.0 authentication-tokens:1.4 authorize-project:1.4.0 aws-credentials:191.vcb_f183ce58b_9 aws-java-sdk:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-cloudformation:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-codebuild:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-ec2:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-ecr:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-ecs:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-elasticbeanstalk:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-iam:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-logs:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-minimal:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-sns:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-sqs:1.12.246-349.v96b_b_f7eb_a_c3c aws-java-sdk-ssm:1.12.246-349.v96b_b_f7eb_a_c3c basic-branch-build-strategies:1.3.2 blueocean:1.25.5 blueocean-autofavorite:1.2.5 blueocean-bitbucket-pipeline:1.25.5 blueocean-commons:1.25.5 blueocean-config:1.25.5 blueocean-core-js:1.25.5 blueocean-dashboard:1.25.5 blueocean-display-url:2.4.1 blueocean-events:1.25.5 blueocean-git-pipeline:1.25.5 blueocean-github-pipeline:1.25.5 blueocean-i18n:1.25.5 blueocean-jwt:1.25.5 blueocean-personalization:1.25.5 blueocean-pipeline-api-impl:1.25.5 blueocean-pipeline-editor:1.25.5 blueocean-pipeline-scm-api:1.25.5 blueocean-rest:1.25.5 blueocean-rest-impl:1.25.5 blueocean-web:1.25.5 bootstrap4-api:4.6.0-5 bootstrap5-api:5.1.3-7 bouncycastle-api:2.26 branch-api:2.1046.v0ca_37783ecc5 build-token-root:151.va_e52fe3215fc caffeine-api:2.9.3-65.v6a_47d0f4d1fe checks-api:1.7.4 cloudbees-bitbucket-branch-source:773.v4b_9b_005b_562b_ cloudbees-folder:6.729.v2b_9d1a_74d673 code-coverage-api:3.0.4 command-launcher:84.v4a_97f2027398 config-file-provider:3.11 credentials:1139.veb_9579fca_33b_ credentials-binding:523.vd859a_4b_122e6 data-tables-api:1.12.1-2 display-url-api:2.3.6 docker-commons:1.19 docker-java-api:3.2.13-37.vf3411c9828b9 docker-plugin:1.2.9 docker-workflow:1.29 durable-task:496.va67c6f9eefa7 echarts-api:5.3.3-1 email-ext:2.90 embeddable-build-status:237.v37e2c03964c9 external-monitor-job:191.v363d0d1efdf8 favorite:2.4.1 font-awesome-api:6.1.1-1 forensics-api:1.15.1 git:4.11.3 git-client:3.11.0 git-forensics:1.9.0 git-parameter:0.9.17 gitea:1.4.3 github:1.34.4 github-api:1.303-400.v35c2d8258028 github-branch-source:1677.v731f745ea_0cf google-oauth-plugin:1.0.6 gravatar:2.2 handlebars:3.0.8 handy-uri-templates-2-api:2.1.8-22.v77d5b_75e6953 htmlpublisher:1.30 jackson2-api:2.13.3-285.vc03c0256d517 jakarta-activation-api:2.0.0-3 jakarta-mail-api:2.0.0-6 javax-activation-api:1.2.0-4 javax-mail-api:1.6.2-7 jaxb:2.3.6-1 jdk-tool:1.5 jenkins-design-language:1.25.5 jjwt-api:0.11.5-77.v646c772fddb_0 jobcacher:264.vb_f4770b_79801 jquery:1.12.4-1 jquery3-api:3.6.0-4 jsch:0.1.55.2 junit:1119.1121.vc43d0fc45561 kubernetes:3670.v6ca_059233222 kubernetes-client-api:5.12.2-193.v26a_6078f65a_9 kubernetes-credentials:0.9.0 ldap:2.10 lockable-resources:2.15 mailer:435.v79ef3972b_5c7 matrix-auth:3.1.5 matrix-project:772.v494f19991984 metrics:4.1.6.2 mina-sshd-api-common:2.8.0-30.vf9df64641cb_d mina-sshd-api-core:2.8.0-30.vf9df64641cb_d momentjs:1.1.1 mstest:1.0.0 nunit:0.27 oauth-credentials:0.5 okhttp-api:4.9.3-105.vb96869f8ac3a pam-auth:1.8 pipeline-build-step:2.18 pipeline-graph-analysis:195.v5812d95a_a_2f9 pipeline-groovy-lib:593.va_a_fc25d520e9 pipeline-input-step:449.v77f0e8b_845c4 pipeline-milestone-step:101.vd572fef9d926 pipeline-model-api:2.2114.v2654ca_721309 pipeline-model-definition:2.2114.v2654ca_721309 pipeline-model-extensions:2.2114.v2654ca_721309 pipeline-multibranch-defaults:2.1 pipeline-rest-api:2.24 pipeline-stage-step:293.v200037eefcd5 pipeline-stage-tags-metadata:2.2114.v2654ca_721309 pipeline-stage-view:2.24 pipeline-utility-steps:2.13.0 plain-credentials:1.8 plugin-util-api:2.17.0 popper-api:1.16.1-3 popper2-api:2.11.5-2 prism-api:1.28.0-2 pubsub-light:1.16 saml:2.298.vc7a_2b_3958628 scm-api:608.vfa_f971c5a_a_e9 scoring-load-balancer:1.0.1 script-security:1175.v4b_d517d6db_f0 sidebar-link:2.2.0 simple-theme-plugin:103.va_161d09c38c7 snakeyaml-api:1.30.2-76.vc104f7ce9870 sse-gateway:1.25 ssh-agent:295.v9ca_a_1c7cc3a_a_ ssh-credentials:291.v8211e4f8efb_c ssh-slaves:1.834.v622da_57f702c sshd:3.242.va_db_9da_b_26a_c3 structs:318.va_f3ccb_729b_71 timestamper:1.18 token-macro:293.v283932a_0a_b_49 translation:1.16 trilead-api:1.67.vc3938a_35172f variant:1.4 versioncolumn:2.2 warnings-ng:9.14.0 windows-slaves:1.8.1 workflow-aggregator:590.v6a_d052e5a_a_b_5 workflow-api:1188.v0016b_4f29881 workflow-basic-steps:980.v82219a_ed188e workflow-cps:2746.v0da_83a_332669 workflow-durable-task-step:1190.vc93d7d457042 workflow-job:1207.ve6191ff089f8 workflow-multibranch:716.vc692a_e52371b_ workflow-scm-step:400.v6b_89a_1317c9a_ workflow-step-api:625.vd896b_f445a_f8 workflow-support:833.va_1c71061486b_ ```

What Operating System are you using (both controller, and any agents involved in the problem)?

Both are on linux. Official jenkins image for controller and a custom ubuntu based vm as agent with docker and an container based on ubuntu too.

Reproduction steps

stage('frontend') {
  agent {
    docker {
      label 'docker && linux'
      image 'ghcr.visualon.de/visualon/builder:2.4.25@sha256:2d9c2200b8663090f380ab32a5ec3765b56e37894af9489022e70fc313d062e2'
    }
  }

  options {
    cache(caches: [[$class: 'ArbitraryFileCache', cacheValidityDecidingFile: 'yarn.lock', compressionMethod: 'TARGZ', excludes: '', includes: '**/*', path: '.yarn/cache']], defaultBranch: 'main', maxCacheSize: 512)
  }

  steps {
    sh 'yarn install'
  }
}

added that to my job (using yarn v3) and it simply fails

Expected Results

it should ignore the missing folder and cache the files after all steps are done

Actual Results

11:19:12  java.lang.NullPointerException
11:19:12    at jenkins.plugins.jobcacher.ArbitraryFileCache.resolvePath(ArbitraryFileCache.java:163)
11:19:12    at jenkins.plugins.jobcacher.ArbitraryFileCache.cache(ArbitraryFileCache.java:146)
11:19:12    at jenkins.plugins.jobcacher.CacheManager.cache(CacheManager.java:64)
11:19:12    at jenkins.plugins.jobcacher.pipeline.CacheStep$ExecutionImpl.start(CacheStep.java:113)
11:19:12    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:322)
11:19:12    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:196)
11:19:12    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:124)
11:19:12    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47)
11:19:12    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
11:19:12    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

Anything else?

No response

repolevedavaj commented 2 years ago

I am currently setting up an environment to reproduce this. Strange is, that this error seems to be triggered because the workspace is not set.

repolevedavaj commented 2 years ago

I was not able yet to reproduce this issue. Is this happening in every execution?

viceice commented 2 years ago

i think yes, don't really remember.

i tried to use minio as backed, but i saw that there was a fix recently. so maybe that was the cause?

i can try again to reproduce next week.

darxriggs commented 2 years ago

I am hitting a similar issue even with the latest tag 281.v338c5559864d (running locally with mvn hpi:run).

It is caused when the file does not (yet) exist that is configured to verify if a cache is up-to-date.

The error is as follows:

FATAL: path validity-file cannot be resolved within the current workspace
java.lang.IllegalStateException: path validity-file cannot be resolved within the current workspace
    at jenkins.plugins.jobcacher.ArbitraryFileCache.getCurrentCacheValidityDecidingFileHash(ArbitraryFileCache.java:236)
    at jenkins.plugins.jobcacher.ArbitraryFileCache.matchesCurrentCacheValidityDecidingFileHash(ArbitraryFileCache.java:229)
    at jenkins.plugins.jobcacher.ArbitraryFileCache.isCacheOutdated(ArbitraryFileCache.java:209)
    at jenkins.plugins.jobcacher.ArbitraryFileCache.resolveValidCache(ArbitraryFileCache.java:188)
    at jenkins.plugins.jobcacher.ArbitraryFileCache.resolveValidCache(ArbitraryFileCache.java:167)
    at jenkins.plugins.jobcacher.ArbitraryFileCache.cache(ArbitraryFileCache.java:148)
    at jenkins.plugins.jobcacher.CacheManager.cache(CacheManager.java:64)
    at jenkins.plugins.jobcacher.CacheWrapper.setUp(CacheWrapper.java:104)
    at jenkins.tasks.SimpleBuildWrapper.setUp(SimpleBuildWrapper.java:294)
    at hudson.model.Build$BuildExecution.doRun(Build.java:157)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:514)
    at hudson.model.Run.execute(Run.java:1888)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:99)
    at hudson.model.Executor.run(Executor.java:431)

I have a simple job to test different details. Its config.xml is as follows:

<?xml version='1.1' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command>echo test &gt; validity-file

mkdir -p path1
echo 1.1 &gt; path1/file1.1
echo 1.2 &gt; path1/file1.2

mkdir -p path2
echo 2 &gt; path2/file2</command>
      <configuredLocalRules/>
    </hudson.tasks.Shell>
  </builders>
  <publishers/>
  <buildWrappers>
    <jenkins.plugins.jobcacher.CacheWrapper plugin="jobcacher@999999-SNAPSHOT">
      <maxCacheSize>32</maxCacheSize>
      <caches>
        <jenkins.plugins.jobcacher.ArbitraryFileCache>
          <path>path1</path>
          <includes>**/*</includes>
          <excludes>path1/file1.2</excludes>
          <useDefaultExcludes>true</useDefaultExcludes>
          <cacheValidityDecidingFile>validity-file</cacheValidityDecidingFile>
          <compressionMethod>ZIP</compressionMethod>
        </jenkins.plugins.jobcacher.ArbitraryFileCache>
      </caches>
      <defaultBranch></defaultBranch>
    </jenkins.plugins.jobcacher.CacheWrapper>
  </buildWrappers>
</project>

I already tried to fix this a while ago but then was wondering of I am maybe using the plugin incorrectly. My fix would be to check for the existence of the file in matchesCurrentCacheValidityDecidingFileHash() before accessing it and to return false if it is absent.

repolevedavaj commented 2 years ago

@darxriggs I agree that this is a similar issue, but I believe that the problem described by @viceice seems to be triggered by a missing workspace. Anyway, if it's a use-case to not fail in case of a missing cacheValidityDecidingFile, we could just log it and continue. @darxriggs do you want to create a separate ticket for it?

viceice commented 2 years ago

not sure why the workspace doesn't exist on that step. maybe because I've disabled default checkout on pipeline level?

my original pipeline was much more complicated.

darxriggs commented 2 years ago

@viceice Could you update the issue title to be more descriptive?

viceice commented 2 years ago

sure, let me try to reproduce again with current version before.

repolevedavaj commented 2 years ago

In the official documentation of the options directive you can find this (https://www.jenkins.io/doc/book/pipeline/syntax/#stage-options):

Inside a stage, the steps in the options directive are invoked before entering the agent or checking any when conditions.

So maybe you need to move the step into your steps and execute your commands within the cache step closure:

steps {
  cache(caches: [[$class: 'ArbitraryFileCache', cacheValidityDecidingFile: 'yarn.lock', compressionMethod: 'TARGZ', excludes: '', includes: '**/*', path: '.yarn/cache']], defaultBranch: 'main', maxCacheSize: 512) {
    sh 'yarn install'
  }
}

This is the way you would do it in a scripted pipeline.

viceice commented 2 years ago

@repolevedavaj Thanks, i used options, because the jenkins ui builder showed it like that. I've now using that and it works.

darxriggs commented 1 year ago

When the plugin doesn't support to use options in declarative pipelines: a) Shouldn't this be mentioned in the README? b) Shouldn't the implementation be adapted so that it does not show up in the pipeline builder?

@viceice Could you provide a screenshot showing how you configured it with the builder when hitting the issue?

viceice commented 1 year ago

@darxriggs see issue description.

darxriggs commented 1 year ago

Yes I know, the description contains the full pipeline code.

The missing piece is, how you came up with this. Was it via Pipeline SyntaxDeclarative Directive GeneratorDirectives → selecting options → adding cache?

The goal is to prevent other users to run into the same issue.

viceice commented 1 year ago

yes, i think I found it that way. didn't remember as it was long ago 🤷‍♂️🤗