Summary of Practical Examples With Github-Actions

Photo by Roman Synkevych on Unsplash

What is GitHub Actions?

Easily automate all software workflows with world-class CI / CD. You can build, test, and deploy code directly from GitHub to get code review, branch management, and problem triage to work the way you want.

Practical examples

  • Environmental variables (Global)

Environmental variables (Global)

on:
push:
branches:
- main
env:
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }}

Environmental variables (In step)

jobs:
Build:
runs-on: ubuntu-latest
steps:
- name: install
run: yarn install
env:
ENVIRONMENT: test

Multiple command simplification

jobs:
Build:
runs-on: ubuntu-latest
steps:
- name: Install_And_Build
run: |
yarn install
yarn build

Specify subfolder

jobs:
Build:
runs-on: ubuntu-latest
steps:
- name: deploy
working-directory: source
run: |
yarn deploy

Action trigger condition (Specified folder)

on:
push:
branches:
- master
paths:
- source/**

Action trigger condition (Other than the specified folder)

on:
push:
branches:
- master
paths-ignore:
- source/**

Execution order of multiple jobs

jobs:
Build:
runs-on: ubuntu-latest
steps:
- run: Test
Deploy:
needs:
- Build
runs-on: ubuntu-latest
steps:
- run: Test

Workflow execution between repositories → repository-dispatch(From)

jobs:
Backend:
runs-on: ubuntu-latest
steps:
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v1.1.0
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
repository: owner/repository
event-type: next

Workflow execution between repositories → repository-dispatch(To)

on:
repository_dispatch:
types: next

Send notification to Slack

jobs:
Slack:
runs-on: ubuntu-latest
steps:
- name: Slack Notify
uses: rtCamp/action-slack-notify@v2.0.1
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_CHANNEL: build
SLACK_TITLE: Build Success
SLACK_COLOR: '#43a047'
SLACK_MESSAGE: ${{ github.repository }} Build Success
SLACK_USERNAME: ${{ github.repository }}