定期触发每13天的Azure管道

huangapple go评论81阅读模式
英文:

Scheduled trigger every 13 days Azure pipeline

问题

我想要添加一个定期触发器,在Azure中每隔13天执行一次流水线。我似乎找不到使用普通的cron作业语法的答案,我看到你可以将其与正则表达式结合使用。起始日不重要。我考虑了类似以下的内容:

schedules:
- cron: "0 1 * * * $((((($(date +%s) / 86400) % 13))==0))"
  displayName: 每隔13天执行

但是我得到了语法错误。如何在Azure中向cron计划的触发器添加正则表达式?

如果您回显正则表达式的结果:echo $((((($(date +%s) / 86400) % 13))==0)),您可以看到它按预期工作,现在关键是将其与yaml文件中的cron作业一起使用。

显然,像这样的内容:0 0 */13 * * 不起作用,因为它将在13号和26号运行,并且从一个月的26号到下个月的13号,超过了13天。

编辑:
0 */312 * * * 会起作用吗?根据https://crontab.guru/#0_/312_**,似乎会起作用。我只是不确定它是否基于24小时制,因此实际上永远不会运行?因为,嗯,每312小时一次,一天中从来没有312小时,但如果它不这样解释,那就应该没问题?
我看到有人在https://stackoverflow.com/questions/4549542/cron-job-every-three-days 写了类似的东西,得到了15个赞。

第二次编辑: /312 不起作用。频率不能高于23,这是有道理的。不要相信Stack上的一切 定期触发每13天的Azure管道

英文:

I want to write add a scheduled trigger to fire a pipeline in Azure once every 13 days. I can't seem to find a answer with the normal cron jobs syntax and I've seen you can combine it with regular expressions. The starting day does not matter. I thought about something like this:

schedules:
- cron: "0 1 * * * $((((($(date +%s) / 86400) % 13))==0))"
displayName: Every 13 days execution

But I am getting a syntax error. How do I add a regex to a cron scheduled triger in azure?

If you echo the result of the regex: echo $((((($(date +%s) / 86400) % 13))==0)), you can see it works as expected, now it is about using it with the cron job in a yaml file.

Obviously, something like: 0 0 */13 * * doesn't work, because it will be on the 13th and 26th and from the 26th of a month, to the 13th of the next, is more than 13 days.

Edit:
Would 0 */312 * * * work? According to https://crontab.guru/#0_/312_** it seems like it would. I'm just unsure if it is 24 hour based and therefore never actually run? Because, well, once every 312 hours, there is never 312 hours in a day, but if doesn't interpret it like that it should be fine?
Saw a guy wrote something similar on https://stackoverflow.com/questions/4549542/cron-job-every-three-days and it got 15 upvotes.

Second edit: The /312 does not work. The frequency cannot be higher than 23, which makes sense. Don't believe everything you read on Stack 定期触发每13天的Azure管道

答案1

得分: 0

>schedules:
- cron: "0 1 * * * $((((($(date +%s) / 86400) % 13))==0))"
  displayName: 每13天执行

>But I am getting a syntax error.

我遇到了语法错误。

This syntax error should be caused by that 6 entries are included where only 5 are needed or you are missing branches parameter in your schedules.

这个语法错误可能是因为你包含了6个条目,而只需要5个,或者你在你的schedules中缺少了branches参数。

>The /312 does not work.

/312 不起作用。

I tried this and it works fine without any error:

我尝试过这个,没有任何错误,它运行良好:

schedules:
  - cron: "0 */312 * * *"
    displayName: 每13天执行
    branches:
      include:
        - master
    always: true

What do you mean by "does not work", do you met any error message? If so, please share the error you met.

你说的“不起作用”是什么意思,你遇到了任何错误消息吗?如果是这样,请分享你遇到的错误。
英文:

>`schedules:

  • cron: "0 1 * * * $((((($(date +%s) / 86400) % 13))==0))"
    displayName: Every 13 days execution`

>But I am getting a syntax error.

This syntax error should be caused by that 6 entries are included where only 5 are needed or you are missing branches parameter in your schedules.

>The /312 does not work.

I tried this and it works fine without any error:

