Gitlab CI-仅在由CRON触发时才运行管道 [英] Gitlab CI - Run pipeline ONLY when triggered by CRON
问题描述
你好,我是gitlab的新手,到目前为止,我成功完成了所有工作(运行程序,管道等)
Hello I am new to gitlab, and so far i succeded in doing everything (runners, pipelines, etc...)
但是我现在想将自动测试设置为每天仅生成一次,由CRON触发,但不由git repo上的每次推送触发.
But I now want to set automatic tests build only once per day, triggered by a CRON but NOT TRIGGERED by each push made on the git repo.
我正在使用没有新的SCHEDULE功能的8.14.2 gitlab
所以我用了这个gitlab.ci.yml:
So I used this gitlab.ci.yml:
stages:
- test
test-karma:
stage: test
image: node:8.9.4
before_script:
- apt-get update
- apt-get --yes install npm
- apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4
libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1
libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6
ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
- npm install @angular/cli@1.4.5
- npm install
environment: dev
script:
- ./node_modules/.bin/ng test --single-run=true --watch=false
only:
- triggers
except:
- pushes
但是,尽管只有/除了一部分,却会触发作业:
But despite the only / except part, the job is triggered :
()对于每个git push(我不想要)
()由我想要的CRON
() For each git push (which i don't want)
() By the CRON which I want
所以我找到了另一种方式
1-在gitlab上将变量SHOULD_EXECUTE设置为false
2-通过if语句保护每个脚本行
3-使用以下CRON:
So I found another way
1- Set a variable SHOULD_EXECUTE to false on gitlab
2- Protect every script line by an if statement
3- Use the following CRON:
00 4 * * * curl -X POST -F token=MY_TOKEN -F ref=master -F "variables[SOULD_EXECUTE]=true" http://MY_GITLAB_URL/api/v3/projects/30/trigger/builds
4-并将gitlab.ci.yml更改为
4- And changed the gitlab.ci.yml to
stages:
- test
test-karma:
stage: test
image: node:8.9.4
before_script:
- if [ ${SOULD_EXECUTE} == "true" ]; then apt-get update;fi
- if [ ${SOULD_EXECUTE} == "true" ]; then apt-get --yes install npm ;fi
- if [ ${SOULD_EXECUTE} == "true" ]; then apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4
libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1
libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6
ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget;fi
- if [ ${SOULD_EXECUTE} == "true" ]; then npm install @angular/cli@1.4.5;fi
- if [ ${SOULD_EXECUTE} == "true" ]; then npm install; else echo "TEST JOB SKIPPED";fi
- echo 'SOULD_EXECUTE => ' + ${SOULD_EXECUTE}
environment: dev
script:
- if [ ${SOULD_EXECUTE} == "true" ]; then ./node_modules/.bin/ng test --single-run=true --watch=false;fi
only:
- triggers
这是丑陋的...有人可以帮我设置这个吗?非常感谢.
It's Ugly... Could some one help me to set this up ? Thank you so much.
致谢.
推荐答案
我修复了问题,却不知道如何或为什么.我不得不将仓库移到gitlab的另一个组中.现在,它运行良好,唯一的:触发器(除了:pushs之外)使我不必为每次按下都运行管道,而我可以使用CRON post调用来运行它.
I fixed my issue without knowing how or why. I had to move the repo to another group in gitlab. And now it's working perfectly, the only : triggers (without except : pushes) enable me to NOT run a pipeline for each pushes, and I can run this with the CRON post call.
这篇关于Gitlab CI-仅在由CRON触发时才运行管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!