Gitlab Runner - New folder for each build

Viewed 13376

I'm using Gitlab CI for my project. When I push on develop branch, it runs tests and update the code on my test environment (a remote server).

But the gitlab runner is already using the same build folder : builds/a3ac64e9/0/myproject/myproject

But I would like to create a now folder every time :

  • builds/a3ac64e9/1/yproject/myproject
  • builds/a3ac64e9/2/yproject/myproject
  • builds/a3ac64e9/3/yproject/myproject
  • and so on

Using this, I could just update my website by changing a symbolic link pointing to the last runner directory.

Is there a way to configure Gitlab Runner this way ?

3 Answers

While it doesn't make sense to use your build directory as your deployment directory, you can setup a custom build directory

Open config.toml in a text editor: (more info on where to find it here)

Set enabled = true under [runners.custom_build_dir] (more info here)

[runners.custom_build_dir]
  enabled = true

In your .gitlab-ci.yml file, under variables set GIT_CLONE_PATH. It must start with $CI_BUILDS_DIR/, e.g. $CI_BUILDS_DIR/$CI_JOB_ID/$CI_PROJECT_NAME, which will probably give you what you're looking for, although if you have multiple stages, they will have different job IDs. Alternatively, you could try $CI_BUILDS_DIR/$CI_COMMIT_SHA, which would give you a unique folder for each commit. (More info here)

variables:
    GIT_CLONE_PATH: '$CI_BUILDS_DIR/$CI_JOB_ID/$CI_PROJECT_NAME'

Unfortunately there is currently an issue with using GIT_BUILDS_DIR in GIT_CLONE_PATH, if you're using Windows and Powershell, so you may have to do something like this as a work-around, if all your runners have the same build directory: GIT_CLONE_PATH: 'C:\GitLab-Runner/builds/$CI_JOB_ID/$CI_PROJECT_NAME'

You may want to take a look at the variables available to you (predefined variables) to find the most suitable variables for your path.

You might want to read the following answer Changing the build intermediate paths for gitlab-runner

I'll repost my answer here:

Conceptually, this approach is not the way to go; the build directory is not a deployment directory, it's a temporary directory, to build or to deploy from, whereas on a shell executor this could be fixed.

So what you need is to deploy from that directory with a script as per gitlab-ci.yml below, to the correct directory of deployment.

stages:
- deploy

variables:
  TARGET_DIR: /home/ab12/public_html/$CI_PROJECT_NAME

deploy:
  stage: deploy
  script:
     mkdir -pv $TARGET_DIR
     rsync -r --delete ./ $TARGET_DIR
  tags:
    - myrunner

This will move your projectfiles in /home/ab12/public_html/

naming your projects as project1 .. projectn, all your projects could use this same .gitlab-ci.yml file.

You can not achieve this only with Gitlab CI runner configuration, but you can create 2 runners, and assign them exclusively to each branch by using a combination of only and tags keywords.

Assuming your two branches are named master and develop and two runners have been tagged with master_runner and develop_runner tags, your .gitlab-ci.yml can look like this:

master_job:
   <<: *your_job
   only:
     - master
   tags:
     - master_runner

develop_job:
   <<: *your_job
   only:
     - develop
   tags:
     - develop_runner

(<<: *your_job is your actual job that you can factorize)

Related