You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab CI新手求助:master分支测试构建成功后如何调用脚本?

Hey there! Let me walk you through exactly how to set this up in GitLab CI—since you're new to this, I'll keep it simple and skip the overly technical jargon.

Core Idea

GitLab CI uses a .gitlab-ci.yml file at your project's root to define a "pipeline"—a sequence of stages that run one after another. Only if all jobs in earlier stages succeed will the next stage run. So we’ll add a dedicated stage (let’s call it deploy) that runs your notification script only after your build and test jobs pass on the master branch.

Step-by-Step Setup

1. Create/Edit Your .gitlab-ci.yml

If you don’t already have this file in your project root, create it. This is where all your CI configuration lives.

2. Define Pipeline Stages

First, list the order of stages you want. We’ll stick with build → test → deploy (adjust if your existing pipeline uses different stage names):

stages:
  - build
  - test
  - deploy

3. Add Your Existing Build/Test Jobs

Include your existing build and test jobs (replace the example commands with your actual build/test scripts):

# Build stage - runs your project's build steps
build_job:
  stage: build
  script:
    - echo "Running build commands (e.g., compile, package, etc.)"
    # Replace this with your real build script/commands
  only:
    - master  # Restrict to master branch (remove if you need it for other branches)

# Test stage - runs your tests, only after build succeeds
test_job:
  stage: test
  script:
    - echo "Running tests (e.g., unit tests, integration tests)"
    # Replace with your real test commands
  only:
    - master
  needs: [build_job]  # Ensures this job waits for build_job to finish successfully

4. Add the Notification Job

This is the key part—add a job that runs your notification script only when build and test pass on master:

# Notify server job - runs only after build and test succeed on master
notify_server_job:
  stage: deploy
  script:
    - echo "Triggering server update script..."
    - ./path/to/your/notification-script.sh  # Replace with your script's actual path
  only:
    - master  # Only run this on master branch
  needs: [test_job]  # Waits for test_job to succeed before running

Important Notes

  • Script Location: Make sure your notification-script.sh is in your project repository (so GitLab Runner can access it). If your script lives on the target server, you’ll need to set up SSH access from the Runner to that server (see below).
  • SSH Access for Server Commands: If your script needs to SSH into the target server to run pull/build/restart commands, add these steps to the script section of notify_server_job (first, add your SSH private key as a GitLab CI variable under Settings > CI/CD > Variables named SSH_PRIVATE_KEY):
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan your-server-ip-or-domain >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    
  • Runner Dependencies: Ensure the GitLab Runner (the machine running your CI jobs) has all tools your script needs (e.g., bash, curl, ssh). If you’re using a shared GitLab Runner, most common tools are pre-installed; if it’s a self-hosted Runner, install any missing software.
  • Test the Pipeline: Manually trigger a pipeline on master (via GitLab UI) to verify all stages run in order, and your notification script executes as expected.

内容的提问来源于stack exchange,提问作者Gerolmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:13:39