hectorsector / actions-workflow

https://github-learning-lab-staging.herokuapp.com/githubtraining/enable-a-workflow-with-github-actions
0 stars 0 forks source link

Improve the deployment workflow #6

Open hectorsector opened 5 years ago

ghost commented 5 years ago

By filling in the args field, you're passing arguments to the action, which is running in a Docker container.

In this case, we passed in arguments with the -e option. This allows us to set environment variables in Zeit Now.

When an action runs, some environment variables are set by GitHub and available in the runtime environment. Although these are available in the Docker container, they need to be explicitly passed to Zeit Now if we want our deployed application to make use of them.

Get your deployment URL by going into the Actions tab. You can then visit it to examine the newly deployed app. You'll see that it now looks something like this:

screenshot of deployed web app with confetti

ghost commented 5 years ago

We don't always have to dig into the Actions tab to get the deployment URL, though. One of the best things about defining workflows with GitHub Actions is the ability to use multiple services in a single place to make your life easier.

Let's practice this by sending yourself an SMS notification when a deployment occurs. You'll need to create an account on Nexmo for this next step.

:keyboard: Activity part 1: Get tokens for Nexmo

On Nexmo:

  1. Create an account on Nexmo, if you don't already have one. You can signup with GitHub if you'd like.
  2. Navigate to your SMS Getting started guide and jot down each of the following items:
    • API key will be used on GitHub as NEXMO_API_KEY
    • API secret will be used on GitHub as NEXMO_API_SECRET
  3. Navigate to Numbers section, jot down:
    • The Number assigned to you by Nexmo will be used on GitHub as NEXMO_NUMBER

:keyboard: Activity: Use an action to send an SMS notification on deployment

  1. Edit .github/main.workflow on this branch.
  2. On the side bar, select your deployment workflow.
  3. Drag the connector from the deployment action to the new action block.
  4. In the Choose action field, type nexmo-community/nexmo-sms-action@master
  5. Click Use
  6. In the label field, type sms notify
  7. In the :lock: secrets section, create new secrets for each of the following fields:
    • NEXMO_API_KEY: the API key provided to you by Nexmo
    • NEXMO_API_SECRET: the API secret provided to you by Nexmo
    • NEXMO_NUMBER: the phone number provided to you by Nexmo
    • PHONE: your phone number, the number you wish to notify, preceded by the country code
  8. In the args field we'll enter the body of the message, I suggest:
    $PHONE A deployment just occurred check it out
  9. Click Done.
  10. Click Start commit on the top right.
  11. Enter a commit message.
  12. Select Commit directly to your branch.
  13. Click Commit changes

I'll respond when I detect a new commit on this branch.

ghost commented 5 years ago

You should get a notification soon! But it won't include the URL of your deployment. That's because you haven't supplied the Nexmo SMS action with the URL. To do this, we need to save that URL in the deploy action, and access it again in the SMS action. This is possible as a result of the runtime environment that is created when actions run in the Docker container.

:keyboard: Activity: Capture the output from an existing action

  1. Edit the main.workflow file on this branch.
  2. On the side bar, select your deployment workflow.
  3. Click edit in your deployment action.
  4. In the args field, add deploy > deploy.txt.
    • Remember that we've previously used this field to pass in the runtime environment variables. You'll want to leave those in place, so the complete arguments will be:
      -e GITHUB_SHA=$GITHUB_SHA -e GITHUB_ACTOR=$GITHUB_ACTOR deploy > deploy.txt
  5. Click Done.
  6. Click Start commit on the top right.
  7. Enter a commit message.
  8. Select Commit directly to your branch.
  9. Click Commit changes

I'll respond when I detect a new commit on this branch.

ghost commented 5 years ago

We've now captured the output from the deployment action, although we can't see it. It is stored in a file called deploy.txt in the Docker container. We can use the captured deployment URL currently sitting in our runtime environment to include it in the SMS notification.

:keyboard: Activity: Send a URL with the SMS notification

  1. Edit the main.workflow file on this branch.
  2. On the side bar, select your deployment workflow.
  3. Click edit in your sms notify action.
  4. In the args field, add a cat deploy.txt to the args field.
    • Remember that this field previously contained your meesage, so the complete arguments will be:
      $PHONE A deployment just occurred at `cat deploy.txt` check it out
  5. Click Done.
  6. Click Start commit on the top right.
  7. Enter a commit message.
  8. Select Commit directly to your branch.
  9. Click Commit changes

I'll respond when I detect a new commit on this branch.