How to avoid password when using sudo in gitlab-ci

2019-08-18 06:31发布

问题:

I have a private docker repository where i store my build images. I did copy my registry certificates and updated my /etc/hosts file to authenticate registry from my local machine.

I could login to registry with 'sudo docker login -u xxx -p xxx registry-name:port'

But when i try same docker login command from gitlab-ci stage, it is failing with this error:

sudo: no tty present and no askpass program specified.

This is how i'm trying to achieve this.

ssh manohara@${DEPLOY_SERVER_IP} "sudo docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"

I also tried adding this gitlab-runner ALL=(ALL) NOPASSWD: ALL at the bottom of /etc/sudoers file, but no luck Where am i going wrong?

回答1:

According to this Source you can use

ssh -t remotehost "sudo ./binary"

The -t allocates a pseudo-tty.

Or in your example:

ssh -t manohara@${DEPLOY_SERVER_IP} "sudo docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"