Get current date and time in GitHub workflows

I have a GitHub workflow for releasing nightly snapshots of the repository. It uses the create-release action. This is how the workflow file looks right now:

name: Release Nightly Snapshot

on:
  schedule:
  - cron: "0 0 * * *"

jobs:
  build:
    name: Release Nightly Snapshot
    runs-on: ubuntu-latest
    steps:
      - name: Checkout master Branch
        uses: actions/checkout@v2
        with:
          ref: 'master'
      - name: Create Release
        id: nightly-snapshot
        uses: actions/create-release@latest
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: 'nightly snapshot'
          release_name: 'nightly snapshot'
          draft: false
          prerelease: false

I want tag_name and release_name to use the current date and time, instead of hard-coded values. However, I couldn't find any documentation on it. How should I do it?

Upvotes: 99

Views: 102740

Answers (7)

Avishek Dhar
Avishek Dhar

Reputation: 41

I tried with $GITHUB_OUTPUT but could not fetch it to shown under print summary so instead i used $GITHUB_ENV. Under a job i defined steps and after completing all Jobs i declared the Print summary.

 steps:
      - name: Set current date as env variable
        id: date
        run: echo "date=$(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_ENV

 
- name: Print Summary
    if: always()
    shell: bash
    run: |
      echo '<h1>Job summary:</h1>' >> $GITHUB_STEP_SUMMARY
      echo '' >> $GITHUB_STEP_SUMMARY
      echo '- Triggered event: ${{ github.event_name }}d Job' >> $GITHUB_STEP_SUMMARY
      echo '- EXECUTION_DAY: executed on Date_Time: ${{ env.date}}' >> $GITHUB_STEP_SUMMARY
      echo '- Last Time when Repo Updated:' ${{ github.event.repository.updated_at}} | sed 's/:/./g' >> $GITHUB_STEP_SUMMARY

Upvotes: 0

Lloyd Hamilton
Lloyd Hamilton

Reputation: 320

Update

The set-output command is deprecated and will be disabled soon. Please upgrade to using Environment Files.

Documentation can be found here

name: deploy
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Get current date
        id: date
        run: |
          echo "{date}={$(date +'%Y-%m-%d')}" >> $GITHUB_STATE
      - name: Test with environment variables
        run: echo $TAG_NAME - $RELEASE_NAME
        env:
          TAG_NAME: nightly-tag-${{ env.date }}
          RELEASE_NAME: nightly-release-${{ env.date }}

Upvotes: 8

13013SwagR
13013SwagR

Reputation: 621

A clean solution is to use ${{ github.event.repository.updated_at}} which is pretty close to current datetime$(date +%Y%m%d%H%M)

Format is ISO 8601
e.g 2022-05-15T23:33:38Z

Pros:

  • Doesn't require shell execution
  • Directly accessible from all your workflow
  • Allows cross-referencing with GitHub events

Cons:

  • The serial stays the same when rebuilding on the same commit
  • Strict format
    you can still modify the format in a shell
    e.g. echo ${{ github.event.repository.updated_at}} | sed 's/:/./g'
  • Not available with Act testing framework
  • The value doesn't change on retries as it isn't a repository (Thanks @FabrícioCeolin for pointing this out)

References:
Github context
Event object

Upvotes: 38

Shivam Bharadwaj
Shivam Bharadwaj

Reputation: 2296

# Set the date time
- name: Retrieve current Date Time in Singapore TimeZone
        shell: bash
        run: echo "START_TIME=$(TZ=":Asia/Singapore" date -R|sed 's/.....$//')" >> $GITHUB_ENV

# Fetch the date time as Github ENV variable
- name: print date
        run: echo ${{ env.START_TIME }}

Output-

Mon, 21 Aug 2023 15:34:24

**you can change the Timezone as required

Upvotes: 6

EarlGreytea16
EarlGreytea16

Reputation: 51

if $GITHUB_ENV doesn't work, use $GITHUB_OUTPUT instead:

name: Flutter CI
run-name: ${{ github.actor }} is testing GitHub Actions 🚀
on:
  push:
    tags:
      - '*'
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: "Set current date as env variable"
        run: |
          echo "builddate=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
        id: version  # this is used on variable path

      - name: Publishing Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ steps.version.outputs.builddate }}
          name: ${{ steps.version.outputs.builddate }}
          body: "your date"

      - name: print date
        run: |
          echo $DEBUG
        env:
          DEBUG: ${{ steps.version.outputs.builddate }}-DEBUG
          RELEASE: ${{  steps.version.outputs.builddate }}-RELEASE

Upvotes: 5

creimers
creimers

Reputation: 5303

Here's another way to do this via environment variables (from this post):

name: deploy
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Set current date as env variable
        run: echo "NOW=$(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_ENV
      - name: Echo current date
        run: echo $NOW # Gives "2022-12-11T01:42:20"

This sets the date as an environment variable, which is useful if you want to consume it in scripts / programs in subsequent steps.

Upvotes: 61

Bertrand Martel
Bertrand Martel

Reputation: 45432

From this post you can create a step that set its output with the value $(date +'%Y-%m-%d')

Then use this output using ${{ steps.date.outputs.date }}. The following show an example for environment variables and for inputs :

on: [push, pull_request]
name: build
jobs:
  build:
    name: Example
    runs-on: ubuntu-latest
    steps:
      - name: Get current date
        id: date
        run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
      - name: Test with environment variables
        run: echo $TAG_NAME - $RELEASE_NAME
        env:
          TAG_NAME: nightly-tag-${{ steps.date.outputs.date }}
          RELEASE_NAME: nightly-release-${{ steps.date.outputs.date }}
      - name: Test with input
        uses: actions/hello-world-docker-action@master
        with:
          who-to-greet: Mona-the-Octocat-${{ steps.date.outputs.date }}

Outputs :

* Test with environment variables
nightly-tag-2020-03-31 - nightly-release-2020-03-31

* Test with input
Hello Mona-the-Octocat-2020-03-31

Upvotes: 140

Related Questions