0

I have a job which creates builds with "sudo" or root privileges. Hence, when directory cleanup occurs at the start of a job run in GitLab CI-CD pipeline, I get the error as "Permission denied" while cleaning repository items.

I tried implementing a command with sudo rm -r * in after_script, but turns out the artifacts are uploaded after scripts are executed in following order:

  1. before_script

  2. script

  3. after_script

  4. artifacts uploading

What am I trying to achive:

I am in search of a way to clean the project directory after the artifacts are uploaded.

OR

If I could specify cloning of the repository into a particular(custom) directory.

I am fairly new to Gitlab Pipelines. Any help will be appreciated!

Tech Girl
  • 169
  • 2
  • 17
  • Are you using your own `gitlab-runners` to run your Pipeline or shared runners (if you're using gitlab.com)? If using your own, do you know what `executor` you're using? – Adam Marshall Mar 25 '21 at 18:53
  • 1
    @AdamMarshall Yes I am using my own gitlab runner. Also found the solution by using ```pre_clone_script```. – Tech Girl Apr 01 '21 at 11:47
  • 1
    Glad you figured it out! If you can, post what you did to solve your question in an answer and mark it as "Accepted" so that others who have the same problem know what you did to solve it. – Adam Marshall Apr 01 '21 at 16:00

1 Answers1

0

I figured out a way to solve this issue. Posting here the solution in case anyone faces similar issue.

There are certain set of commands which I wanted to execute after the job runs correctly and before cloning of git repository for another job on same build server. I achieved it by doing the following:

In /etc/gitlab-runner/config.toml add pre_clone_script key as below:

pre_clone_script = "<your commands here - for ex: sudo rm *>"

This script runs before cloning of the git repository takes place.

Tech Girl
  • 169
  • 2
  • 17
  • Can you show a working example of `pre_clone_script` with multiple commands? Will it work with pipes? e.g. `wget ... | sudo bash` – Frak Nov 17 '21 at 21:21