loading...

Learning Notes of GitHub Actions

ssmak profile image Steve Mak ・3 min read

Brief Intro.

GitHub Actions is the pipelines of CI/CD workflows on GitHub. You can create more than one workflow in a repository. You must store workflows in the .github/workflows directory in the root of your repository.

Workflows must have at least one job, and jobs contain a set of steps that perform individual tasks. Steps can run commands or use an action. You can create your own actions or use actions shared by the GitHub community and customzie them as needed.
(Workflows > Jobs > Steps)

The Basic

1) Creating a workflow file in the root of the repository (~/.github/workflows)

mkdir .github
cd .github
mkdir workflows

2) In .github/workflows, add a .yml or .yaml file for the workflow. For example, .github/workflows/continuous-integration-workflow.yml

touch ~/.github/workflows/continuous-integration-workflow.yml

3) Use the "Workflow syntax for GitHub Actions" reference documentation to choose events to trigger an action, add actions, and customize your workflow.

4) Commit your changes in the workflow file to the branch where you want your workflow to run.

Sample 1

name: Greet Everyone
# This workflow is triggered when changes are pushed to any branch in the repository.
on: [push]

# Trigger on schedule
#on:
#  schedule:
#    - cron:  '0 * * * *'

# Trigger on push with filtering for specific branches, tags, and paths
#on:
#  push:
#    branches:
#      - master
#    tags:
#      - v1
#    # file paths to consider in the event. Optional; defaults to all.
#    paths:
#      - 'test/*'

jobs:
  build:
    # Job name is Greeting
    name: Greeting
    # This job runs on Linux
    runs-on: ubuntu-latest
    steps:
      # Clone the repository
      - name: Clone the repository
        uses: actions/checkout@v2
      # This step uses GitHub's hello-world-javascript-action: https://github.com/actions/hello-world-javascript-action
      - name: Hello world
        uses: actions/hello-world-javascript-action@v1
        with:
          who-to-greet: 'Mona the Octocat'
        id: hello
      # This step prints an output (time) from the previous step's action.
      - name: Echo the greeting's time
        run: echo 'The time was ${{ steps.hello.outputs.time }}.'

Sample 2

name: CI Workflow Sample 2
on:
  push:
    branches:
      - master
jobs:
  build:
    name: Build from source
    runs-on: ubuntu-latest
    steps:
      - name: Clone source from repository
        uses: actions/checkout@v2
        with:
          ref: 'master'
      - name: Prepare build environment
        uses: actions/setup-node@v1
        with:
          node-version: '10.x'
      - name: Build for production
        run: cd Sources && npm install && gulp build --env prod
      - name: Show current directory
        run: pwd
      - name: Create archive for production build
        run: 'cd Sources && tar -cvf Release.zip build/prod'

Sample 3

name: ci -> cd
on:
  push:
    branches:
      - master
jobs:
  build:
    name: Build from source
    runs-on: ubuntu-latest
    steps:
      - name: Clone source from repository
        uses: actions/checkout@v2
        with:
          ref: 'master'
      - name: Prepare build environment
        uses: actions/setup-node@v1
        with:
          node-version: '12.x'
      - name: Build for production
        run: |
          npm install
          npm run build
      - name: Generate artifacts for release
        run: |
          mkdir Release
          cp -r dist/* Release
          cp -r public/* Release
      - uses: actions/upload-artifact@v1
        with:
          name: Release
          path: Release
  deploy:
    needs: build #this job depends on build job (run after when the build job completed)
    name: Deploy artifacts to ftp
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v1
        with:
          name: Release
      - name: Upload to ftp
        uses: sebastianpopp/ftp-action@releases/v2
        with:
          host: ${{ secrets.FTP_SERVER }}
          user: ${{ secrets.FTP_USERNAME }}
          password: ${{ secrets.FTP_PASSWORD }}
          localDir: "Release"
          remoteDir: "demo.ssmak.xyz"

Discussion

pic
Editor guide