a first look at github actions

Create an Action

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

mkdir -p ajcwebdev-actions/.github/workflows
cd ajcwebdev-actions
touch .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.

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.

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.

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.

- 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.

# .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.

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

Create a new blank repository

You can create a blank repository by visiting repo.new or using the gh repo create command with the GitHub CLI. 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.

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.

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.

Click your action to see the specific workflow.

Click "build" to see more details.

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'

7