在 GitHub Actions 中使用自己的 Makefile 是一个常见的需求,特别是当您希望自动化构建、测试或部署过程时。以下是一个详细的指南,介绍如何在 GitHub Actions 中使用 Makefile。
首先,确保您的项目根目录中有一个 Makefile
。这是一个简单的示例 Makefile
:
# Makefile
.PHONY: all build test
all: build test
build:
echo "Building the project..."
# 这里可以添加实际的构建命令,例如:gcc main.c -o main
test:
echo "Running tests..."
# 这里可以添加实际的测试命令,例如:./run_tests.sh
在您的项目中,创建一个 GitHub Actions 工作流文件。工作流文件通常位于 .github/workflows/
目录下,并以 .yml
或 .yaml
作为扩展名。
以下是一个示例工作流文件,名为 ci.yml
:
# .github/workflows/ci.yml
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Make
run: sudo apt-get install -y make
- name: Run Makefile
run: make all
name: CI
:定义工作流的名称。on
:指定何时触发工作流。在这个示例中,工作流会在推送到 main
分支或创建针对 main
分支的拉取请求时触发。jobs
:定义工作流中的一个或多个作业。 build
:作业的名称。runs-on: ubuntu-latest
:指定作业运行的环境。在这个示例中,使用最新的 Ubuntu 版本。steps
:定义作业中的步骤。 name: Checkout code
:使用 actions/checkout@v2
操作从 GitHub 仓库中检出代码。name: Set up Make
:安装 make
工具。name: Run Makefile
:运行 make all
命令,这将执行 Makefile
中的 all
目标。将您的 Makefile
和 GitHub Actions 工作流文件提交到您的 GitHub 仓库:
git add Makefile .github/workflows/ci.yml
git commit -m "Add Makefile and GitHub Actions workflow"
git push origin main
推送更改后,您可以在 GitHub 仓库的 "Actions" 选项卡中查看工作流的运行结果。工作流会自动运行,并显示每个步骤的输出和状态。
云+社区沙龙online第6期[开源之道]
高校公开课
腾讯云存储专题直播
企业创新在线学堂
北极星训练营
北极星训练营
北极星训练营
北极星训练营
开箱吧腾讯云
Elastic 实战工作坊
领取专属 10元无门槛券
手把手带您无忧上云