schedules:
  - cron: "0 */312 * * *"
    displayName: Every 13 days execution
    branches:
      include:
        - master
    always: true

What do you mean by "does not work", do you met any error message? If so, please share the error you met.

答案2

得分: 0

请让我知道是否我理解正确,我得出以下结论:

  1. 你不能将像这样的正则表达式添加到yaml文件的cron部分 - 不过,我对此并不确定。我只是无法弄清楚如何做,也没有在线上或Azure的文档中找到任何示例,在yaml文件的cron部分使用正则表达式。

  2. 如果你看我的编辑,我尝试使用 cron 0 */312 * * *,根据 https://crontab.guru/#0_/312_**,它说:“在每第312小时的0分钟。”,但小时是24小时制的,cron是无状态的(它不了解以前的执行情况、运行时间、是否失败等等),因此它永远不会到达第312小时,因为小时实际上是基于时钟小时的。它不会提供语法错误,只是每24小时执行一次。如果你想测试它,我建议你执行以下操作:

    touch /home/user/Desktop/everyminute.log
    crontab -e
    

    在行的末尾添加以下内容:

    */1 * * * * echo "tick $(/bin/date)" >> /home/user/Desktop/everyminute.log
    */61 * * * * echo "tack $(/bin/date)" >> /home/user/Desktop/everyminute.log
    */100 * * * * echo "tock $(/bin/date)" >> /home/user/Desktop/everyminute.log
    

    然后执行 tail -f /home/user/Desktop/everyminute.log

    你会看到每分钟都会出现一个带有时间的tick,当小时过去后,你会看到tick、tack和tock在同一时间执行(一小时后),这证实了它是60索引的(在分钟上),我假设小时也是一样的。

  3. 所以,我最终采用了一种方法。我在yaml文件中添加了 cron 0 13 * * 0,它每周日运行一次,并在实际的bash脚本中加入了 if [ $(($(date +%V) % 2)) -eq 0 ] 在第一行,以检查是否是偶数周。因此,流水线仍然每周执行一次(运行它几乎不占用任何资源),但只有在周数为偶数时才执行作业。顺便说一下,这也意味着它是每14天一次,而不是每13天一次,但我也可以使用它。

英文:

Please let me know if I am wrong, but I have come to the following conclusion:

You cannot add a regex like that into the cron section of the yaml file - however, I am not sure about this. I just couldn't figure out how to do it and didn't find any examples online or at Azure's documentation, where they use regex in the yaml file under the cron section.
2.
If you look at my edits, I tried to do cron 0 */312 * * *, and according to https://crontab.guru/#0_/312_**, it says: “At minute 0 past every 312nd hour.”, but the hours are 24-hour indexed and cron is stateless (it doesn't have know anything about previous executions, the time it ran, if they failed or not etc.) so it will never reach the 312nd hour, since the hours are actual based on the clock-hours. It doesn't provide a syntax error, it just executes every 24 hours instead. If you want to test it, I suggest you do the following:

touch /home/user/Desktop/everyminute.log
crontab -e

Add the following to the end of the line:

*/1 * * * * echo "tick $(/bin/date)" >> /home/user/Desktop/everyminute.log
*/61 * * * * echo "tack $(/bin/date)" >> /home/user/Desktop/everyminute.log
*/100 * * * * echo "tock $(/bin/date)" >> /home/user/Desktop/everyminute.log

and then do a tail -f /home/user/Desktop/everyminute.log.
You'll see the everyminute a tick with the time and when the hour has passed, you'll see a tick, tack and tock hit at the same time (after a hour), which confirms the fact, that it is 60 indexed (when minutes) and I assume the same in hours.

  1. So, I ended up doing a hack. I add: cron 0 13 * * 0 to the yaml file, which runs every week on Sunday, and in my actual bash script that is executed in the pipeine, I added if [ $(($(date +%V) % 2)) -eq 0 ] on line 1, to check if the week is even or odd. So the pipeline is still executed every week (it takes virtually zero ressources to just run it), but it only executes the job, if the week is even. Btw, this also means that it is every 14th day and not every 13th day, but I could use that as well.

huangapple
  • 本文由 发表于 2020年1月6日 17:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定