通过 GitHub Actions 实现代码的自动编译和发布
GitHub Actions 是一个非常强大的工具,可以用来实现各种自动化任务,包括自动编译和发布 release。以下是一个基本的工作流程,展示如何使用 GitHub Actions 实现这一目标:
-
创建 GitHub Actions 工作流文件:在你的 GitHub 仓库中,创建一个
.github/workflows
目录,并在该目录中创建一个新的工作流文件,例如release.yml
。 -
定义工作流:在
release.yml
文件中,定义你的工作流步骤。以下是一个示例工作流,它演示了如何在每次推送到main
分支时进行编译并创建一个新的 release。
name: Build and Release
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "14"
- name: Install dependencies
run: npm install
- name: Build project
run: npm run build
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: build/
release:
needs: build
runs-on: ubuntu-latest
steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build-artifacts
- name: Create GitHub Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v1.0.0
release_name: Release v1.0.0
body: |
Release notes for v1.0.0
draft: false
prerelease: false
- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: build/
asset_name: build-artifacts.zip
asset_content_type: application/zip
解释
-
工作流名称和触发器:
name: Build and Release
:定义工作流的名称。on
条件定义了工作流何时被触发,这里是每当推送到main
分支时触发。
-
构建作业:
runs-on: ubuntu-latest
:指定工作流程运行的环境。actions/checkout@v4
:将代码仓库检出到 runner。actions/setup-node@v2
:设置 Node.js 环境。npm install
:安装项目依赖。npm run build
:构建项目。actions/upload-artifact@v4
:将构建的工件上传,以便在后续步骤中使用。actions/upload-artifact
的实现代码可以在https://github.com/actions/upload-artifact
找到。
-
发布作业:
needs: build
:指定此作业依赖于名为build
的作业。actions/download-artifact@v2
:下载构建工件。actions/create-release@v1
:在 GitHub 上创建一个新 release。GITHUB_TOKEN
用于进行身份验证。actions/upload-release-asset@v1
:将构建工件作为 release 资源上传。
注意事项
- GITHUB_TOKEN:GitHub 提供的内置秘密,用于在 Actions 中进行身份验证。
- 自定义步骤:根据你的项目和需求,你可能需要调整或添加步骤。例如,编译步骤可能会有所不同,具体取决于你使用的编程语言和构建工具。
- 版本号:在实际使用中,你可能希望自动生成版本号而不是硬编码版本号。
更多示例代码
# .github/workflows/github-actions-demo.yaml
# 将代码打包为 tar.gz 并发布
name: Auto Release # 工作流名
run-name: Auto Release # 运行时的工作流名
on: [push]
jobs:
build: # job 名可以自定义
runs-on: ubuntu-latest # 也可以是容器
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Package VM
run: tar -zcvf VM.tar.gz VM
- name: Package VPS
run: tar -zcvf VPS.tar.gz VPS
- name: Upload Artifact VM.tar.gz
uses: actions/upload-artifact@v4
with:
name: VM.tar.gz
path: VM.tar.gz
- name: Upload Artifact VPS.tar.gz
uses: actions/upload-artifact@v4
with:
name: VPS.tar.gz
path: VPS.tar.gz
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.extract_branch.outputs.branch }}-${{ github.run_number }}
release_name: Release for ${{ steps.extract_branch.outputs.branch }} at ${{ github.sha }}
draft: false
prerelease: false
- name: Upload Release Asset 1
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./VM.tar.gz
asset_name: VM.tar.gz
asset_content_type: application/gzip
- name: Upload Release Asset 2
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./VPS.tar.gz
asset_name: VPS.tar.gz
asset_content_type: application/gzip
# 一些可用变量
# runner.os: 运行 Action 的系统名
# job.status: job 的状态
# github.actor: 运行的 Action
# github.event_name: 触发 Action 的事件名
# github.ref: 当前分支名
# github.repository: 存储库名
# github.workspace: 存储库工作区