Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit 4e20044

Browse files
authored
Merge pull request #5 from githubtraining/full-explains
More complete explanations
2 parents e7184ba + bfd919a commit 4e20044

15 files changed

+264
-140
lines changed

config.yml

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,67 @@ steps:
4444
# event: User commits the proper trigger
4545
- title: Configure a trigger based on labels
4646
description: Configure the workflow trigger based on an a label being added
47-
event: pull_request
47+
event: pull_request.synchronize
4848
link: '{{ repoUrl }}/pull/1'
4949
actions:
50+
- type: getFileContents
51+
action_id: fileContents
52+
filename: '.github/workflows/deploy-staging.yml'
53+
required: false
54+
5055
# We validate that trigger is correct
51-
#- type: gate
56+
- type: gate
57+
every: true
58+
gates:
59+
- left: '%payload.pull_request.title%'
60+
operator: ===
61+
right: Create a staging workflow
62+
- left: '/on:\s*\n\s*pull_request:\s*\n\s*types:\s*\[\s*labeled\s*\]\s*/gi'
63+
operator: test
64+
right: '%actions.fileContents%'
65+
else:
66+
- type: respond
67+
with: 0Xe_unexpected.md
68+
data:
69+
expected: commit the file `.github/workflows/deploy-staging.yml` and a `[labeled]` trigger on the `staging-workflow` branch
5270

53-
# Step 2: Choose the environment for AWS
71+
# Step 2: Trigger a job on specific labels
5472

5573
# We tell the user to write the proper environment in the workflow file
5674
- type: respond
57-
with: 02_environment.md
75+
with: 02_conditional.md
5876

5977
# event: User commits proper environment in workflow
60-
- title: Choose the environment for AWS
61-
description: Choose the Ubuntu environment for our app
62-
event: pull_request
78+
- title: Trigger a job on specific labels
79+
description: Use a conditional to trigger a job on a specific label
80+
event: pull_request.synchronize
6381
link: '{{ repoUrl }}/pull/1'
6482
actions:
83+
- type: getFileContents
84+
action_id: fileContents
85+
filename: '.github/workflows/deploy-staging.yml'
86+
required: false
6587

6688
# We validate
67-
#- type: gate
89+
- type: gate
90+
left: /\s*if:\s*contains\s*\(\s*github\.event\.pull_request\.labels\.\*\.name,\s*'stage'\)/gi
91+
operator: test
92+
right: '%actions.fileContents%'
93+
else:
94+
- type: respond
95+
with: 0Xe_unexpected.md
96+
data:
97+
expected: "the conditional `if: contains(github.event.pull_request.labels.*.name, 'stage')` in the `.github/workflows/deploy-staging.yml` file"
98+
6899

69100
# Step 3: Write the steps for the staging workflow
70101
# We tell the user to write the proper steps for the workflow
71-
- type: respond
72-
with: 03_workflow-steps.md
102+
- type: respond
103+
with: 03_workflow-steps.md
73104

74105
# event: commit, user writes the steps for the workflow, using recommended existing GitHub steps
75-
- title: Write the steps for the staging workflow
76-
description: Write the steps for the staging deployment workflow
106+
- title: Deploy a Node.js app to AWS for the first time
107+
description: Use the GitHub-created action to deploy to AWS
77108
event: pull_request
78109
link: '{{ repoUrl }}/pull/1'
79110
actions:
@@ -127,28 +158,12 @@ steps:
127158

128159
# event: responding with a comment
129160

130-
- title: Confirm AWS S3 configuration
131-
description: Create an AWS account by the following specifications, and confirm here
132-
event: issue_comment.created
161+
- title: Create an S3 bucket
162+
description: Create an S3 bucket in AWS and tell the action where to find it
163+
event: pull_request.synchronize
133164
link: '{{ repoUrl }}/pull/2'
134165
actions:
135166

136-
# Step 6: Enter environment variables
137-
138-
# We ask user to enter their environment variables into this repository.
139-
140-
- type: respond
141-
with: 06_env-variables.md
142-
143-
# event: Respond with a comment
144-
- title: Create and store environment variables
145-
description: Create your AWS IAM secrets and enter them in this repository
146-
event: issue_comment.created
147-
link: '{{ repoUrl }}/pull/2'
148-
actions:
149-
150-
# Validate if possible
151-
152167
# Step 7: Approve pull request adding aws-config.yml and sam-template.yml
153168

154169
# We explain what the purpose of these templates are, and ask the user to approve
@@ -239,29 +254,29 @@ steps:
239254

240255
# We tell the user to write the proper environment
241256
- type: respond
242-
with: 10_environment.md
257+
with: 10_deploy-prod.md
243258

