Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a script before the branch is deleted in GitLab-CI

GitLab-CI executes the stop-environment script in dynamic environments after the branch has been deleted. This effectively forces you to put all the teardown logic into the .gitlab-ci.yml instead of a script that .gitlab-ci.yml just calls.

Does anyone know a workaround for this? I have a shell script that removes the deployment. This script is part of the repository and can also be called locally (i.e. not onli in an CI environment). I want GitLab-CI to call this script when removing a dynamic environment but it's obviously not there anymore when the branch has been deleted. I also cannot put this script to the artifacts as it is generated before the build by a configure script and contains secrets. It would be great if one could execute the teardown script before the branch is deleted.

Here's a relevant excerpt from the .gitlab-ci.yml

deploy_dynamic_staging:
    stage: deploy
    variables:
        SERVICE_NAME: foo-service-$CI_BUILD_REF_SLUG
    script:
        - ./configure
        - make deploy.staging
    environment:
        name: staging/$CI_BUILD_REF_SLUG
        on_stop: stop_dynamic_staging
    except:
        - master

stop_dynamic_staging:
    stage: deploy
    variables:
        GIT_STRATEGY: none
    script:
        - make teardown # <- this fails
    when: manual
    environment:
        name: staging/$CI_BUILD_REF_SLUG
        action: stop
like image 733
Bastian Venthur Avatar asked Jul 03 '17 09:07

Bastian Venthur


People also ask

How do I delete a branch in GitLab CI?

GitLab delete remote branch overview Open a Terminal window in the gitlab-made-easy repository on the client machine; Switch to the master branch via the 'git checkout' command; Delete the branch locally; Push to origin with the –delete flag; and.

What is the use of dependencies keyword in job configuration in GitLab CI Yml?

dependencies. Note: Introduced in GitSwarm 2016.2 and GitLab Runner v1. 1.1. This feature should be used in conjunction with artifacts and allows you to define the artifacts to pass between different builds.

What are rules in GitLab?

Introduced in GitLab 12.3. Use rules to include or exclude jobs in pipelines. Rules are evaluated in order until the first match. When a match is found, the job is either included or excluded from the pipeline, depending on the configuration.


1 Answers

Probably not ideal, but you can curl the script using the gitlab API before running it:

curl  \
    -X GET https://gitlab.example. com/raw/master/script.sh\
    -H 'PRIVATE-TOKEN: ${GITLAB_TOKEN}' > script.sh
like image 121
Anon20 Avatar answered Sep 30 '22 16:09

Anon20