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.
Related Resources
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:
- Overview: https://github.com/features/actions
- Documentation: https://help.github.com/en/actions
- Github Actions Marketplace: https://github.com/marketplace?type=actions
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.
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.