Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 284
Wowbagger and his liquid lunch Avatar asked Mar 31 '20 01:03

Wowbagger and his liquid lunch


People also ask

How do I see action minutes in GitHub?

In the upper-right corner of any page, click your profile photo, then click Settings. In the "Access" section of the sidebar, click Billing and plans. Under "GitHub Actions", view details of your minutes used.

What is billable time GitHub Actions?

Billable job execution minutes are only shown for jobs run on private repositories that use GitHub-hosted runners and are rounded up to the next minute. There are no billable minutes when using GitHub Actions in public repositories or for jobs run on self-hosted runners.


2 Answers

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 
like image 136
Bertrand Martel Avatar answered Sep 20 '22 18:09

Bertrand Martel


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 

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

like image 22
creimers Avatar answered Sep 18 '22 18:09

creimers