244259
# event: commit, user writes the environment
245260

246-
- title: Choose the environment for AWS
247-
description: Commit the proper environment for AWS to the workflow file
261+
- title: Complete the deployment to production workflow
262+
description: Commit the steps to the production workflow that allow you to deploy on merge to master.
248263
event: pull_request
249264
link: '{{ repoUrl }}/pull/3'
250265
actions:
251266

252267
# We validate
253268
# - type: gate
254269

255-
# Step 11: Write the steps for the production workflow
270+
# Step 11: Create the Docker image and push it to GPR
256271

257272
# We tell the user to write the proper steps for the workflow
258273
- type: respond
259274
with: 11_workflow-steps.md
260275

261276
# event: commit, user writes the steps for the workflow, using recommended existing GitHub steps
262277

263-
- title: Write the steps for the production workflow
264-
description: Write the steps for the production deployment to the workflow file
278+
- title: Create the Docker image and push it to GPR
279+
description: Add a step to the workflow for building and pushing a Docker image to GPR.
265280
event: pull_request
266281
link: '{{ repoUrl }}/pull/3'
267282
actions:
@@ -291,12 +306,7 @@ steps:
291306

292307
# check that its merged
293308
- type: gate
294-
every: true
295-
gates:
296-
- left: '%payload.action%'
297-
operator: ===
298-
right: closed
299-
- left: '%payload.pull_request.merged%'
309+
left: '%payload.pull_request.merged%'
300310

301311
# final response: congratulations
302312
- type: respond

responses/01_label-trigger.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
# Welcome to the course!
22

3-
We will be working with Continuous Delivery. We will...
4-
- Create a workflow to deploy to staging based on a label
5-
- Create a workflow to deploy to production based on merging to master
6-
- Use AWS configuration
3+
We'll learn how to create a workflow that enables Continuous Delivery. You'll:
4+
- create a workflow to deploy to staging based on a label
5+
- create a workflow to deploy to production based on merging to master
6+
7+
Before you start, you should be familiar with GitHub and Continuous Integration. If you aren't sure where to start, you may want to check out these two Learning Lab courses:
78

