删除分支/标记后,GitLab CI触发管道

时间:2019-06-14 07:53:31

标签: git gitlab yaml gitlab-ci

有一个仓库。 GitLab CI已配置为将工件部署到孤立分支'artifacts / $ BRANCH'。
(也许这不是存储工件的最佳方法,但是对于该项目而言,它足够方便

我想检测删除(合并和删除)BRANCH的时刻,然后运行CI作业以删除相应的artifacts/$BRANCH。 我在文档中寻找解决方案,找到了关键字only/except,但没有合适的方法。

目前,我唯一的想法是对每个管道和/或预定的管道进行清理工作。
真正很酷的解决方案是“删除分支时触发管道”。可能吗?

以下是工件存储过程的实现方式:

## --- STORE ARTIFACTS TO A DEDICATED BRANCH ---
store_artifacts:
  stage: deploy
  dependencies: 
  - build
  variables:
    GIT_STRATEGY: none
  script:
  - echo "COMMIT MESSAGE" >commit_message
  - REV_LABEL=v1.0.2-abracadabra
  ## --------- URL --------------
  - echo "$KI" >/tmp/ki  &&  chmod 400 /tmp/ki
  - export GIT_SSH_COMMAND="ssh -i/tmp/ki -oStrictHostKeyChecking=no"
  - SSH_URL=$(echo "$CI_REPOSITORY_URL" | perl -pe 's#https://(.+?):(.+?)@(.+?)/(.+?).git#git@\3:\4.git#' )
  ## ---------- CLONE, ADD and COMMIT ---------------
  - git clone "$SSH_URL" --depth 1 --single-branch -b artifacts/$CI_COMMIT_REF_NAME ./artifacts  ||  { git init artifacts && git -C artifacts checkout --orphan artifacts/$CI_COMMIT_REF_NAME; }
  - echo >README.md.tmp  "## AVDS $REV_LABEL"
  #\n \n\n### Changelog\n\n\n---\n\n"
  - for f in * ;do F="$REV_LABEL-$f"; ln $f artifacts/$F || { echo >&2 "Forced ln $f"; ln -f $f artifacts/$F; }; git -C artifacts add $F; echo >>README.md.tmp  "$F  "; done
  - echo -e >>README.md.tmp  "\n## Changelog\n$(cat commit_message)\n\n---\n"  
  - cat artifacts/README.md >>README.md.tmp  || true
  - mv README.md.tmp artifacts/README.md
  - cd artifacts
  - git add README.md
  - DEBUG git status 
  - git config --global user.email "ci@sledge.space"
  - git config --global user.name "GitLab CI"
  - git commit -m"$REV_LABEL

    $(cat commit_message)
    "
  ## ------------ PUSH --------------
  - git push "$SSH_URL" HEAD:artifacts/$CI_COMMIT_REF_NAME
  ## ----------- EPILOG -----------
  - echo "See artifacts at "$'\e[1;37m'"$CI_PROJECT_URL/tree/artifacts/$CI_COMMIT_REF_NAME"$'\e[0m'

其中KI是专用SSH密钥。答案末尾有更多内容。

1 个答案:

答案 0 :(得分:0)

解决方法的想法已经实现:

before_script:
- function DEBUG { >&2 "$@" || true; }

remove_stale_artifacts:
  stage: deploy
  dependencies:
  image: alpine
  variables:
    GIT_CHECKOUT: "false"
    GIT_SUBMODULE_STRATEGY: none
  script:
  - apk add --no-cache git perl openssh-client
  - mkdir /temp  &&  mount -t tmpfs -o size=50m tmpfs /temp
  ## --- FIND STALED ARTIFACTS BRANCHES ---
  - git branch -r  --list origin/artifacts/\* | grep -v HEAD | sed -e s#origin/##g -Ee s#^\\s+##g >/temp/ARTIFACTS_BRANCHES
  - DEBUG cat /temp/ARTIFACTS_BRANCHES
  - git branch -r  --list origin/\*           | grep -v -e HEAD -e artifacts/| sed -e s#origin/##g -Ee s#^\\s+#artifacts/#g >/temp/BRANCHES
  - DEBUG cat /temp/BRANCHES
  - fgrep -vf /temp/BRANCHES /temp/ARTIFACTS_BRANCHES >/temp/STALE_ARTIFACTS_BRANCHES || return 0
  - DEBUG cat /temp/STALE_ARTIFACTS_BRANCHES
  - PUSH_SPEC=$(sed -e 's#\s+##g' -e 's#^#:#g' /temp/STALE_ARTIFACTS_BRANCHES)
  - DEBUG echo "$PUSH_SPEC"
  ## --------- URL --------------
  - echo "$KI" >/tmp/ki  &&  chmod 400 /tmp/ki
  - export GIT_SSH_COMMAND="ssh -i/tmp/ki -oStrictHostKeyChecking=no"
  - SSH_URL=$(echo "$CI_REPOSITORY_URL" | perl -pe 's#https://(.+?):(.+?)@(.+?)/(.+?).git#git@\3:\4.git#' )
  - git push "$SSH_URL" $PUSH_SPEC

其中KI是包含私钥SSH密钥的变量。应该在RepoSettings-> CI-> Variables中设置。公钥应添加到部署密钥中,并应允许写访问。