Run pipeline on gitlab when branch is deleted
Asked Answered
P

4

11

Is it possible to run a pipeline thanks to Gitlab CI/CD after a branch is deleted.

In fact I want to edit file after that branch is removed.

For example, I have two repositories, R1 and R2.

In R1 I have 2 branches

  • B1
  • B2

I want to edit a file from R2 when B1 (which is a R1's branch) is removed.

I know how to access and edit the file but I don't know how can I set a trigger for removed branch.

Phlegethon answered 7/9, 2020 at 14:2 Comment(0)
C
3

@Novikov 's answer is exactly what you need, I also thought at first that it worked only manually, but it works automatically. I add some precision that is not in his answer.

I had a similar utility as you.

  • Repo-1 contains the application code.
  • Repo-2 contains the terraform code to deploy/destroy the infrastructure of the application.

Repo-1 pipeline triggers a pipeline on my repo-2 that will deploy the application and so my need was that deleting a branch in repo-1 triggers the repo-2 pipeline to delete the environment.

So i used the environment feature, the gitlab documentation explain how to use it.

deploy:
  stage: deploy
  script:
    - {...}
  environment:
    name: deployment-${CI_COMMIT_BRANCH}
    on_stop: destroy

destroy:
  stage: deploy
  variables:
    GIT_STRATEGY: none
  script:
    - {...}
  environment: 
    name: deployment-${CI_COMMIT_BRANCH}
    action: stop
  when: manual

IMPORTANT

  • I know that we use when: manual but it work automaticaly when a branch is deleted/merged.
  • Both jobs (deploy - destroy) need have the same stage.
  • GIT_STRATEGY variable need to be set to none otherwise it will try to fetch the repo from a branch that no longer exists and the job will fail.
  • Use a dynamic variable for the environment name otherwise it won't work, each environment must have its own name.
  • It is also important to precise that environment feature does not work with the trigger feature, so if you want to trigger another pipeline you need to use the API.

Source: Gitlab Documentation

Christabella answered 1/2, 2023 at 13:45 Comment(0)
L
2

You can use environments. Look at Stopping an environment.

GitLab will automatically trigger a stop action when the associated branch is deleted. The stop_review job must be in the same stage as the deploy_review job in order for the environment to automatically stop.

deploy_review:
  stage: deploy
  script:
    - echo "Deploy a review app"
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://$CI_ENVIRONMENT_SLUG.example.com
    on_stop: stop_review
  only:
    - branches
  except:
    - master

stop_review:
  stage: deploy
  variables:
    GIT_STRATEGY: none
  script:
    - echo "Implement editing files from R2 here"
  when: manual
  environment:
    name: review/$CI_COMMIT_REF_NAME
    action: stop

It's not fully what you want, but maybe more useful for you.

Lawyer answered 7/9, 2020 at 18:48 Comment(1)
Thank you for your response, but stop_review is not called when I removed a branch, it'll be called only from manual action..Phlegethon
M
0

It is not possible for the pipeline to know when you deleted a branch either via the UI nor via git branch -r -D. Pipelines run as a commit hook on the gitlab server (when you push), and when you click the Run Pipeline button manually in the UI.

That being said, it is possible to delete a branch from a pipeline job, so while I will leave it up to you to determine how you wish to do that, you can add a job that uses the trigger keyword to trigger a pipeline in R2 after a job in your R1 pipeline deletes the branch.

See the trigger docs: https://docs.gitlab.com/ee/ci/yaml/index.html#trigger and look into multi-project pipelines.

So in R2 you have a pipeline which is setup to edit some file In R1, then you would setup a job that deletes the B1 branch and follow that job with a "trigger job" which runs in R1 and sends a command to the Gitlab API to kick off the pipeline in R2.

Melanochroi answered 22/6, 2022 at 6:38 Comment(0)
H
0

Using environments as it is suggested above, is the deleted branch name available in the script then somehow? Because I have to execute some clean up actions related to the deleted branch.

Hydro answered 2/4 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.