logo
🚀 DevOps

GitHub Actions

GitHub Actions Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · DevOps🧭 Markdown 速查🏷️ 2 个标签
#ci#github
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Introduction

GitHub Actions is a CI/CD platform that enables automation of software workflows, allowing developers to build, test, and deploy code right from their GitHub repositories.


Workflow Files

Github Action Workflows are defined in special YAML files, typically stored in a .github/workflows directory in the github repository.

YAML
滚动查看更多
name: hello-world
on: push
jobs:
    hello-world-job:
        runs-on: ubuntu-latest
        steps:
            - name: Hello World
              run: echo "Hello World!"

Viewing your workflow runs

  • On GitHub.com, navigate to the main page of the repository.
  • Under your repository name, click Actions.
  • In the left sidebar, click the workflow you want to display, in this example "hello-world"

Workflow Syntax
YAML
滚动查看更多
name: learn-github-actions
run-name: ${{ github.actor }} is learning GitHub Actions
on: [push]
jobs:
    check-bats-version:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4
            - uses: actions/setup-node@v3
              with:
                  node-version: '14'
            - run: npm install -g bats
            - run: bats -v
Workflow Syntax Descriptions
LineDescription
name:Sets the name of the GitHub Actions workflow. It's a label used for identifying the workflow in the repository.
run-name:Sets a custom name for the run, using a GitHub context ${{ github.actor }} to include the name of the user who initiated the run.
on:Specifies the event that triggers the workflow. In this case, the workflow is triggered on any push event to the repository.
jobs:Defines a group of jobs that will be executed as part of the workflow. Each job runs independently in the workflow.
check-bats-version:Identifier for a specific job within the workflow. This job is named check-bats-version.
runs-on:Specifies the type of machine to run the job on. Here, it's set to run on the latest version of Ubuntu.
steps:Contains a sequence of tasks (steps) that will be executed as part of the job.
uses:Used to specify an action to include as part of a step. For example, actions/checkout@v4 checks out the repository, and actions/setup-node@v3 sets up a Node environment.
with:Specifies additional parameters for the action. It's used in conjunction with uses to configure the action.
node-version:Contains parameter under with, specifying the version of Node.js to be set up by the setup-node action. In this case, it's set to version '14'.
Events
YAML
滚动查看更多
name: Event-trigger-on-push-example

on: [push] # event is defined here

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - name: Run a script
              run: echo "This workflow runs on every push to the repository."

Event Triggers

Event NameDescription
pushTriggers on a push to the repository.
pull_requestTriggers on pull request events.
pull_request_reviewTriggers on pull request review events.
pull_request_review_commentTriggers on comments on pull request reviews.
pull_request_targetFor workflows in forked repositories.
forkTriggers when a repository is forked.
issue_commentTriggers on issue and PR comments.
issuesTriggers on issue events.
labelTriggers on label events.
milestoneTriggers on milestone events.
deploymentTriggers on deployment.
deployment_statusTriggers on deployment status updates.
publicTriggers when repo goes from private to public.
repository_dispatchTriggers on a custom repository event.
scheduleTriggers on a defined schedule.
workflow_dispatchAllows manual triggering of the workflow.
workflow_runTriggers on the completion of another workflow.
createTriggers when a branch or tag is created.
deleteTriggers when a branch or tag is deleted.
page_buildTriggers on GitHub Pages build events.
releaseTriggers on release events.
watchTriggers when someone stars the repo.
registry_packageTriggers on registry package events.
statusTriggers on status updates to a Git commit.
projectTriggers on project board events.
project_cardTriggers on project card events.
project_columnTriggers on project column events.
memberTriggers on collaborator events.
gollumTriggers on wiki page updates.
Jobs

Single Job:

YAML
滚动查看更多
name: Single Job
on: [push]
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - name: Run a build script
              run: script/build

Multiple Jobs:

YAML
滚动查看更多
name: CI Workflow

on: [push]

jobs:
    job-1:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - name: Runs job 1
              run: echo "Running Job 1"

    job-2:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - name: Runs job 2
              run: echo "Running Job 2"

    job-3:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - name: Runs job 3
              run: echo "Running Job 3"
