debiangitlabgitlab-cisudebian-jessie

Gitlab-ci, image debian:jessie. Can't change user with su command


I'm using Gitlab-ci for my job with Debian jessie image. Firstly, I need install some packages. Everything is ok. Then, I want to change user from root to a non-root user, but su command doesn't work. Below is a test about changing user.

My .gitlab-ci.yml:

image: debian:jessie

test:
    script:
    - whoami
    - adduser --disabled-password --gecos "" builder
    - su -l builder
    - whoami

My result:

$ whoami
root
$ adduser --disabled-password --gecos "" builder
Adding user `builder' ...
Adding new group `builder' (1000) ...
Adding new user `builder' (1000) with group `builder' ...
Creating home directory `/home/builder' ...
Copying files from `/etc/skel' ...
$ su builder
$ whoami
root

Solution

  • I found an answer for myself:

     image: debian:jessie
    
     test:
         script:
         - whoami
         - adduser --disabled-password --gecos "" builder
         - su -l builder
    -    - whoami
    +    - su builder -c "whoami"
    

    Using command su <username> -c "<command>" will help running command with <username> user, in case mentioned.