How to bring Github Actions to your project

Written byKalanKalan
💡

If you have any questions or feedback, pleasefill out this form

This post is translated by ChatGPT and originally written in Mandarin, so there may be some inaccuracies or mistakes.

A Brief Description of GitHub Actions

Built-in CI/CD for GitHub.

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

Introduction

In the past, implementing CI might involve lengthy discussions (or even arguments) within the team about the best CI solutions, such as CircleCI, DroneCI, Jenkins, and others. However, as long as the code is hosted on GitHub, integrating CI becomes a breeze with GitHub Actions. While it may not be a universal solution or a silver bullet, I find it quite straightforward for typical scenarios like running tests or even pushing and deploying code. Below, I will introduce how to get started.

How to Get Started

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

Before diving in, it might be quicker to share a few document links to get you started. However, if you prefer to read through the documentation first, here are some resources:

How to Add GitHub Actions to Your Project

A workflow consists of several jobs, and each job is made up of multiple steps. A step can be a GitHub Action, running a command, and so on. Here's 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 just by changing a file; you can use this ignore
      - 'docs/**'
      - 'README.md'
      - 'LICENSE'
      - 'CONTRIBUTING.md'
    branches-ignore:
      - 'xxxx'
    tags-ignore:
      - 'v1.*'
env: # Set 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, if you want to dynamically evaluate conditions 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' }}

When running, GitHub Actions automatically provides some context for you.

Environment Variables

Environment variables can be set in the secrets of your project. By default, only collaborators can see them, and others cannot access the secrets via Pull Requests in GitHub Actions.

Github Secret

This article serves as a quick reference on how to use GitHub Actions; next time you set it up, it should be much faster!

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

Buy me a coffee