Steps
YAML
滚动查看更多
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            # step 1
            - name: Check out repository
              uses: actions/checkout@v2

            # step 2
            - name: Set up Node.js
              uses: actions/setup-node@v2
              with:
                  node-version: '14'

            # step 3
            - name: Install dependencies
              run: npm install

            # step 4
            - name: Run tests
              run: npm test
Github Runners & Self Hosted Runners

Github Hosted Runner:

YAML
滚动查看更多
name: Workflow
on: [push]
jobs:
    build:
        runs-on: ubuntu-latest # default runner
        steps:
            - uses: actions/checkout@v2
            - name: Run a script
              run: echo "Hello, world!"

Self Hosted Runner

YAML
滚动查看更多
name: Workflow with Self-Hosted Runner
on: [push]
jobs:
    build:
        runs-on: self-hosted
        steps:
            - uses: actions/checkout@v2
            - name: Run a script
              run: echo "Hello from self-hosted runner!"
Environment Variables

Custom variable defined using environments.

YAML
滚动查看更多
jobs:
    build:
        runs-on: ubuntu-latest
        env:
            CUSTOM_VARIABLE: 'Hello, World!' # Custom variable defined using env:
        steps:
            - name: Check environment variable
              run: echo "Value of CUSTOM_VAR is $CUSTOM_VAR"
Secrets

To add a new secret in your github repository nagivate to Repository > Settings > Security > Secrets and Variables > Actions > New Repository Secret

Example Secrets workflow:

YAML
滚动查看更多
name: Workflow with Secrets

on: [push]

jobs:
    example_job:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout repository
              uses: actions/checkout@v2
            - name: Use a secret
              run: echo "The secret is ${{ secrets.MY_SECRET }}"
Artifacts

To access your artifact navigate to Repository > Actions > Workflow Run > Artifacts

YAML
滚动查看更多
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Build project
              run: make build
            - name: Upload build artifact
              uses: actions/upload-artifact@v3 # upload Artifacts prebuilt action
              with:
                  name: my-artifact
                  path: path/to/artifact
Caching Dependencies

Dependency caches stores downloaded packages or compiled binaries of your workflows.

YAML
滚动查看更多
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - name: Cache dependencies
              uses: actions/cache@v2 # stores downloaded packages or compiled binaries
              with:
                  path: |
                      path/to/dependencies
                      another/path
                  key: ${{ runner.os }}-deps-${{ hashFiles('**/lockfile') }} # hash of the dependency lock file is generated in the OS
            - name: Install dependencies
              run: install-command
Matrix Strategies
YAML
滚动查看更多
jobs:
    build:
        runs-on: ubuntu-latest
        strategy:
            matrix:
                node-version: [12.x, 14.x, 16.x]
                # matrix strategy runs enables you to run jobs across multiple combinations of environments and OS's
                os: [ubuntu-latest, windows-latest, macOS-latest]
        steps:
            - uses: actions/checkout@v2
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v1
              with:
                  node-version: ${{ matrix.node-version }}
            - run: npm install
            - run: npm test
              env:
                  CI: true
Conditions and Expressions

Branch Conditions:

YAML
滚动查看更多
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v2
            - name: Run build
              if: github.ref == 'refs/heads/main' # "Run build" step will only execute if the current branch is main.
              run: make build

Event Trigger Conditions:

YAML
滚动查看更多
jobs:
    test:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v2
            - name: Run tests
              if: github.event_name == 'pull_request' # "Run tests" step is executed only when the workflow is triggered by a pull request event
              run: npm test
Workflow Commands

Depending on your OS, if you are running ubuntu-latest, bash commands should work

YAML
滚动查看更多
steps:
    - name: Set environment variable
      run: echo "NAME=value" >> $GITHUB_ENV
Concurrency

The concurrency field creates a group based on the github.head_ref. If a new run starts within the same concurrency group, it cancels any in-progress runs.

YAML
滚动查看更多
jobs:
    my_job:
        runs-on: ubuntu-latest
        concurrency:
            group: ${{ github.head_ref }}
            cancel-in-progress: true
        steps:
            - name: Run a script
              run: echo "Running script..."

Also see

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572