Kalan's Blog

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

四零二曜日電子報上線啦!訂閱訂起來

Software Engineer / Taiwanese / Life in Fukuoka
This blog supports RSS feed (all content), you can click RSS icon or setup through third-party service. If there are special styles such as code syntax in the technical article, it is still recommended to browse to the original website for the best experience.

Current Theme light

我會把一些不成文的筆記或是最近的生活雜感放在短筆記,如果有興趣的話可以來看看唷!

Please notice that currenly most of posts are translated by AI automatically and might contain lots of confusion. I'll gradually translate the post ASAP

How to bring Github Actions to your project

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