能否实现Jenkins构建失败时自动关闭GitLab Merge Request?
Can Jenkins automatically close/delete a GitLab Merge Request when build fails?
Absolutely feasible! The default "Accept gitlab merge request on success" post-build action only handles successful builds, but we can extend this workflow to close (or delete) MRs on failure using GitLab's API and Jenkins' scripting capabilities. Here's a step-by-step guide to set this up:
Prerequisites
- Ensure the GitLab Plugin is installed and properly configured in Jenkins, with a valid GitLab access token that has
apipermissions (to modify merge requests). - Confirm Jenkins can reach your GitLab instance (no firewall restrictions between the two systems).
- Enable "Inject GitLab variables" in your Jenkins job's GitLab configuration to access MR-specific environment variables (like the MR ID) during builds.
Step-by-Step Implementation
Option 1: For Freestyle Projects
- In your freestyle job, navigate to Post-build Actions and add an Execute shell (or Execute Windows batch command if using Windows) step.
- Use this script to check build status and trigger a GitLab API call to close the MR on failure:
# Replace these with your actual GitLab details GITLAB_URL="https://your-gitlab-domain" PROJECT_ID="your-project-id" GITLAB_TOKEN="your-gitlab-access-token" # Jenkins sets BUILD_STATUS to FAILURE if the build fails if [ "$BUILD_STATUS" = "FAILURE" ]; then curl --request PUT \ --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \ "${GITLAB_URL}/api/v4/projects/${PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}?state_event=close" fiCI_MERGE_REQUEST_IIDis an environment variable injected by the GitLab Plugin, representing the internal ID of the target merge request.
Option 2: For Pipeline Projects
If you're using a Jenkins Pipeline, add a post section to handle failure scenarios directly in your pipeline script:
pipeline { agent any stages { // Your existing build/test stages go here stage('Build') { steps { echo 'Running build tasks...' // Add your build commands here } } } post { failure { script { def gitlabConfig = [ url: 'https://your-gitlab-domain', projectId: 'your-project-id', token: credentials('gitlab-api-token'), // Retrieve token from Jenkins Credentials Manager mrIid: env.CI_MERGE_REQUEST_IID ] sh """ curl --request PUT \ --header "PRIVATE-TOKEN: ${gitlabConfig.token}" \ "${gitlabConfig.url}/api/v4/projects/${gitlabConfig.projectId}/merge_requests/${gitlabConfig.mrIid}?state_event=close" """ echo "Closed GitLab MR #${gitlabConfig.mrIid} due to build failure" } } } }
Key Notes
- Close vs Delete: Use
state_event=closeto close the MR (keeps it in GitLab for reference). To delete the MR entirely, replace it withstate_event=delete– this requires the GitLab token to have thedelete_merge_requestsscope. - Security Best Practice: Never hardcode your GitLab access token. Store it in Jenkins' Credentials Manager and retrieve it securely using the
credentials()method (as shown in the pipeline example). - Testing: Intentionally break a build to verify the MR is closed automatically, ensuring all variables and permissions are correctly configured.
内容的提问来源于stack exchange,提问作者L.r
相关产品推荐
相关产品推荐

