GitHub Actions初体验

185 阅读2分钟

基本概念

GitHub Actions 是一种持续集成和持续交付 (CI/CD) 平台,可用于自动执行生成、测试和部署管道。 您可以创建工作流程来构建和测试存储库的每个拉取请求,或将合并的拉取请求部署到生产环境。

GitHub Actions 旨在帮助您建立强大而动态的自动化

GitHub 提供 Linux、Windows 和 macOS 虚拟机来运行工作流程

工作流程

企业微信截图_7d6484cf-57e9-4b11-834f-d6fb26de384e.png

  • 工作流

工作流程是一个可配置的自动化过程,它将运行一个或多个作业;通过yaml文件定义。

所有的工作流都在.github/workflows


name: GitHub Actions Demo

run-name: ${{ github.actor }} is testing out GitHub Actions 🚀

on: [push]

jobs:

Explore-GitHub-Actions:

runs-on: ubuntu-latest

steps:

- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."

- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"

- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."

- name: Check out repository code

uses: actions/checkout@v3

- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."

- run: echo "🖥️ The workflow is now ready to test your code on the runner."

- name: List files in the repository

run: |

ls ${{ github.workspace }}

- run: echo "🍏 This job's status is ${{ job.status }}."

if: "1 < 2"

- name: Github Context

env:

GITHUB_CONTEXT: ${{toJson(github)}}

run: echo '$GITHUB_CONTEXT'

  • 事件

事件是存储库中触发工作流程运行的特定活动

  • 作业

作业是工作流中在同一运行器上执行的一组步骤。 每个步骤要么是一个将要执行的 shell 脚本,要么是一个将要运行的动作。 步骤按顺序执行,并且相互依赖

  • 操作(步骤)

操作是用于 GitHub Actions 平台的自定义应用程序,它执行复杂但经常重复的任务。

YAML文件

  • actions引用

|-- hello-world (repository)

| |__ .github

| └── workflows

| └── my-first-workflow.yml

| └── actions

| |__ hello-world-action

| └── action.yml

// yum文件

jobs:

build:

runs-on: ubuntu-latest

steps:

# This step checks out a copy of your repository.

- uses: actions/checkout@v3

# This step references the directory that contains the action.

- uses: ./.github/actions/hello-world-action

jobs:

my_first_job:

steps:

- name: My first step

uses: actions/setup-node@v3

  • 变量

steps:

- name: "Say Hello Mona it's Monday"

run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"

env:

First_Name: Mona

  • 数据共享

jobs:

deploy:

environment:

name: github-pages

url: ${{ steps.deployment.outputs.page_url }}

runs-on: ubuntu-latest

steps:

- name: Checkout

uses: actions/checkout@v3

- name: Setup Pages

uses: actions/configure-pages@v3

- name: Upload artifact

uses: actions/upload-pages-artifact@v1

with:

# Upload entire repository

path: './docs/.vuepress/dist'

- name: Deploy to GitHub Pages

id: deployment

uses: actions/deploy-pages@v2

  • 表达式

使用${{expr}},如果使用了if关键字,可以直接写表达式

作为表达式的一部分,可使用 boolean、null、number 或 string 数据类型,表达式不能进行运算

企业微信截图_400b3d21-ecbe-4b43-bb57-68b301203792.png

应用

1、文档部署

2、通过curl方式发送消息