GitHub Actions 在线云编译 OpenWrt 固件
Github Actions 是 Microsoft 收购 GitHub 后推出的 CI/CD 服务,它提供了性能配置非常不错的虚拟服务器环境(E5 2vCPU/7G RAM),基于它可以进行构建、测试、打包、部署项目。对于公开仓库可每月免费使用2000分钟,这对于编译 OpenWrt 来说是非常充足的。
准备工作
GitHub 账号
搭建编译环境,生成.config文件。(可选)
基础使用
在自己搭建编译环境中使用 Lean's OpenWrt 源码生成.config文件。
将上面的Github项目地址打开,点fork到自己账号,然后点击Create new file(创建新文件)按钮。文件名填写为.config,把本地生成的.config 文件的内容复制粘贴到下面的文本框中。翻到页面最下方,点击Commit new file(提交新文件)按钮。在 Actions 页面选择Build OpenWrt,然后点击Run Workflow按钮,即可开始编译
最后经过一两个小时的等待,不出意外你就可以在 Actions 页面看到已经打包好的固件目录压缩包。
定时自动编译
点击查看
TIPS: 源码更新是不确定的,定时编译经常是在编译没有变动的源码,无意义且浪费资源,所以不建议使用。
编辑 workflow 文件(.github/workflows/build-openwrt.yml)取消注释下面两行。
# schedule:
# - cron: 0 8 * * 5
例子是北京时间每周五下午 4 点(16 时)开始编译(周末下班回家直接下载最新固件开始折腾)。如需自定义则按照 cron 格式修改即可,GitHub Actions 的时区为 UTC ,注意按照自己所在地时区进行转换。
编译一个简单的Openwrt
fork项目到自己账号,然后点Actions,选择需要编译的版本,进行编译,等待将近2个小时,就在release里面看到你的固件了。
全手动编译最新Lean大佬项目:
新建一个项目 New repository ,项目名称随意,创建项目。
手动添加代码
点最上面 add file,最上面写文件名字(完整),下面写代码,完了点 Commit changes
DIY-1.sh
XML/HTML代码
- # 此脚本用处是:添加第三方插件
- #=========================================================================================================================
- # 1-添加 ShadowSocksR Plus+ 插件
- sed -i 's/^#\(.*helloworld\)/\1/' feeds.conf.default
- # 2-添加 PowerOff 关机插件
- git clone https://github.com/WukongMaster/luci-app-poweroff.git package/luci-app-poweroff
- # 3-添加 opentomcat 主题
- git clone https://github.com/WukongMaster/luci-theme-opentomcat.git package/luci-theme-opentomcat
- # 4-添加 OpenClash 插件
- sed -i '$a\src-git openclash https://github.com/vernesong/OpenClash' ./feeds.conf.default
- # 5-添加 PassWall 插件
- echo "src-git passwall https://github.com/xiaorouji/openwrt-passwall.git;main" >> "feeds.conf.default"
- echo "src-git passwall2 https://github.com/xiaorouji/openwrt-passwall2.git;main" >> "feeds.conf.default"
- echo "src-git passwall_packages https://github.com/xiaorouji/openwrt-passwall-packages.git;main" >> "feeds.conf.default"
XML/HTML代码
- # 此脚本用处是:定制个性化参数
- #============================================================================================
- # 1-设置默认主题
- # sed -i 's/bootstrap/opentomcat/g' ./feeds/luci/collections/luci/Makefile
- # 2-设置管理地址
- sed -i 's/192.168.1.1/192.168.0.111/g' package/base-files/files/bin/config_generate
- # 3-编译内核版本
- sed -i 's/KERNEL_PATCHVER:=6.1/KERNEL_PATCHVER:=6.6/g' ./target/linux/x86/Makefile
- # 4-设置密码为空
- sed -i '/CYXluq4wUazHjmCDBCqXF/d' package/lean/default-settings/files/zzz-default-settings
- # 5-修改时间格式
- sed -i 's/os.date()/os.date("%Y-%m-%d %H:%M:%S")/g' package/lean/autocore/files/*/index.htm
- # 6-添加固件日期
- sed -i 's/IMG_PREFIX:=/IMG_PREFIX:=$(BUILD_DATE_PREFIX)-/g' ./include/image.mk
- sed -i '/DTS_DIR:=$(LINUX_DIR)/a\BUILD_DATE_PREFIX := $(shell date +'%F')' ./include/image.mk
- # 7-修正硬件信息
- sed -i 's/${g}.*/${a}${b}${c}${d}${e}${f}${hydrid}/g' package/lean/autocore/files/x86/autocore
- # 8-增固件连接数
- sed -i '/customized in this file/a net.netfilter.nf_conntrack_max=165535' package/base-files/files/etc/sysctl.conf
XML/HTML代码
- # 此脚本用处是:定制个性化参数
- #============================================================================================
- # 1-设置默认主题
- # sed -i 's/bootstrap/opentomcat/g' ./feeds/luci/collections/luci/Makefile
- # 2-设置管理地址
- sed -i 's/192.168.1.1/192.168.0.111/g' package/base-files/files/bin/config_generate
- # 3-编译内核版本
- # sed -i 's/KERNEL_PATCHVER:=6.1/KERNEL_PATCHVER:=6.6/g' ./target/linux/x86/Makefile
- # 4-设置密码为空
- sed -i '/CYXluq4wUazHjmCDBCqXF/d' package/lean/default-settings/files/zzz-default-settings
- # 5-修改时间格式
- sed -i 's/os.date()/os.date("%Y-%m-%d %H:%M:%S")/g' package/lean/autocore/files/*/index.htm
- # 6-添加固件日期
- sed -i 's/IMG_PREFIX:=/IMG_PREFIX:=$(BUILD_DATE_PREFIX)-/g' ./include/image.mk
- sed -i '/DTS_DIR:=$(LINUX_DIR)/a\BUILD_DATE_PREFIX := $(shell date +'%F')' ./include/image.mk
- # 7-修正硬件信息
- sed -i 's/${g}.*/${a}${b}${c}${d}${e}${f}${hydrid}/g' package/lean/autocore/files/x86/autocore
- # 8-增固件连接数
- sed -i '/customized in this file/a net.netfilter.nf_conntrack_max=165535' package/base-files/files/etc/sysctl.conf
x86.config
把本地环境生成的配置 .config 上传到github或者在线创建文件粘贴配置,如果是其他机型,可以新建H5.config 等自定义设备名称的配置文件,在以下的 workflows 目录的 yml 编译文件中改成对应的配置文件名。
Actions编译
点Actions,根据向导确认,创建下面两个文件,也就是需要编译出结果的配置文件。
OpenWrt-自用版.yml
XML/HTML代码
- name: OpenWrt-自用版
- on:
- workflow_dispatch:
- ## schedule:
- ## - cron: 1 0 * * *
- env:
- REPO_URL: https://github.com/coolsnowwolf/lede
- REPO_BRANCH: master
- FEEDS_CONF: feeds.conf.default
- CONFIG_FILE: x86.config
- DIY_P1_SH: DIY-1.sh
- DIY_P2_SH: DIY-2.sh
- # SSH_ACTIONS: true
- UPLOAD_FIRMWARE: true
- UPLOAD_RELEASE: true
- TZ: Asia/Shanghai
- jobs:
- build:
- runs-on: ubuntu-20.04
- steps:
- - name: 检查项目分支
- uses: actions/checkout@main
- - name: 清理磁盘空间
- uses: jlumbroso/free-disk-space@main
- with:
- tool-cache: true
- android: true
- dotnet: true
- haskell: true
- large-packages: true
- swap-storage: true
- - name: 查看机器配置
- run: |
- echo "警告⚠"
- echo "分配的服务器性能有限,若选择的插件过多,务必注意CPU性能!"
- echo -e "CPU性能排序: 7763 > 8370C > 8272CL > 8171M > E5-2673\n"
- echo "--------------------------CPU信息--------------------------"
- echo "CPU物理数量: $(cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l)"
- echo "CPU核心数量: $(nproc)"
- echo -e "CPU型号信息:$(cat /proc/cpuinfo | grep -m1 name | awk -F: '{print $2}')\n"
- echo "--------------------------内存信息--------------------------"
- echo "已安装内存详细信息:"
- echo -e "$(sudo lshw -short -C memory | grep GiB)\n"
- echo "--------------------------硬盘信息--------------------------"
- echo "硬盘数量: $(ls /dev/sd* | grep -v [1-9] | wc -l)" && df -hT
- - name: 安装编译环境
- env:
- DEBIAN_FRONTEND: noninteractive
- run: |
- sudo rm -rf /etc/apt/sources.list.d/* /usr/share/dotnet /usr/local/lib/android /opt/ghc
- sudo -E apt-get -qq update
- sudo -E apt-get -qq install $(curl -fsSL https://github.com/iii80/openwrt-list/raw/main/depends-ubuntu-2004)
- sudo -E apt-get -qq autoremove --purge
- sudo -E apt-get -qq clean
- sudo timedatectl set-timezone "$TZ"
- sudo mkdir -p /workdir
- sudo chown $USER:$GROUPS /workdir
- - name: 克隆固件源码
- working-directory: /workdir
- run: |
- df -hT $PWD
- git clone $REPO_URL -b $REPO_BRANCH openwrt
- ln -sf /workdir/openwrt $GITHUB_WORKSPACE/openwrt
- - name: 添加外部插件
- run: |
- [ -e $FEEDS_CONF ] && mv $FEEDS_CONF openwrt/feeds.conf.default
- chmod +x $DIY_P1_SH
- cd openwrt
- $GITHUB_WORKSPACE/$DIY_P1_SH
- - name: 更新外部插件
- run: cd openwrt && ./scripts/feeds update -a
- - name: 安装外部插件
- run: cd openwrt && ./scripts/feeds install -a
- - name: 加载个人设置
- run: |
- [ -e files ] && mv files openwrt/files
- [ -e $CONFIG_FILE ] && mv $CONFIG_FILE openwrt/.config
- chmod +x $DIY_P2_SH
- cd openwrt
- $GITHUB_WORKSPACE/$DIY_P2_SH
- - name: 下载固件dL库
- id: package
- run: |
- cd openwrt
- make defconfig
- make download -j8
- find dl -size -1024c -exec ls -l {} \;
- find dl -size -1024c -exec rm -f {} \;
- # - name: SSH connection to Actions
- # uses: P3TERX/ssh2actions@main
- # if: env.SSH_ACTIONS == 'true' || contains(github.event.action, 'ssh')
- - name: 正在编译固件
- id: compile
- run: |
- cd openwrt
- echo -e "$(nproc) thread compile"
- make -j$(nproc) || make -j1 || make -j1 V=s
- echo "status=success" >> $GITHUB_OUTPUT
- grep '^CONFIG_TARGET.*DEVICE.*=y' .config | sed -r 's/.*DEVICE_(.*)=y/\1/' > DEVICE_NAME
- [ -s DEVICE_NAME ] && echo "DEVICE_NAME=_$(cat DEVICE_NAME)" >> $GITHUB_ENV
- echo "FILE_DATE=_$(date +"%Y%m%d%H%M")" >> $GITHUB_ENV
- echo "FILE_TIME=$(date "+%Y年%m月%d日")" >> $GITHUB_ENV
- echo "TAG_TIME=$(date +"%Y.%m.%d-%H%M%S")" >> $GITHUB_ENV
- - name: 查看磁盘用量
- if: (!cancelled())
- run: df -hT
- - name: 打包整理固件
- id: organize
- if: env.UPLOAD_FIRMWARE == 'true' && !cancelled()
- run: |
- cd openwrt/bin/targets/*/*
- rm -rf packages && mkdir packages
- find -name "*esxi-flat.vmdk*" | xargs -i mv -f {} packages
- find -name "*kernel.bin*" | xargs -i mv -f {} packages
- find -name "*rootfs*" | xargs -i mv -f {} packages
- find -name "*.manifest*" | xargs -i mv -f {} packages
- find -name "*vmlinuz*" | xargs -i mv -f {} packages
- find -name "*esxi.vmdk*" | xargs -i mv -f {} packages
- find -name "*buildinfo*" | xargs -i mv -f {} packages
- find -name "*json*" | xargs -i mv -f {} packages
- find -name "*sha256sums*" | xargs -i mv -f {} packages
- rm -rf packages
- echo "FIRMWARE=$PWD" >> $GITHUB_ENV
- echo "status=success" >> $GITHUB_OUTPUT
- - name: 打包上传固件
- uses: actions/upload-artifact@main
- if: steps.organize.outputs.status == 'success' && !cancelled()
- with:
- name: OpenWrt-Mini${{ env.DEVICE_NAME }}${{ env.FILE_DATE }}
- path: ${{ env.FIRMWARE }}
- - name: 创建发布标签
- id: tag
- if: env.UPLOAD_RELEASE == 'true' && !cancelled()
- run: |
- echo "release_tag=${{ env.TAG_TIME }}" >> $GITHUB_OUTPUT
- touch release.txt
- echo "OpenWrt-Mini_ X86_64-自用版" >> release.txt
- echo "默认地址:192.168.0.111" >> release.txt
- echo "默认密码:无密码" >> release.txt
- echo "插件清单:ShadowSocksR Plus+,PassWall,OpenClash" >> release.txt
- echo "status=success" >> $GITHUB_OUTPUT
- - name: 发布新版固件
- uses: softprops/action-gh-release@master
- if: steps.tag.outputs.status == 'success' && !cancelled()
- env:
- GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
- with:
- name: ${{ env.FILE_TIME }} -自用版
- tag_name: ${{ steps.tag.outputs.release_tag }}
- body_path: release.txt
- files: ${{ env.FIRMWARE }}/*
- - name: 清理编译流程
- uses: Mattraks/delete-workflow-runs@main
- with:
- retain_days: 0
- keep_minimum_runs: 2
- - name: 删除旧版固件
- uses: dev-drprasad/delete-older-releases@master
- if: env.UPLOAD_RELEASE == 'true' && !cancelled()
- with:
- keep_latest: 10
- delete_tags: true
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OpenWrtR-R23.7.7-自用版.yml
XML/HTML代码
- name: OpenWrt-R23.7.7-自用版
- on:
- workflow_dispatch:
- ## schedule:
- ## - cron: 1 0 * * *
- env:
- REPO_URL: https://github.com/coolsnowwolf/lede
- REPO_BRANCH: master
- FEEDS_CONF: feeds.conf.default
- CONFIG_FILE: x86.config
- DIY_P1_SH: DIY-1.sh
- DIY_P3_SH: DIY-3.sh
- # SSH_ACTIONS: true
- UPLOAD_FIRMWARE: true
- UPLOAD_RELEASE: true
- TZ: Asia/Shanghai
- jobs:
- build:
- runs-on: ubuntu-20.04
- steps:
- - name: 检查项目分支
- uses: actions/checkout@main
- - name: 清理磁盘空间
- uses: jlumbroso/free-disk-space@main
- with:
- tool-cache: true
- android: true
- dotnet: true
- haskell: true
- large-packages: true
- swap-storage: true
- - name: 查看机器配置
- run: |
- echo "警告⚠"
- echo "分配的服务器性能有限,若选择的插件过多,务必注意CPU性能!"
- echo -e "CPU性能排序: 7763 > 8370C > 8272CL > 8171M > E5-2673\n"
- echo "--------------------------CPU信息--------------------------"
- echo "CPU物理数量: $(cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l)"
- echo "CPU核心数量: $(nproc)"
- echo -e "CPU型号信息:$(cat /proc/cpuinfo | grep -m1 name | awk -F: '{print $2}')\n"
- echo "--------------------------内存信息--------------------------"
- echo "已安装内存详细信息:"
- echo -e "$(sudo lshw -short -C memory | grep GiB)\n"
- echo "--------------------------硬盘信息--------------------------"
- echo "硬盘数量: $(ls /dev/sd* | grep -v [1-9] | wc -l)" && df -hT
- - name: 安装编译环境
- env:
- DEBIAN_FRONTEND: noninteractive
- run: |
- sudo rm -rf /etc/apt/sources.list.d/* /usr/share/dotnet /usr/local/lib/android /opt/ghc
- sudo -E apt-get -qq update
- sudo -E apt-get -qq install $(curl -fsSL https://github.com/iii80/openwrt-list/raw/main/depends-ubuntu-2004)
- sudo -E apt-get -qq autoremove --purge
- sudo -E apt-get -qq clean
- sudo timedatectl set-timezone "$TZ"
- sudo mkdir -p /workdir
- sudo chown $USER:$GROUPS /workdir
- - name: 克隆固件源码
- working-directory: /workdir
- run: |
- df -hT $PWD
- git clone $REPO_URL -b $REPO_BRANCH openwrt
- ln -sf /workdir/openwrt $GITHUB_WORKSPACE/openwrt
- cd openwrt
- git reset --hard b201c29fa2749a6265ebf0b80ac3a94ad34ba6e4
- - name: 添加外部插件
- run: |
- [ -e $FEEDS_CONF ] && mv $FEEDS_CONF openwrt/feeds.conf.default
- chmod +x $DIY_P1_SH
- cd openwrt
- $GITHUB_WORKSPACE/$DIY_P1_SH
- ## sed -i 's/KERNEL_PATCHVER:=6.6/KERNEL_PATCHVER:=6.1/g' ./target/linux/x86/Makefile
- - name: 更新外部插件
- run: cd openwrt && ./scripts/feeds update -a
- - name: 安装外部插件
- run: cd openwrt && ./scripts/feeds install -a
- - name: 加载个人设置
- run: |
- [ -e files ] && mv files openwrt/files
- [ -e $CONFIG_FILE ] && mv $CONFIG_FILE openwrt/.config
- chmod +x $DIY_P3_SH
- cd openwrt
- $GITHUB_WORKSPACE/$DIY_P3_SH
- - name: 下载固件dL库
- id: package
- run: |
- cd openwrt
- make defconfig
- make download -j8
- find dl -size -1024c -exec ls -l {} \;
- find dl -size -1024c -exec rm -f {} \;
- # - name: SSH connection to Actions
- # uses: P3TERX/ssh2actions@main
- # if: env.SSH_ACTIONS == 'true' || contains(github.event.action, 'ssh')
- - name: 正在编译固件
- id: compile
- run: |
- cd openwrt
- echo -e "$(nproc) thread compile"
- make -j$(nproc) || make -j1 || make -j1 V=s
- echo "status=success" >> $GITHUB_OUTPUT
- grep '^CONFIG_TARGET.*DEVICE.*=y' .config | sed -r 's/.*DEVICE_(.*)=y/\1/' > DEVICE_NAME
- [ -s DEVICE_NAME ] && echo "DEVICE_NAME=_$(cat DEVICE_NAME)" >> $GITHUB_ENV
- echo "FILE_DATE=_$(date +"%Y%m%d%H%M")" >> $GITHUB_ENV
- echo "FILE_TIME=$(date "+%Y年%m月%d日")" >> $GITHUB_ENV
- echo "TAG_TIME=$(date +"%Y.%m.%d-%H%M%S")" >> $GITHUB_ENV
- - name: 查看磁盘用量
- if: (!cancelled())
- run: df -hT
- - name: 打包整理固件
- id: organize
- if: env.UPLOAD_FIRMWARE == 'true' && !cancelled()
- run: |
- cd openwrt/bin/targets/*/*
- rm -rf packages && mkdir packages
- find -name "*esxi-flat.vmdk*" | xargs -i mv -f {} packages
- find -name "*kernel.bin*" | xargs -i mv -f {} packages
- find -name "*rootfs*" | xargs -i mv -f {} packages
- find -name "*.manifest*" | xargs -i mv -f {} packages
- find -name "*vmlinuz*" | xargs -i mv -f {} packages
- find -name "*esxi.vmdk*" | xargs -i mv -f {} packages
- find -name "*buildinfo*" | xargs -i mv -f {} packages
- find -name "*json*" | xargs -i mv -f {} packages
- find -name "*sha256sums*" | xargs -i mv -f {} packages
- rm -rf packages
- echo "FIRMWARE=$PWD" >> $GITHUB_ENV
- echo "status=success" >> $GITHUB_OUTPUT
- - name: 打包上传固件
- uses: actions/upload-artifact@main
- if: steps.organize.outputs.status == 'success' && !cancelled()
- with:
- name: OpenWrt-Mini${{ env.DEVICE_NAME }}${{ env.FILE_DATE }}
- path: ${{ env.FIRMWARE }}
- - name: 创建发布标签
- id: tag
- if: env.UPLOAD_RELEASE == 'true' && !cancelled()
- run: |
- echo "release_tag=${{ env.TAG_TIME }}" >> $GITHUB_OUTPUT
- touch release.txt
- echo "OpenWrt-Mini_ X86_64-R23.7.7-自用版" >> release.txt
- echo "默认地址:192.168.0.111" >> release.txt
- echo "默认密码:无密码" >> release.txt
- echo "插件清单:ShadowSocksR Plus+,PassWall,OpenClash" >> release.txt
- echo "status=success" >> $GITHUB_OUTPUT
- - name: 发布新版固件
- uses: softprops/action-gh-release@master
- if: steps.tag.outputs.status == 'success' && !cancelled()
- env:
- GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
- with:
- name: ${{ env.FILE_TIME }} -R23.7.7-自用版
- tag_name: ${{ steps.tag.outputs.release_tag }}
- body_path: release.txt
- files: ${{ env.FIRMWARE }}/*
- - name: 清理编译流程
- uses: Mattraks/delete-workflow-runs@main
- with:
- retain_days: 0
- keep_minimum_runs: 2
- - name: 删除旧版固件
- uses: dev-drprasad/delete-older-releases@master
- if: env.UPLOAD_RELEASE == 'true' && !cancelled()
- with:
- keep_latest: 10
- delete_tags: true
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
注意:首次编译之前必须新建一个release,比如v1.0.0,才可以正常生成release。
详细代码可以参考:https://github.com/iii80/My-lede