Github Actions: The Basics
Github actions are a convenient way to add continuous integration (CI) and continuous deployment (CD) to your existing Github repositories. This guide will show beginners how to iterate quickly and experiment with the basics until they gain the confidence to use some of the more advanced features.
Minimal Workflow
Below is very simple workflow that:
- Starts an Ubuntu runner for a job called “Continuous-Integration”
- Checks out the current commit
- Prints Hello World!
Follow along by creating a yaml file in your Github repository under the folder .github/workflows
### .github/workflows/ci-basics.yaml
name: CI Basics
# Decide when this workflow runs
on:
# Triggers the workflow on all commits
push
# Each workflow is composed of one or more jobs
jobs:
# This workflow has a single job called "Continuous-Integration"
Continuous-Integration:
# The runner that the job will use
runs-on: ubuntu-latest
# Each job runs a sequential list of steps or "actions"
steps:
# Standard action to checkout the code in this commit
- uses: actions/checkout@v2
# Run the classic getting started code :)
- name: Hello World
run: echo Hello World!
Commit this file into a new or existing branch in your codebase and head over to the Actions section.
Click on the workflow run which will be titled your commit summary to see the workflow details
Now that we checked in our first workflow and can see what it did let’s expand upon the example.
Adding A Build Step
Now that we have a baseline it’s time to implement a build step. After all, that’s what CI is all about.
To demonstrate the example let’s build a really simple Docker image. Thankfully, the Ubuntu runner comes with the docker toolset already set up.
To follow along copy the example files below into the root of your repository
<!--- index.html --->
<!DOCTYPE html>
<html>
<head>
<title>Github Workflow Example</title>
</head>
<body>
<p>I was deployed using Github Actions!</p>
</body>
</html>
And a Dockerfile
### Dockerfile
FROM nginx
COPY index.html /usr/share/nginx/html/
With our files prepared we can add the build steps to our existing Continuous-Integration job
### .github/workflows/ci-basics.yaml
name: CI Basics
# Decide when this workflow runs
on:
# Triggers the workflow on all commits
push
# Each workflow is composed of one or more jobs
jobs:
# This workflow has a single job called "Continuous-Integration"
Continuous-Integration:
# The runner that the job will use
runs-on: ubuntu-latest
# Each job runs a sequential list of steps or "actions"
steps:
# Standard action to checkout the code in this commit
- uses: actions/checkout@v2
# Run the classic getting started code :)
- name: Hello World
run: echo Hello World!
# Create the CI artifact
- name: Build Docker Image
run: docker build -t asafpeleg/github-workflow-example .
Deployment Step
For the final piece we will add a continuous deployment step. In our case we will publish our Docker image to the public registry.
In order to publish to the public Docker registry it is required to login first. Since the login credentials are sensitive it is a good idea to save them as Github Repository Secrets. Navigate to Settings -> Secrets -> Actions and click the “New repository secret” button and add your docker hub password.
With the secret in place we can add the final steps to the Continuous-Integration job.
### .github/workflows/ci-basics.yaml
name: CI Basics
# Decide when this workflow runs
on:
# Triggers the workflow on all commits
push
# Each workflow is composed of one or more jobs
jobs:
# This workflow has a single job called "Continuous-Integration"
Continuous-Integration:
# The runner that the job will use
runs-on: ubuntu-latest
# Each job runs a sequential list of steps or "actions"
steps:
# Standard action to checkout the code in this commit
- uses: actions/checkout@v2
# Run the classic getting started code :)
- name: Hello World
run: echo Hello World!
# Create the CI artifact
- name: Build Docker Image
run: docker build -t asafpeleg/github-workflow-example .
# Login action provided by docker
- name: “Login to Docker Hub”
uses: docker/login-action@v1
with:
username: asafpeleg
password: ${{ secrets.DOCKER_PASSWORD }}
— name: “Publish Docker Image”
run: docker push asafpeleg/github-workflow-example
These two final steps that were added are:
- Another pre-built action (similar to the checkout step) that simply takes a username and password to succeed. Take notice of the password input which is supplied from our Github Repository Secret that was just created. Variables in Github workflows have the standard form
${{ variable }}
. - Publish the image built in the “Build Docker Image” step up to the public registry.
This deployment step concludes the guide. With these newly learned skill you should be well on your way to adding CI/CD to all your Github projects.