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

Document how to pass the necessary credentials to allow push to remote GIT repository #33

Open
ghilainm opened this issue Jan 11, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@ghilainm
Copy link

Currently it is not clear how we should pass credentials (or what is the strategy) to configure push to remote git repository, could you please update documentation to clarify that point?

@nedtwigg nedtwigg added the enhancement New feature or request label Jan 11, 2022
@nedtwigg
Copy link
Member

Wow! Not sure how we missed that. If you aren't using SSH:

// similar to https://github.com/ajoberstar/grgit/blob/5766317fbe67ec39faa4632e2b80c2b056f5c124/grgit-core/src/main/groovy/org/ajoberstar/grgit/auth/AuthConfig.groovy
private static CredentialsProvider creds() {
String username = System.getenv(GRGIT_USERNAME_ENV_VAR);
if (username != null) {
String password = System.getenv(GRGIT_PASSWORD_ENV_VAR);
if (password == null) {
password = "";
}
return new UsernamePasswordCredentialsProvider(username, password);
}
username = System.getenv(GH_TOKEN_ENV_VAR);
if (username != null) {
return new UsernamePasswordCredentialsProvider(username, "");
}
return null;
}
private static final String GRGIT_USERNAME_ENV_VAR = "GRGIT_USER";
private static final String GRGIT_PASSWORD_ENV_VAR = "GRGIT_PASS";
private static final String GH_TOKEN_ENV_VAR = "gh_token";

If you are using SSH:

// similar to https://github.com/ajoberstar/grgit/blob/5766317fbe67ec39faa4632e2b80c2b056f5c124/grgit-core/src/main/groovy/org/ajoberstar/grgit/auth/AuthConfig.groovy
private static CredentialsProvider creds() {
String username = System.getenv(GRGIT_USERNAME_ENV_VAR);
if (username != null) {
String password = System.getenv(GRGIT_PASSWORD_ENV_VAR);
if (password == null) {
password = "";
}
return new UsernamePasswordCredentialsProvider(username, password);
}
username = System.getenv(GH_TOKEN_ENV_VAR);
if (username != null) {
return new UsernamePasswordCredentialsProvider(username, "");
}
return null;
}
private static final String GRGIT_USERNAME_ENV_VAR = "GRGIT_USER";
private static final String GRGIT_PASSWORD_ENV_VAR = "GRGIT_PASS";
private static final String GH_TOKEN_ENV_VAR = "gh_token";

Agreed we should document better.

@ghilainm
Copy link
Author

Thanks for your answer. In the mean time I figure out the issue on my side. The problem was when integrating it in gitlab ci. I had to change the branch which was cloned and the remote origin to use another user than the ci user.

This was not that easy to figure out that one, don’t how I could avoid this for the next one who wants to do that.

@nedtwigg
Copy link
Member

How about if git errors looked like this:

### Expected the current branch to be `main` and to push to `origin` because
spotlessChangelog {
  remote 'origin'
  branch 'main'
...

### Actual current branch is: git rev-parse --abbrev-ref HEAD
develop
### Actual remote origin is: git remote show origin
* remote origin
  Fetch URL: https://github.com/diffplug/spotless-changelog
  Push  URL: https://github.com/diffplug/spotless-changelog
  HEAD branch: main
  Remote branches:
    main                                 tracked
    master                               tracked

### You might need to set the remote url with: git remote set-url --push origin https://blahblahblah

### You might need to set credentials with:
- user/pass using environment variables `GRGIT_USER` and `GRGIT_PASS`
- token using environment variable `gh_token`
- ssh using `ssh://` URL and possibly adding `-PsshStrictHostKeyChecking=no` to Gradle command line

### The specific error you hit was
<e.printStackTrace()>

Anything missing from the above which would have helped resolve your case?

@ghilainm
Copy link
Author

Seems good to me!

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

No branches or pull requests

2 participants