diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5696eb0652acddfaf329b19104bc75eddad8d96f..160006bff5d43e9e2bc1d4b2756833c579caa57c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -171,11 +171,25 @@ behave-grafana: - test/**/* - openappstack/**/* -terminate: +# Remove droplet after merge +terminate_droplet_after_merge: + stage: cleanup + script: | + if [ "$(git cat-file -p HEAD | grep -c '^parent')" -gt 1 ] + then + pipeline=$(git cat-file -p 893aed0e1f1ce02a77b8f2a66ad03c24f09915d9 | grep 'Merge branch' | tr -dc '0-9') + echo "Current HEAD is a merge commit, removing droplet from related merge request pipeline #${pipeline}." + python3 -c "import greenhost_cloud; greenhost_cloud.terminate_droplets_by_name(\"^ci-${pipeline}\.\")" + else + echo "Current HEAD is NOT a merge commit, nothing to do." + fi + only: + refs: + - master + +terminate_old_droplets: stage: cleanup script: - # Remove droplet after successful tests - - echo "$CI_COMMIT_MESSAGE" | grep '!ci_dont_terminate' && echo 'Termination of droplet disabled in commit message.' || python3 -m openappstack $HOSTNAME --terminate # Remove droplet older than 2 days - python3 -c "import greenhost_cloud; greenhost_cloud.terminate_droplets_by_name(\"^ci-\", 2)" only: @@ -186,7 +200,8 @@ terminate: - test/**/* - openappstack/**/* -# This trivial job works around a Gitlab bug: if no job runs at all due to +# We need one job that run every time (without any `only:` limitation). +# This works around a Gitlab bug: if no job runs at all due to # `only`, Gitlab gets confused and doesn't allow you to merge the MR: # https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html#limitations gitlab-merge-workaround: