75

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?

chenrui
  • 8,910
  • 3
  • 33
  • 43

6 Answers6

123

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
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • 2
    Not an expert, but I don't understand how this can work, as it seems you mix 2 different concepts. `echo "{name}={value}" >> $GITHUB_ENV` is for setting an environment variable, but you define a `name` env variable your script never use. And `${{ steps..outputs. }}` is for reading the output of a step, but you try to read a `date` output the `date` step never wrote. I would use instead: `run: echo "::set-output name=today::$(date +'%Y-%m-%d')"` and then `${{ steps.date.outputs.today }}`. – Jean-Alexis Aufauvre Jan 02 '21 at 12:42
  • 7
    For me a new step with `run: echo "MY_DATE=$(date +%Y%m%d%H%M)" >> $GITHUB_ENV` and in a subsequent step `${{ env.MY_DATE }}` worked. As described in this answer https://stackoverflow.com/a/64649397/2584278 . – Faber Jan 02 '21 at 16:08
  • 2
    The answer doesn't work for me but @Jean-AlexisAufauvre works. – JavierSegoviaCordoba Jan 30 '21 at 19:44
  • @chenrui your edit is not working, the original output should works. I don't understand why you remove set-output. – JavierSegoviaCordoba Jan 30 '21 at 19:47
  • Seems ::set-output is obsolete now. still looking for how to set build date to an env variable – Pete Dec 29 '21 at 15:00
  • 2
    @Pete Can you provide a source that `set-output` is obsolete? I cannot find any such information. – Cas Jan 20 '22 at 15:14
  • this solution is now deprecated on Github Actions – luismesas May 14 '23 at 18:15
45

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.

Tyler Chong
  • 650
  • 2
  • 12
  • 24
creimers
  • 4,975
  • 4
  • 33
  • 55
  • 6
    Unfortunately, `set-env` is deprecated now: https://github.blog/changelog/2020-11-09-github-actions-removing-set-env-and-add-path-commands-on-november-16/ – Max Nov 23 '20 at 15:12
  • 1
    Thanks, I just use $(date +'%Y-%m-%dT%H:%M:%S') to set a temporary build for prerelease packages. e.g. -p:PackageVersion=$(date +'%Y%m%d.%H%M%S')-preview. – Atif Rehman Nov 23 '21 at 19:13
  • 3
    I think the syntax is different now `echo "NOW=$(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_ENV` – maxisam Sep 10 '22 at 21:21
  • use ${{ env.var_name }} to access the environment variables, https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-writing-an-environment-variable-to-github_env – Shubham Jul 04 '23 at 03:37
21

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

13013SwagR
  • 433
  • 4
  • 13
  • Another major downside is the fact that without further formatting this is invalid for a tag name due to the colons. – Salvage Jun 08 '22 at 16:37
  • 1
    I tried this but I get the time of the **previous** update (not the current one, the one the Action runs on) – WoJ Jan 21 '23 at 10:56
  • 1
    I loved this answer. Please add in Cons: If you retry the jobs, the serial stays the same. – Fabrício Ceolin Feb 22 '23 at 17:13
4

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 }}
Lloyd Hamilton
  • 101
  • 1
  • 3
1

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
Andrii Bodnar
  • 1,672
  • 2
  • 17
  • 24
0
# 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

Shivam Bharadwaj
  • 1,864
  • 21
  • 23