多个合并请求在gitlab中形成一个源分支

时间:2020-04-28 07:02:00

标签: continuous-integration gitlab

我需要创建一个从release-01developstaging的合并请求。

为此,我为自己的工作赚了.gitlab-ci.yml

我没有放所有东西

image: node:13.10.1

default:
  before_script:
    - apt-get update -qq && apt-get install && apt-get -y install yarn
    - yarn config set cache-folder .yarn
    - yarn install --frozen-lockfile
  cache:
    paths:
      - node_modules/

stages:
  - build
  - lint
  - test
  - develop
  - createMRDS
  - staging
  - createMRRD
  - createMRRS
  - createMRSM
  - production

# create auto mr release-* -> staging && develop
CreateMrRS:
  stage: createMRRS
  only:
    - /^release-.*$/
  script:
    - TARGET_BRANCH=staging sh ./.gitlab/autoMergeRequest.sh

CreateMrRD:
  stage: createMRRD
  only:
    - /^release-.*$/
  script:
    - TARGET_BRANCH=develop sh ./.gitlab/autoMergeRequest.sh

和一个执行此操作的脚本autoMergeRequest.sh

#!/usr/bin/env bash
# Extract the host where the server is running, and add the URL to the APIs
HOST="${CI_API_V4_URL}/projects/"
NAME="?️ auto_mr ? api ? ${CI_COMMIT_REF_NAME} > ${TARGET_BRANCH}"
# work in $ version
ASSIGNEE="[3, 2]"

# The description of our new MR, we want to remove the branch after the MR has
# been closed
BODY="{
    \"id\": ${CI_PROJECT_ID},
    \"source_branch\": \"${CI_COMMIT_REF_NAME}\",
    \"target_branch\": \"${TARGET_BRANCH}\",
    \"remove_source_branch\": true,
    \"title\": \"${NAME}\",
    \"assignee_ids\": ${ASSIGNEE},
    \"labels\":\"auto-mr, ${TARGET_BRANCH}\"
}";

# Require a list of all the merge request and take a look if there is already
# one with the same source branch
LISTMR=$(curl --silent "${HOST}${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${GITLAB_PRIVATE_TOKEN}");
COUNTBRANCHES=$(echo "${LISTMR}" | grep -o "\"source_branch\":\"${CI_COMMIT_REF_NAME}\"" | wc -l);

# No MR found, let's create a new one
if [ "${COUNTBRANCHES}" -eq "0" ]; then
    curl -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" \
        --header "PRIVATE-TOKEN:${GITLAB_PRIVATE_TOKEN}" \
        --header "Content-Type: application/json" \
        --data "${BODY}";

    echo "Opened a new merge request: ${NAME}";
    exit;
fi

echo "No new merge request opened";

此脚本可以正常工作(我将其用于其他合并请求),但是对于两个作业createMRRScreateMRRD,它只为我创建了一个先生,而在另一个上没有错误。

我唯一的日志只有(对于未发生的先生)

$ TARGET_BRANCH=develop sh ./.gitlab/autoMergeRequest.sh
No new merge request opened

0 个答案:

没有答案