DEV Community

Cover image for a first look at github actions
ajcwebdev
ajcwebdev

Posted on • Updated on • Originally published at ajcwebdev.com

 

a first look at github actions

Introduction

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

mkdir -p ajcwebdev-actions/.github/workflows
cd ajcwebdev-actions
echo > .github/workflows/hello.yml
echo '.DS_Store' > .gitignore
Enter fullscreen mode Exit fullscreen mode

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:
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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 }}."
Enter fullscreen mode Exit fullscreen mode

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 }}."
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

01-actions-tab

Click your action to see the specific workflow.

02-action-summary

Click "build" to see more details.

03-build-info

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)
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

04-action-output

Top comments (2)

Collapse
 
cerchie profile image
Lucia Cerchie

I just did the official Github tutorial last week-- this was a really clear review, thank you!

Collapse
 
ajcwebdev profile image
ajcwebdev

Awesome, I've been hearing about actions forever from @bdougieyo and finally decided to take the dive, very glad that I did!

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.