actions / cache

Cache dependencies and build outputs in GitHub Actions
MIT License
4.39k stars 1.17k forks source link

Maven documentation example leads to cached settings.xml #1195

Open pcornelissen opened 1 year ago

pcornelissen commented 1 year ago

I am a happy user of the cache action and it works great, but it took me basically a whole day to locate the reason for a failing build. I have added settings to the setup-java step that currently runs before the caching step.

The new settings led to changes in the generated ~/.m2/settings.xml file but they had no effect and I almost went crazy because the settings were the same as for a different repo that did not had the same problem.

It turned out that the settings.xml was overwritten by a cached version...

Currently the caching examples suggest to use something like:

   - name: Cache Maven packages
      uses: actions/cache@v3
      with:
        path: ~/.m2
        key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
        restore-keys: ${{ runner.os }}-m2

It would be better to be able to exclude the settings.xml easily, but I haven't found a simple solution for that, but at least we could bust the cache when the settings.xml changes, so I'd suggest to update the documentation to something like:

    - name: Cache Maven packages
      uses: actions/cache@v3
      with:
        path: ~/.m2
        key: ${{ runner.os }}-m2-${{ hashFiles('~/.m2/settings.xml') }}-${{ hashFiles('**/pom.xml') }}
        restore-keys: ${{ runner.os }}-m2-${{ hashFiles('~/.m2/settings.xml') }}

Is that approach ok or are there better solutions?

cyrilou242 commented 1 year ago

I use:

with:
  path: |
    ~/.m2/repository
    ~/.m2/wrapper

I suspect you could also do this (not tested)

with:
  path: |
    ~/.m2/
    !~/.m2/settings.xml

I prefer the first version because it's less exposed to some programs writing in m2 without you knowing.

github-actions[bot] commented 4 months ago

This issue is stale because it has been open for 200 days with no activity. Leave a comment to avoid closing this issue in 5 days.

pcornelissen commented 4 months ago

It would be really good to have better documentation that is ready for prime time and doesn't lead to weird pipeline bugs :-)

pdurbin commented 2 months ago

Thanks for the conversation here. It helped me a lot. Plus https://mxschmitt.github.io/action-tmate/ to see what what going on with my settings.xml file.

Changing path: ~/.m2 to path: ~/.m2/repository was what I need to prevent setting.xml from being overwritten.

When I look at https://github.com/actions/cache/blob/v4.0.2/examples.md#java---maven (the latest example) it does show path: ~/.m2/repository already:

- name: Cache local Maven repository
  uses: actions/cache@v3
  with:
    path: ~/.m2/repository
    key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
    restore-keys: |
      ${{ runner.os }}-maven-

So is there still something to fix? Using just path: ~/.m2 does seem to be a gotcha. Maybe it should be written up somewhere? šŸ¤·