GitHub allows each step in a workflow to send text to the Actions interface using a variable called GITHUB_STEP_SUMMARY
.
GITHUB_STEP_SUMMARY
is a variable that contains a path to a file where each step can store text.
If the file contains any text at the end of a step’s run, the text is written to the workflow summary and styled as Github Flavored Markdown. Along with Markdown, we can also use emojis to add extra flair to the step summary.
- name: Terraform Plan
continue-on-error: true
id: plan
run: |
terraform plan -input=false -no-color -out=tfplan
terraform show -no-color tfplan > plan.txt
- name: Display the plan summary
id: display
run: |
{
awk '/No changes. Your infrastructure matches the configuration./ {
print "## " $0
print "Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed."
exit
}' plan.txt
awk '/Terraform will perform the following actions:/ {
print "## Terraform will perform the following actions:"
print "|Action|Resource|"
print "|------|--------|"
exit
}' plan.txt
awk '/Terraform used the selected/{ next } /will be/ || /must be/ {print "|" $5 "|" $2 "|"; next} /Plan:/{ print "## " $0; next }' plan.txt \
| sed -e 's/created/:white_check_mark: create/' -e 's/destroyed/:bangbang: destroy/' -e 's/replaced/:recycle: replace/'
} > plan.md
cat plan.md >> $GITHUB_STEP_SUMMARY
Using the Terraform configuration provided, this produces the following output on the initial plan:
-
For details on creating or updating a service account, see the instructions in lesson 04_04.
Add the following permission to the service account you will use for this exercise:
AmazonS3FullAccess
-
Create an S3 bucket to use for storing Terraform state files.
- Go to the S3 homepage.
- Select
Create bucket
. - Give your bucket a name. The bucket name must be globally unique and must not contain spaces or uppercase letters. See rules for bucket naming.
- Confirm the region for the bucket. It should be the same region you will use to configure your service account in the repository.
- Keep all defaults and select
Create bucket
at the bottom of the form. - Make a note of your bucket name. Make a note of the region where you created the bucket.
-
Create a new repo and add the exercise files for this lesson.
-
Move the workflow files into
.github/workflows
: -
Update the terraform configuration to match your AWS account settings.
-
Edit terraform.tf. Update the
bucket
andregion
assignments so that they match the bucket and region you used in the previous steps.terraform { required_version = "~> 1.4" backend "s3" { key = "github-actions-cicd/terraform.tfstate" bucket = "ADD_YOUR_BUCKET_NAME_HERE" # the bucket region = "ADD_YOUR_REGION_NAME_HERE" # the region } }
-
-
Configure the service account credentials.
- Select
Settings
->Secrets and variables
->Actions
. - Select
New respository secret
. - Create entries for the following using the values retrieved when you created the service account.
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_ACCOUNT_NUMBER
- Select the
Variables
tab. - Select
New repository variable
. - Create an entry for
AWS_REGION
using the same region as the bucket created in previous steps.
- Select
-
Create a branch protection rule.
- Select
Settings
->Branches
->Add branch protection rule
. - Under "Branch name pattern" enter:
main
. - Under "Protect matching branches":
- Select
Require a pull request before merging
. - Un-select
Require approvals
. (This is because you can't approve your own merge requests. Keeping this option selected would require you to override the merge protection on each pull request.) - Select
Require status checks to pass before merging
. - At the bottom of the page, select
Create
.
- Select
- Select
-
Create an environment protection rule.
- Select
Settings
->Environments
->New environment
. - For the name, enter
Production
. SelectConfigure environment
. - Select
Required reviewers
. - In the search field, enter your GitHub user name and select it.
- Select
Save protection rules
.
- Select
-
Edit the file
variables.tf
. -
Find the
server_count
code block at the top of the file.variable "server_count" { type = number default = 3 description = "The total number of VMs to create" }
Change
default = 3
->default = 4
. -
Select
Commit changes
. -
Select
Create a new branch for this commit and start a pull request
. Then select,Propose changes
. -
Select
Create pull request
. -
Observe the checks and summaries from GitHub Actions being written to the pull request. Wait for the workflow to complete.
-
Select
Merge pull request
->Confirm merge
. -
Go to the
Actions
tab. Select the most recent running workflow. -
Observe the pipeline's progress and note the updates to the workflow summary.
-
When prompted, select
Review deployments
. -
Select
Production
->Approve and deploy
. -
Observe the pipeline's progress and note the updates to the workflow summary.
-
Follow the links in the workflow summary to view the e-commerce site deployed by the workflow.
To avoid costs associated with running resources in AWS, please remove them by running the 99-Destroy Resources
workflow.
- Select the
Actions
tab. - Select the workflow
99-Destroy Resources
. - Next to "This workflow has a workflow_dispatch event trigger.", Select
Run workflow
->Run workflow
. - Select the running workflow. Observe the pipeline's progress and note the updates to the workflow summary.
- When prompted, select
Review deployments
. - Select
Production
->Approve and deploy
. - Observe the pipeline's progress and note the updates to the workflow summary.