Considering the JavaScript counter is going to take a bit of tinkering I thought I'd put GitHub Actions into the mix, so pushing any changes to my local files automatically update the S3 bucket on AWS.
-
A working knowledge of GitHub
-
A high level understanding of CI/CD, so you understand what this is trying to achieve
As required by the Challenge:
You do not want to be updating either your back-end API or your front-end website by making calls from your laptop. You want them to update automatically whenever you make a change to the code. (This is called continuous integration and deployment, or CI/CD.)
-
GitHub Actions create workflows (in YAML, stored in
.github/workflows
) that can handle common build tasks, like CI/CD. You can use an action to compress images, test your code, and push to the site to AWS when the main branch changes. -
Actions are fully integrated with vanilla GitHub, so should be easy to use on an existing repo...
-
Before attempting to place a GitHub Action on my project repo, I'm going to practice with a tutorial or two.
-
You can create your own GH Actions or incorporate an existing (community created) one in the Marketplace.
-
There's a hint in the Challenge rules that I'll need to invalidate the CloudFront cache (so the JavaScript visitor counter can show updates in real time), so I need to include that in my Action.
-
There's a warning not to commit AWS credentials to source control. I believe Actions can handle this in Secrets, so I've made a mental note to research this too.
-
Turns out I've not been pushing my branches to main, just merging them locally once I'm happy. I'm about to collaborate on group project with GitHub Organisation, so I've just learnt that in time...
I found a Hello World tutorial via GitHub Learning Lab.
-
git clone
hello-github-actions
onto local machine -
Open the repo in your code editor
-
Create a new branch called
first-action
-
Create a folder called
action-a
and a file calledDockerfile
-
Copy and paste the Dockerfile code from the tutorial into your file
FROM debian:9.5-slim
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
-
Commit and push this to GitHub
-
Open and resolve a pull request
-
In this same branch create a file in the
action/a
directory calledentrypoint.sh
-
Add this code to the new file...
#!/bin/sh -l
sh -c "echo Hello world my name is $INPUT_MY_NAME"
...where $INPUT_MY_NAME
is an environment variable called MY_NAME
- Stage, commit and push changes to GitHub
-
On the same branch, create a file called
action-a/action.yml
-
Paste this:
name: 'Hello Actions'
description: 'Greet someone'
author: 'octocat@github.com'
inputs:
MY_NAME:
description: 'Who to greet'
required: true
default: 'World'
runs:
using: 'docker'
image: 'Dockerfile'
branding:
icon: 'mic'
color: 'purple'
- Stage, commit and push changes to GitHub
-
Create a file called
.github/workflows/main.yml
-
To
main.yml
add:
# This name appears on any pull request or in the Actions tab
name: A workflow for my Hello World file
# This indicates that your workflow will execute any time
# code is pushed to your repo, using the push event
on: push
- Stage, commit and push changes to GitHub
N.B. Workflows piece together jobs, and jobs piece together steps. We'll now create a job that runs an action. Actions can be used from within the same repo, from any other public repo, or from a published Docker container image.
- Add this to
main.yml
:
jobs:
build:
name: Hello world action
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./action-a
with:
MY_NAME: 'Sam'
jobs
: is the base component of a workflow run
build
: is the identifier we're attaching to this job
name
: is the name of the job, this is displayed on GitHub when the workflow is running
runs-on
: defines the type of machine to run the job on. The machine can be either a GitHub-hosted runner or a self-hosted runner.
steps
: the linear sequence of operations that make up a job
uses: actions/checkout@v1
uses a community action called checkout to allow the workflow to access the contents of the repository
uses: ./action-a
provides the relative path to the action we created in the action-a directory of the repository
with
: is used to specify the input variables that will be available to your action in the runtime environment. In this case, the input variable is MY_NAME, and it is currently initialized to "Sam".
- Stage, commit and push changes to GitHub
In the same window, click the workflow you want to view, and you'll see the log (with your name in it)!!!!
-
As a final step, merge this pull request so the action will be part of the
main
branch. Push tomain
-
Delete your branch
The Lab is really nicely integrated with my repo and the next set of instructions being triggered by successful commits is ingenious. It was really helpful to go this deep today but for such a 'simple' action, i.e. outputting a string, it wasn't all that intuitive. Utilising branches in this manner, is a really nice way to build things up slowly before making everything live. Simple code, really powerful.
This introductary lab is about authoring your own Actions. Next time, I'll look at how to choose, edit, and integrate Actions from the Marketplace