Maven/gradle release-plugin authentication in gitlab ci

1.3k Views Asked by At

I am using https://github.com/researchgate/gradle-release with the below config in my ci on Gitlab:

release:
image: gradle:jdk11
stage: release
script:
 - git checkout master
 - ./gradlew release
only:
 - master
when: manual

And it complains that the user does not have access to write to the repo.

Execution failed for task ':example-core:preTagCommit'.
> Failed to push to remote - [][remote: You are not allowed to upload code.
fatal: unable to access 'https://gitlab.com/myUser/example.git/': The 
requested URL returned error: 403

I checked and the CI Job Token only has read access. Is there a way to login here as part of the script ideally with a token instead of hard-coding credentials? Thanks.

Edit one: I tried using SSH key/ deploy key with the following config. But it did not work:

release:
image: gradle:jdk11
stage: release
script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client - 
y )'

- eval $(ssh-agent -s)
- ssh-add <(echo "$CI_CD_SSH_KEY")
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- git config --global user.email "[email protected]"
- git config --global user.name "GitLab CI/CD"
- git checkout -B master
- ./gradlew release -DskipTests

Edit two: I have added the correct config here. The trick is to check out the repo after adding the SSH key to the right place and to the SSH agent:

release:
image: gradle:jdk11
stage: ...
before_script:
# Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)

# Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
- echo "$CI_CD_SSH_KEY" | tr -d '\r' | ssh-add -


- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
 # copy the keys to the right place
- ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- git config --global user.email "[email protected]"
- git config --global user.name "GitLab CI/CD"
- git remote set-url origin [email protected]:username/project.git
- git checkout master
# grab the version from the properties file
- VERSION=`grep 'version' ./gradle.properties | grep -oE "(([0-9]{1,3}\.){2} 
[0-9]{1,3})"`

After this, you can then run your script like this:

  script:
- ./gradlew release
0

There are 0 best solutions below