GitLab合并请求批准并合并后自动打标签、编译及构建存储的实现方案咨询
Great question! You actually don't need non-official APIs to make this work—GitLab's built-in CI/CD pipelines can handle every part of your workflow, including triggering the job right after a merge request is merged into develop. Let's walk through exactly how to set this up:
1. Core Trigger: React to Merges into develop
The key is to configure your pipeline to run only when an MR is merged into develop (or when code is pushed to develop, which happens automatically after an MR merge). GitLab's rules keyword lets you define this trigger clearly:
- Use
$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop"and$CI_MERGE_REQUEST_STATE == "merged"to target only completed MRs - Or use
$CI_COMMIT_BRANCH == "develop" && $CI_PIPELINE_SOURCE == "push"to catch all pushes todevelop(including MR merges)
2. Auto-Create a Dev Tag After Merge
Once the trigger fires, the first step is to create and push a dev-prefixed tag. You'll use Git commands in the CI job, and GitLab's built-in CI_JOB_TOKEN gives you permission to push tags to the repo.
3. Compile the Source Code
After tagging completes, run your project's compilation command. We'll use needs to ensure this job only runs if the tagging job succeeds.
4. Save Build Artifacts to Your Local Server
Finally, copy the compiled artifacts to your local test server using scp (or rsync for larger files). You'll need to store your server's SSH private key as a CI variable for secure access.
Full .gitlab-ci.yml Example
# Define pipeline stages in order stages: - tag - build - deploy # Job 1: Auto-create dev tag post-merge auto_tag_dev: stage: tag rules: # Trigger only when MR is merged into develop - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" && $CI_MERGE_REQUEST_STATE == "merged"' script: # Configure Git user for tagging - git config --global user.name "GitLab CI Bot" - git config --global user.email "ci-bot@your-company.com" # Generate a unique dev tag (date + short commit hash) - TAG_NAME="dev-$(date +%Y%m%d)-${CI_COMMIT_SHORT_SHA}" # Create and push the tag using CI_JOB_TOKEN - git tag $TAG_NAME - git push https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_REPOSITORY_URL#*@} $TAG_NAME # Job 2: Compile source code compile_source: stage: build needs: [auto_tag_dev] # Only run after tagging succeeds rules: - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" && $CI_MERGE_REQUEST_STATE == "merged"' script: # Replace with your project's compilation commands # Example for Node.js: npm install && npm run build # Example for Java: mvn clean package -DskipTests make clean build # Save compiled artifacts for the next job artifacts: paths: - dist/ # Update this to your project's build output directory expire_in: 1 week # Optional: auto-delete artifacts after 1 week # Job 3: Copy artifacts to local test server save_to_local_server: stage: deploy needs: [compile_source] # Only run after compilation succeeds rules: - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" && $CI_MERGE_REQUEST_STATE == "merged"' script: # Set up SSH access to your local server - mkdir -p ~/.ssh - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa - ssh-keyscan your-local-server-ip >> ~/.ssh/known_hosts # Copy artifacts to server (update path and user as needed) - scp -r dist/ test-user@your-local-server-ip:/var/test-storage/
Key Setup Notes
- Secure Variables: Go to your project's
Settings > CI/CD > Variablesand addSSH_PRIVATE_KEYas a protected and masked variable (this is your private key for accessing the local server). - Runner Requirements: Ensure your GitLab runner has all necessary tools installed (git, ssh, compilers for your project, etc.).
- Tag Customization: Adjust the
TAG_NAMEpattern to fit your team's needs—you could use an incrementing version number instead of date/hash if preferred. - Restrict Direct Pushes: If you want to block direct pushes to
develop(so only MRs trigger the pipeline), enable "Protected Branches" fordevelopin your project settings.
内容的提问来源于stack exchange,提问作者Wentao Wu

