Kalan's Blog

Software Engineer / Taiwanese / Life in Fukuoka

Current Theme light

Describing Github Actions in One Sentence

Built-in CI/CD in Github.

![github-actions](/img/Screenshot_2020-05-02 kjj6198 animal-crossing-info.png "github-actions")

Introduction

In the past, setting up CI in a team could involve some time-consuming discussions (or arguments) about CI solutions like CircleCI, DroneCI, Jenkins, etc. However, if the team hosts their code on Github, they can easily integrate CI with Github. While it may not be a panacea or a silver bullet, it is quite simple to achieve in general scenarios such as running tests or even pushing and deploying. Let's dive into the details.

How to Integrate

Simply add a .github/workflows/your-workflow.yml description file in the root directory of your code.

Before we begin, it may be faster to directly provide a few links to the documentation. If you prefer to read the documentation first before getting hands-on, you can refer to:

How to Add Github Actions to Your Project

A workflow consists of multiple jobs, and each job is composed of several steps, which can be a Github action or a command execution, etc. Here is a basic example:

name: your-name

# Refer to the documentation for available events
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#on
on:
  push:
    branches:
      - master
    paths-ignore: # Sometimes, you may not want to trigger Github Actions for certain file changes, you can use this ignore list
      - 'docs/**'
      - 'README.md'
      - 'LICENSE'
      - 'CONTRIBUTING.md'
    branches-ignore:
      - 'xxxx'
    tags-ignore:
      - 'v1.*'
env: # Define environment variables
  PROJECT_ID: ${{ secrets.PROJECT_ID }}
  RUN_REGION: asia-northeast2
  SERVICE_ACCOUNT: ${{ secrets.SERVICE_ACCOUNT }}

jobs:
  setup-build-and-deploy:
    name: Setup gcloud and deploy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
        with:
          version: '290.0.1'
          project_id: ${{ secrets.PROJECT_ID }}
          service_account_email: ${{ secrets.SA_EMAIL }}
          server_account_key: ${{ secrets.SA_KEY }}
          export_default_credentials: true
      - name: Deploy
        run: |-
          echo $SERVICE_ACCOUNT > /tmp/key.json && \
          gcloud auth activate-service-account --key-file /tmp/key.json && \
          gcloud app deploy --project "$PROJECT_ID"

For more advanced scenarios where you want to dynamically evaluate at runtime, you can write it like this: https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions

env:
  my_env_var: ${{ ALPHA ? 'A' : 'B' }}

Github Actions itself provides some context while running.

Environment Variables

Environment variables can be set in the project's secrets, which are only visible to collaborators by default. Others cannot inject secrets into Github Actions through pull requests.

Github Secret

This article serves as a quick note on how to use Github Actions, and the next time you set it up, it should be faster.

Prev

Svelte Notes (2): Compiler is more clever than you mess

Next

How to do smooth scroll with one line of CSS

If you found this article helpful, please consider buy me a drink ☕️ It'll make my ordinary day shine✨

Buy me a coffee

作者

Kalan 頭像照片,在淡水拍攝,淺藍背景

愷開 | Kalan

Hi, I'm Kai. I'm Taiwanese and moved to Japan in 2019 for work. Currently settled in Fukuoka. In addition to being familiar with frontend development, I also have experience in IoT, app development, backend, and electronics. Recently, I started playing electric guitar! Feel free to contact me via email for consultations or collaborations or music! I hope to connect with more people through this blog.