DEV Community

loading...
Cover image for a first look at github actions

a first look at github actions

anthony-campolo
full stack web dev
Updated on ・2 min read

GitHub Actions can be used to automate, customize, and execute your software development workflows right in your repository. You can discover, create, and share actions to perform any job you'd like, including CI/CD, and combine actions in a completely customized workflow.

1. Create an Action

mkdir ajcwebdev-actions
cd ajcwebdev-actions
mkdir -p .github/workflows
touch .github/workflows/main.yml
Enter fullscreen mode Exit fullscreen mode

This is a basic workflow to help you get started with Actions.

name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:
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.

Jobs

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:
      - uses: actions/checkout@v2
      - name: Run a one-line script
        run: echo ajcwebdev-gh-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 "Branch name is ${{ github.ref }}."
          echo "Repository is ${{ github.repository }}."
          echo "Job status is ${{ job.status }}."
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 to print "ajcwebdev-gh-actions" (echo ajcwebdev-gh-actions) using the runners shell.

Complete GitHub Action

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 ajcwebdev-gh-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 "Branch name is ${{ github.ref }}."
          echo "Repository is ${{ github.repository }}."
          echo "Job status is ${{ job.status }}."
Enter fullscreen mode Exit fullscreen mode

2. Create a blank GitHub repository

Go to repo.new and create a new repository.

git init
git add .
git commit -m "first action"
git branch -M main
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-

Click your action to see the specific workflow.

02-

Click "build" to see more details.

03-

04-

05-

06-

07-

Discussion (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
anthony-campolo Author

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