A First Look at GitHub Actions
Tags: github, actions, cicd, workflows
Published: 2021-07-20 21:53:28

---
canonical_url: "https://ajcwebdev.com/first-look-github-actions/"
description: "GitHub Actions can be used to automate, customize, and execute software development workflows from within a GitHub repository."
---

> ___All of this project's code can be found in the [First Look monorepo](https://github.com/ajcwebdev/a-first-look/tree/main/deployment/actions/) on my GitHub.___

## Introduction

[GitHub Actions](https://docs.github.com/en/actions) can be used to automate, customize, and execute software development workflows from within a GitHub repository. You can discover, create, and share actions to perform CI/CD jobs and combine actions in a customized workflow.

## Create an Action

We will create a boilerplate workflow to help you get started with Actions.

```bash
mkdir -p ajcwebdev-actions/.github/workflows
cd ajcwebdev-actions
echo > .github/workflows/hello.yml
echo '.DS_Store' > .gitignore
```

`on` controls when the workflow will run. `push` and `pull_request` events trigger the workflow but only for the main branch. `workflow_dispatch` allows you to run this workflow manually from the Actions tab.

```yaml
name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:
```

A workflow run is made up of one or more `jobs` that can run sequentially or in parallel. Our workflow contains a single job called `build` that is running on `ubuntu-latest`.

```yaml
jobs:
  build:
    runs-on: ubuntu-latest
```

`steps` represent a sequence of tasks that will be executed as part of the job. `uses` checks-out your repository under `$GITHUB_WORKSPACE`, so your job can access it. `run` will run a single command (`echo "Hello from GitHub Actions"`) which will print `Hello from GitHub Actions` using the runner's shell.

```yaml
    steps:
      - uses: actions/checkout@v2
      - name: Run a one-line script
        run: echo "Hello from GitHub Actions"
```

The action will then run a multi-line script to print a series of messages containing common environment variables such as the repository name and job status.

```yaml
      - name: Run a multi-line script
        run: |
          echo "Job was triggered by a ${{ github.event_name }} event."
          echo "Job is now running on a ${{ runner.os }} server hosted on GitHub."
          echo "The branch name is ${{ github.ref }}."
          echo "The repository name is ${{ github.repository }}."
          echo "Job status is ${{ job.status }}."
```

Here is the complete GitHub Action.

```yaml
# .github/workflows/hello.yml

name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Run a one-line script
        run: echo "Hello from GitHub Actions"
      - name: Run a multi-line script
        run: |
          echo "Job was triggered by a ${{ github.event_name }} event."
          echo "Job is now running on a ${{ runner.os }} server hosted on GitHub."
          echo "The branch name is ${{ github.ref }}."
          echo "The repository name is ${{ github.repository }}."
          echo "Job status is ${{ job.status }}."
```

## Push your project to a GitHub repository

Initialize the repository, add all changes to the staging area, and commit all staged changes.

```bash
git init
git add .
git commit -m "Action Jackson"
```

### Create a new blank repository

You can create a blank repository by visiting [repo.new](https://repo.new) or using the [`gh repo create`](https://cli.github.com/manual/gh_repo_create) command with the [GitHub CLI](https://cli.github.com/). Enter the following command to create a new repository, set the remote name from the current directory, and push the project to the newly created repository.

```bash
gh repo create ajcwebdev-actions \
  --public \
  --source=. \
  --description="An Example GitHub Action Project" \
  --remote=upstream \
  --push
```

If you created a repository from the GitHub website instead of the CLI then you will need to set the remote and push the project with the following commands.

```bash
git remote add origin https://github.com/ajcwebdev/ajcwebdev-actions.git
git push -u origin main
```

Go to the actions tab on your GitHub repository to see your action.

![01-actions-tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj69m00h1i0xr6cgp8o7.png)

Click your action to see the specific workflow.

![02-action-summary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly6c0e9yqyrokel278l7.png)

Click "build" to see more details.

![03-build-info](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkpq3ymt5qcbk29bu2gr.png)

Click "Set up job" for more info.

```
Current runner version: '2.285.1'
Operating System
  Ubuntu
  20.04.3
  LTS

Virtual Environment
  Environment: ubuntu-20.04
  Version: 20211219.1
  Included Software: https://github.com/actions/virtual-environments/blob/ubuntu20/20211219.1/images/linux/Ubuntu2004-README.md
  Image Release: https://github.com/actions/virtual-environments/releases/tag/ubuntu20%2F20211219.1

Virtual Environment Provisioner
  1.0.0.0-main-20211214-1

GITHUB_TOKEN Permissions
  Actions: write
  Checks: write
  Contents: write
  Deployments: write
  Discussions: write
  Issues: write
  Metadata: read
  Packages: write
  Pages: write
  PullRequests: write
  RepositoryProjects: write
  SecurityEvents: write
  Statuses: write

Secret source: Actions
Prepare workflow directory
Prepare all required actions
Getting action download info
Download action repository 'actions/checkout@v2' (SHA:ec3a7ce113134d7a93b817d10a8272cb61118579)
```

Click "Run actions/checkout@v2" for more info.

```
Syncing repository: ajcwebdev/ajcwebdev-actions
Getting Git version info
Deleting the contents of '/home/runner/work/ajcwebdev-actions/ajcwebdev-actions'
Initializing the repository
Disabling automatic garbage collection
Setting up auth
Fetching the repository
Determining the checkout info
Checking out the ref
/usr/bin/git log -1 --format='%H'
'14b8a71b0852e18cd03880acad4cf4558b3bd0bd'
```

![04-action-output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szzpmt3s68cea1il4bkm.png)