2

Here is my .yml file for a given workflow:

name: Deploy to Fly.io
'on':
  push:
    branches:
      - development # remove this once everything finalized
      - production
    paths: ["server-v2/**"]
defaults:
  run:
    working-directory: server-v2
env:
  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
jobs:
  deploy:
    name: Deploy app
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: superfly/flyctl-actions/setup-flyctl@master
      - run: flyctl deploy --remote-only
  send_notification:
    needs: deploy
    name: Send SMS Notification
    runs-on: ubuntu-latest
    env:
       MESSAGE: "✅ GitHub Action ✅\n\nName: Deploy to Fly.io\nStatus: Complete\n\nAPI is live at:\nhttps://ammarahmedca.fly.dev"
    steps:
      - uses: twilio-labs/actions-sms@v1
        with:
          fromPhoneNumber: ${{ secrets.TWILIO_PHONE_NUMBER }}
          toPhoneNumber: ${{ secrets.MY_PHONE_NUMBER }}
          message: ${{ env.MESSAGE }}
        env:
          TWILIO_ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
          TWILIO_API_KEY: ${{ secrets.TWILIO_API_KEY }}
          TWILIO_API_SECRET: ${{ secrets.TWILIO_API_SECRET }}

It runs the deploy action and then afterward, it sends me an SMS notifying me its complete. I want to embed values into the MESSAGE variable such as the workflow name, and date/time the workflow was called or completed. However, adding env variables there throws an error. As in, doing this:

env:
  MESSAGE: "✅ GitHub Action ✅\n\nName: ${{ env.GITHUB_WORKFLOW }}\nStatus: Complete\n\nAPI is live at:\nhttps://ammarahmedca.fly.dev"

will throw this error:

Invalid workflow file: .github/workflows/twillio-test.yml#L10
The workflow is not valid. .github/workflows/twillio-test.yml (Line: 10, Col: 16): Unrecognized named-value: 'env'. Located at position 1 within expression: env.GITHUB_WORKFLOW

AFAIK, env.GITHUB_WORKFLOW should be a default environment variable with all workflows so I'm not sure why it throws the error.

In any case, I'm looking to add values to the message I'm sending with Twilio, more specifically, the name of the workflow and the date/time it was called. I was unable to find any docs on the Twilio GitHub action. I found out how to do this from a short blog post by Twilio.

Ammar Ahmed
  • 344
  • 3
  • 11

1 Answers1

2

You're right that it's a default environment variable with all workflows, but it isn't within env, it's within github: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context

You can get the GitHub workflow name by referencing it as ${{ github.workflow }}.

James Williams
  • 724
  • 6
  • 20
  • Do you know if there is any github default variable for the date/time the workflow is called? – Ammar Ahmed Nov 12 '22 at 20:40
  • 2
    This should help: https://stackoverflow.com/questions/60942067/get-current-date-and-time-in-github-workflows When you find a context in which you can run the Linux command `date` that suits you, you can add formatting arguments (e.g. `date +%Y` for just the year) to get what you want specifically. – James Williams Nov 13 '22 at 10:44