如何在 github 操作作业步骤中获取差异时间

问题描述 投票:0回答:1

我想找出今天的日期时间和在 githubactions 作业步骤中生成 PR 的日期时间之间的差异。 我该怎么办?

jobs:
  pr-comment:
    runs-on: [self-hosted]
    steps:
      - name: Get diff hours
        id: diff_hours
        run: |
          echo "hours= new Date - context.payload.pull_request.created_at" >> $GITHUB_OUTPUT // I don't know how to useing date format
      - if: steps.diff_hours.outputs.hours > 24
        name: Test. action
        run: ...
github action
1个回答
0
投票

context.payload.pull_request.created_at
string
,所以不能只用它来计算时差。

基本上的想法是将当前日期时间和 pr 日期时间都转换为纪元时间(自 1970 年以来的秒数)并计算差值。

# Get the current date in seconds
current_date_seconds=$(date +%s)

# Convert the custom date to seconds
custom_date_seconds=$(date -d '2023-01-01' +%s)

# Calculate the difference in seconds
difference_seconds=$((current_date_seconds - custom_date_seconds))

# Convert the difference to minutes
difference_days=$((difference_seconds / 60))

Github Actions yaml 格式


run |
echo "difference_in_min=$(( ( $(date +%s) - $(date -d ${{ github.event.pull_request.created_at }} +%s) ) / 60 ))" >>  "$GITHUB_OUTPUT"

有用的链接: 定义作业的输出

© www.soinside.com 2019 - 2024. All rights reserved.