# Gradle "Failed to publish ... 401 Unauthorized" - Fix Publishing Auth in CI

> Fix Gradle maven-publish "Could not PUT ... received status code 401 from server: Unauthorized" in CI - missing or wrong repository credentials in the publishing block. Inject them from CI secrets.

Source: https://latchkey.dev/learn/java-jvm/gradle-publish-401-unauthorized  
Updated: 2026-06-25

Gradle authenticated to the publish target and was rejected with a 401. The `publishing` block sent no credentials or the wrong ones - the repository `credentials {}` is missing, or the CI secret holding the token is absent or stale.

## Diagnose it: get the real failure out of Gradle

Gradle summarises failures aggressively, and the top-level message frequently describes a downstream symptom rather than the cause. Re-run the failing task with diagnostics before changing build logic.

```Terminal
# the actual stack, plus what Gradle decided about the build
./gradlew <task> --stacktrace --info

# is a stale daemon or cache involved?
./gradlew --stop
./gradlew <task> --no-daemon --no-build-cache

# what does Gradle think the environment is?
./gradlew -version
```

> A JVM or Gradle version mismatch between your machine and the runner explains a large share of CI-only Gradle failures. Pin the JDK with `setup-java` and pin Gradle with the wrapper, then confirm both with `./gradlew -version` in CI.

## Configuration cache and CI

- The configuration cache rejects build logic that reads mutable state at execution time, which is why enabling it surfaces errors an existing build never showed.
- Run with `--configuration-cache-problems=warn` first to see the full list rather than failing on the first one.
- A cached configuration keyed to a different environment is worse than none. Include the JDK version in your cache key.

## FAQ

### What causes Gradle "Failed to publish ... 401 Unauthorized"?

There are 2 common causes: no credentials on the publish repository and wrong or missing ci secret. The maven { url = ...

### How do I fix Gradle "Failed to publish ... 401 Unauthorized"?

There are 2 fixes depending on which cause you have: add credentials from ci secrets to the publish repo and confirm the secret is injected into the job. Work through them in order, since the first is the most common.

### What does Gradle "Failed to publish ... 401 Unauthorized" actually mean?

A publish/publishAllPublicationsTo...Repository task fails with Could not PUT '<url>'.

### How do I stop Gradle "Failed to publish ... 401 Unauthorized" happening again?

Always declare credentials {} on publish repositories, sourced from CI secrets. The prevention section lists 3 changes that keep it from recurring.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
