s4u / maven-settings-action

This action setup maven settings.xml
MIT License
103 stars 24 forks source link

Unexpected token error when running s4u/maven-settings-action@v2.8.0 #293

Open Ardesco opened 1 year ago

Ardesco commented 1 year ago

Describe the bug

I'm getting an unexpected token error:

Run s4u/maven-settings-action@v2.8.0 with: servers: [{"id": "sonatype-nexus-snapshots", "username": "", "password": "", "privateKey": "", "passphrase": ""}] sonatypeSnapshots: false apacheSnapshots: false override: true githubServer: true oracleRepo: false env: JAVA_HOME: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/11.0.19-7/x64 JAVA_HOME_11_X64: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/11.0.19-7/x64 Prepare maven setings: /home/runner/.m2/settings.xml maven settings.xml already exists - override Error: Unexpected token in JSON at position 172

The config I have is:

      - name: Set up Sonatype Snapshots Repository
        uses: s4u/maven-settings-action@v2.8.0
        with:
          servers: '[{"id": "sonatype-nexus-snapshots", "username": "${{ secrets.OSSRH_USERNAME }}", "password": "${{ secrets.OSSRH_TOKEN }}", "privateKey": "${{ secrets.MAVEN_GPG_PRIVATE_KEY }}", "passphrase": "${{ secrets.MAVEN_GPG_PASSPHRASE }}"}]'

It looks like the position lines up with the ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} entry, but it;'s hard to work out why it's upset.

To Reproduce

Being reproduced on all my builds at the moment, but as I'm not sure of the exact cause I can't really provide repo instructions here.

Expected behaviour

My settings.xml setup should complete without issue.

Additional context

Could this be related to some characters in my GPG Private Key? Possible escaping issue maybe? I'm really looking for hints to help diagnose the problem.

Ardesco commented 1 year ago

OK I fixed it by passing in env variable instead of the secrets directly like this:

      - name: Set up Sonatype Snapshots Repository
        uses: s4u/maven-settings-action@v2.8.0
        with:
          githubServer: false
          sonatypeSnapshots: true
          servers: |
            [{
              "id": "sonatype-nexus-snapshots", 
              "username": "${env.MAVEN_USERNAME}", 
              "password": "${env.MAVEN_PASSWORD}", 
              "privateKey": "${env.MAVEN_PRIVATE_KEY}", 
              "passphrase": "${env.MAVEN_PASSPHRASE}"
            }]

      - name: Publish to Sonatype Snapshots Repository
        run: mvn clean deploy -DskipTests
        env:
          MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
          MAVEN_PRIVATE_KEY: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
          MAVEN_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}

Not sure why that difference matters, may be worth adding that to the documentation

slawekjaranowski commented 8 months ago

Probably your password contains a char which brake json parsing.

By the way providing sensitive data is better by environment variable it will not store in settings file.

PR with documentation improvement and examples are welcome 😄