8-
Before you start, you should...
99
- [Introduction to GitHub](https://lab.github.com/githubtraining/introduction-to-github)
1010
- [Continuous Integration with GitHub Actions](https://lab.github.com/githubtraining/set-up-continuous-integration-with-github-actions)
1111

1212
### What is Continuous Delivery?
1313

14-
According to [continuousdelivery.com](https://continuousdelivery.com/),
14+
[Martin Fowler](https://martinfowler.com/bliki/ContinuousDelivery.html) defined Continuous Delivery very simply in a 2013 post as follows:
1515

16-
> Continuous Delivery is the ability to get changes of all types—including new features, configuration changes, bug fixes and experiments—into production, or into the hands of users, safely and quickly in a sustainable way.
16+
> Continuous Delivery is a software development discipline where you build software in such a way that the software can be released to production at any time.
1717
1818
A lot of things go into delivering "continuously". These things can range from culture and behavior to specific automation. In this course, we're going to focus on deployment automation.
1919

20+
### Kicking off deployments
21+
22+
Every deployment is kicked off by some trigger. Engineers at many companies, like at GitHub, typically use a ChatOps command as a trigger. The trigger itself isn't incredibly important. In our use case, we'll use labels. When someone applies a "stage" label to a pull request, that'll be our indicator that we'd like to deploy our application to a staging environment.
23+
2024
## Step 1: Configure a trigger based on labels
2125

22-
During the `on` step, we define what should cause this workflow to run. In this case, we want the workflow to run whenever a label is applied to the pull request.
26+
In a GitHub Actions workflow, the `on` step defines what causes the workflow to run. In this case, we want the workflow to run whenever a label is applied to the pull request.
2327

2428
### :keyboard: Activity: Configure the workflow trigger based on an a label being added
2529

@@ -38,6 +42,5 @@ on:
3842

3943
jobs:
4044
build:
41-
if: contains(github.event.pull_request.labels.*.name, 'stage')
42-
43-
```
45+
runs-on: ubuntu-latest
46+
```

responses/02_conditional.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Job conditionals
2+
3+
GitHub Actions features powerful controls for when to execute jobs and the steps within them. One of these controls is `if`, which allows you run a job only when a specific condition is met. See [`jobs.<job_id>.if` in _Workflow syntax for GitHub Actions_](https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idif) for more information.
4+
5+
### Using information within GitHub
6+
7+
Workflows are part of the GitHub ecosystem, so each workflow run gives you access to a rich set of data that you can use to take fine-grained control.
8+
9+
We'd like to run our workflow on a specific label called **stage**, so we'll achieve this in a single line that packs a punch. We'll use:
10+
- [`if:`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idif) is the conditional that will decide if the job will run
11+
- [`contains()`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#contains) is a function that allows us to determine if a value like say, a label named `"stage"`, is contained within a set of values like say, a label array `["bug", "stage", "feature request"]`
12+
- `github.event.pull_request.labels` is specifically accessing the set of labels that triggered the workflow to run. It does this by accessing the [`github` object](https://help.github.com/en/github/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context), and the [`pull_request` event](https://help.github.com/en/github/automating-your-workflow-with-github-actions/events-that-trigger-workflows#pull-request-event-pull_request) that triggered the workflow.
13+
- `github.event.pull_request.labels.*.name` uses [object filters](https://help.github.com/en/github/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context) to filter out all information about the labels, like their color or description, and lets us focus on just the label names.
14+
15+
## Step 2: Trigger a job on specific labels
16+
17+
Let's put all this together to run our job only when a labeled named "stage" is applied to the pull request.
18+
19+
### :keyboard: Activity: Choose the Ubuntu environment for our app
20+
21+
```yml
22+
name: Staging deployment
23+
24+
on:
25+
pull_request:
26+
types: [labeled]
27+
28+
jobs:
29+
build:
30+
runs-on: ubuntu-latest
31+
32+
if: contains(github.event.pull_request.labels.*.name, 'stage')
33+
```

responses/02_environment.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

responses/03_workflow-steps.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
# Workflow steps
22

3-
So far, the workflow knows what the trigger is and what environment to run in. But, what exactly is supposed to run? The "steps" section of this workflow specify what actions to be run in the Ubuntu environment when new labels are added.
3+
So far, the workflow knows what the trigger is and what environment to run in. But, what exactly is supposed to run? The "steps" section of this workflow specifies actions and scripts to be run in the Ubuntu environment when new labels are added.
44

55
## Step 3: Write the steps for the staging workflow
66

7-
We won't be going into detail on the steps of this workflow, but it would be a good idea to check them out. You'll see that we're adding steps using existing actions for:
7+
We won't be going into detail on the steps of this workflow, but it would be a good idea to become familiar with the actions we're using. They are:
88

9-
- `actions/checkout`
10-
- `Deploy to AWS`
9+
- [`actions/checkout`](https://github.com/actions/checkout)
10+
- [`actions/upload-artifact`](https://github.com/actions/upload-artifact)
11+
- [`actions/download-artifact`](https://github.com/actions/download-artifact)
12+
- [`github/deploy-nodejs`](https://github.com/github/deploy-nodejs)
1113

12-
### :keyboard: Activity: Write the steps for the staging deployment workflow
14+
The course [_Using GitHub Actions for CI_](https://lab.github.com/githubtraining/using-github-actions-for-ci) also teaches how to use most of these actions in details.
15+
16+
### :keyboard: Activity: Deploy a Node.js app to AWS for the first time
17+
18+
1. In a new tab, [create an AWS account](https://portal.aws.amazon.com/billing/signup) if you don't already have one.
19+
> Note: You may need a credit card to create an AWS account. If you're a student, you may also be able to take advantage of the [Student Developer Pack](https://education.github.com/pack) for access to AWS. If you'd like to continue with the course without an AWS account, Learning Lab will still respond, but none of the deployments will work.
20+
1. **[Add a user](https://console.aws.amazon.com/iam/home?#/users$new?step=details)** in the IAM service with administrator permission.
21+
- For detailed instructions on adding users, see [_Creating an IAM User in Your AWS Account_](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html) in the AWS docs.
22+
1. In the confirmation screen, copy both the **Access key ID** and the **Secret access key** to a safe space. We'll use these in the next few steps as follows:
23+
- Access key ID ➡️ `AWS_ACCESS_KEY`
24+
- Secret access key ️️️ ➡️ `AWS_SECRET_KEY`
25+
1. Back on GitHub, click on this repository's **[Secrets]({{ repoUrl }}/settings/secrets)** in the Settings tab.
26+
1. Click **Add a new secret**
27+
1. Name your new secret **AWS_ACCESS_KEY** and paste the value from the Access key ID generated on AWS.
28+
1. Click **Add secret**.
29+
1. Click **Add a new secret** again.
30+
1. Name the second secret **AWS_SECRET_KEY** and paste the value from the Secret access key generated on AWS.
31+
1. Click **Add secret**
32+
1. Back in this pull request, edit the `.github/workflows/staging-workflow.yml` file to use a new action
33+
```yml
34+
- name: Deploy to AWS
35+
uses: github/deploy-nodejs@master
36+
env:
37+
AWS_ACCESS_KEY: {% raw %}${{ secrets.AWS_ACCESS_KEY }}{% endraw %}
38+
AWS_SECRET_KEY: {% raw %}${{ secrets.AWS_SECRET_KEY }}{% endraw %}
39+
```
40+
If you'd like to copy the full workflow file, it should look like this:
1341
1442
```yml
1543
name: Staging deployment
@@ -50,7 +78,7 @@ jobs:
5078
path: public
5179

5280
- name: Deploy to AWS
53-
uses: docker://admiralawkbar/aws-nodejs:latest
81+
uses: github/deploy-nodejs@master
5482
env:
5583
AWS_ACCESS_KEY: {% raw %}${{ secrets.AWS_ACCESS_KEY }}{% endraw %}
5684
AWS_SECRET_KEY: {% raw %}${{ secrets.AWS_SECRET_KEY }}{% endraw %}

responses/04_merge-staging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ It won't be "working" yet, because our next step is to work on the configuration
99
### :keyboard: Activity: Merge this staging workflow pull request
1010

1111
1. Merge this pull request
12-
2. Delete the `staging-workflow` branch`
12+
2. Delete the `staging-workflow` branch

responses/05_confirm-aws.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
# AWS Configuration - S3 Buckets
22

3-
For deployment, we will be using AWS.
3+
GitHub Actions is cloud agnostic, so any cloud will work. We'll show how to deploy to AWS in this course.
44

55
### S3 Buckets
66

7-
Amazon S3 Buckets are containers. They're also a very flexible type of data storage- they can be configured to work in many different types of ways. They're popular for their security, scalability, and dependability. Our S3 Bucket will be the container that our application is deployed in, both in staging and in production.
7+
Amazon S3 Buckets are a very flexible type of data storage -- they can be configured to work in many different types of ways. They're popular for their security, scalability, and dependability. Our S3 Bucket will store our application, both in staging and in production.
88

99
## Step 5: Confirm AWS S3 configuration
1010

11-
### :keyboard: Activity: Create an AWS account by the following specifications, and confirm here
11+
### :keyboard: Activity: Create an S3 bucket
1212

13-
1. Create an account at [aws.amazon.com](https://aws.amazon.com/)
14-
- _This requires credit card information. If you'd like to continue with the course without an AWS account, Learning Lab will still respond, but none of the deployments will work._
15-
2. [Create an S3 bucket](https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html)
16-
- If you aren't sure how to get there, you can search for `S3`.
17-
- Name the bucket `github-sam-test`
18-
- The region needs to be the same as what is specified in the `aws-config.yml` file in this pull request. :eyes: **For this exercise, choose us-west-2**. :eyes: If you'd like to choose another region, make sure to update the `aws-config.yml` file to match.
19-
- For all other options, accept the defaults.
20-
3. Confirm that you've created an S3 bucket by commenting anything in this pull request
13+
1. Navigate to the [Amazon S3](https://s3.console.aws.amazon.com/s3/home) service and click on **Create bucket**.
14+
- See [_Create a Bucket_](https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html) on AWS documentation for the most detailed instructions.
15+
1. Name the bucket whatever you'd like, and jot the name down
16+
1. Select a region, and jot it down. You'll need it later. These examples use **US West (Oregon)**, also known as **us-west-2**. If you'd like to choose another region, make sure to update the `aws-config.yml` file to match.
17+
1. For all other options, accept the defaults.
18+
1. In `.github/aws-config.yml` on this branch, change the value of `s3_bucket:` to match your chosen bucket name.
19+
1. In the same file, confirm that the `region:` value matches your chosen region for the S3 bucket.
20+
1. Commit your changes.
21+
22+
I'll respond when I detect a commit on this branch.

responses/07_approve.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ Whenever you're using a GitHub Action, it's important to read the documentation.
1212

1313
This file is a bit trickier. The template `aws-config.yml` file that was documented with the action has a placeholder for this template, but doesn't specify what we should do.
1414

15-
In our case, we created the `sam-template.yml` for you. It contains information that's specific about the application source code in this repository. When we tell AWS to deploy, it wonders "Deploy _what_?". This file communicates which files should be deployed, and how, within our S3 bucket on AWS.
15+
This file is specific to deploying a serverless application to AWS. Specifically, an AWS SAM template tells AWS how to set up the application's architecture. Read more about it in [_AWS SAM Template Concepts_](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html) in the AWS documentation.
16+
17+
In our case, we created the `sam-template.yml` for you. It contains information that's specific about the application's endpoints and structure.
1618

1719
## Step 7: Approve the pull request
1820

0 commit comments

Comments
 (0)