Build and Publish OpenWrt Image #341
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Publish OpenWrt Image | |
on: | |
workflow_dispatch: | |
inputs: | |
model: | |
description: 'Device Model:' | |
required: true | |
target: | |
description: 'Target Architecture:' | |
visible: false | |
version: | |
description: 'OpenWrt Version:' | |
required: true | |
packages: | |
description: 'Extra Packages:' | |
required: false | |
disabled_services: | |
description: 'Disabled Services:' | |
required: false | |
scripts: | |
description: 'UCI Defaults Scripts:' | |
required: false | |
permissions: | |
contents: write | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Load config | |
run: | | |
# Check if a script is specified and set it in the environment | |
if [ -n "${{ github.event.inputs.scripts }}" ]; then | |
echo "SCRIPT=${{ github.event.inputs.scripts }}" >> $GITHUB_ENV | |
if [ ! -f "files/etc/uci-defaults/${{ github.event.inputs.scripts }}" ]; then | |
echo "Error: Specified script file does not exist." | |
exit 1 | |
fi | |
find files/etc/uci-defaults -type f ! -name "${{ github.event.inputs.scripts }}" -delete | |
fi | |
# Create input configuration file | |
files="files" | |
cat <<EOF > input_config | |
openwrt_version=${{ github.event.inputs.version }} | |
packages=${{ github.event.inputs.packages }} | |
files=$files | |
profile=${{ github.event.inputs.model }} | |
target=${{ github.event.inputs.target }} | |
EOF | |
# Add disabled services to the configuration if specified | |
if [ -n "${{ github.event.inputs.disabled_services }}" ]; then | |
echo "disabled_services=${{ github.event.inputs.disabled_services }}" >> input_config | |
fi | |
# Append input configuration to environment variables | |
cat input_config >> $GITHUB_ENV | |
- name: Load secrets | |
if: ${{ github.event.inputs.scripts }} | |
run: | | |
# Inject secrets into the specified script | |
sed -i -e 's/^# ssid=".*"/ssid="${{ secrets.WLAN_NAME }}"/' \ | |
-e 's/^# ssid_key=".*"/ssid_key="${{ secrets.WLAN_PASS }}"/' \ | |
-e 's/^# root_password=".*"/root_password="${{ secrets.ROOT_PASS }}"/' \ | |
-e 's/^# pppoe_name=".*"/pppoe_name="${{ secrets.PPPOE_NAME }}"/' \ | |
-e 's/^# pppoe_key=".*"/pppoe_key="${{ secrets.PPPOE_PASS }}"/' \ | |
files/etc/uci-defaults/${{ env.SCRIPT }} | |
- name: Cache apt packages | |
uses: awalsh128/cache-apt-pkgs-action@latest | |
with: | |
packages: build-essential libncurses-dev zlib1g-dev gawk wget git gettext libssl-dev xsltproc rsync unzip python3 python3-setuptools zstd | |
version: 1.0 | |
- name: Install dependencies | |
run: | | |
# Update package list and install required dependencies | |
sudo apt update | |
sudo apt install -y \ | |
build-essential libncurses-dev zlib1g-dev gawk wget git gettext \ | |
libssl-dev xsltproc rsync unzip python3 python3-setuptools zstd | |
- name: Fetch version.buildinfo | |
id: fetch_version_buildinfo | |
run: | | |
# Determine the OpenWrt version and download the appropriate image builder | |
version="${{ env.openwrt_version }}" | |
target="${{ env.target }}" | |
# Fetch the latest version.buildinfo file | |
if [[ $version == "SNAPSHOT" ]]; then | |
url="https://downloads.openwrt.org/${version,,}s/targets/${target}/version.buildinfo" | |
else | |
url="https://downloads.openwrt.org/releases/${version}/targets/${target}/version.buildinfo" | |
fi | |
wget $url | |
# Get the latest version code | |
echo "version_buildinfo=$(cat version.buildinfo)" >> $GITHUB_ENV | |
- name: Cache OpenWrt Image Builder | |
id: cache-openwrt | |
uses: actions/cache@v4 | |
with: | |
path: openwrt-imagebuilder-*.tar.* | |
key: openwrt-imagebuilder-${{ env.openwrt_version }}-${{ env.target }}-${{ env.version_buildinfo }} | |
- name: Download OpenWrt Image Builder | |
if: steps.cache-openwrt.outputs.cache-hit != 'true' | |
run: | | |
# Determine the OpenWrt version and download the appropriate image builder | |
version="${{ env.openwrt_version }}" | |
target="${{ env.target }}" | |
if [[ $version == "SNAPSHOT" ]]; then | |
url="https://downloads.openwrt.org/${version,,}s/targets/${target}/openwrt-imagebuilder-$(echo $target | tr '/' '-').Linux-x86_64.tar.zst" | |
elif [[ $version < "24.10" ]]; then | |
url="https://downloads.openwrt.org/releases/${version}/targets/${target}/openwrt-imagebuilder-${version}-$(echo $target | tr '/' '-').Linux-x86_64.tar.xz" | |
else | |
url="https://downloads.openwrt.org/releases/${version}/targets/${target}/openwrt-imagebuilder-${version}-$(echo $target | tr '/' '-').Linux-x86_64.tar.zst" | |
fi | |
wget "$url" | |
- name: Build OpenWrt Image | |
run: | | |
tar -xf openwrt-imagebuilder-*.tar.* | |
# Move necessary files and run the build process | |
mv files openwrt-imagebuilder-*/ | |
cd openwrt-imagebuilder-*/ | |
make -j $(nproc) image \ | |
PROFILE="${{ env.profile }}" \ | |
PACKAGES="${{ env.packages }}" \ | |
FILES="${{ env.files }}" \ | |
DISABLED_SERVICES="${{ env.disabled_services }}" | |
- name: Write Build Info | |
run: | | |
# Determine if scripts are used | |
scripts="${{ env.SCRIPT != '' && env.SCRIPT || 'false' }}" | |
disabled_services="${{ env.disabled_services != '' && env.disabled_services || 'false' }}" | |
packages="${{ env.packages != '' && env.packages || 'default' }}" | |
# Get the target directory and write build info | |
target_dir=$(find openwrt-imagebuilder-*/bin/targets/*/*/ -type d -print -quit) | |
echo "image_path=$target_dir" >> $GITHUB_ENV | |
# Get version code from profiles.json | |
version_code=$(grep -o '"version_code":"[^"]*"' $target_dir/profiles.json | awk -F: '{print $2}' | tr -d '"') | |
# Create build info markdown file | |
cat <<EOF > build_info.md | |
## π‘ Custom OpenWrt Build Information | |
- **OpenWrt Version**: \`${{ env.openwrt_version }}\` | |
- **Target**: \`${{ env.target }}\` | |
- **Profile**: \`${{ env.profile }}\` | |
- **Package Selections**: \`${{ env.packages }}\` | |
- **Disabled Services**: \`$disabled_services\` | |
- **Version Code**: \`$version_code\` | |
- **UCI Defaults Scripts**: \`$scripts\` | |
----- | |
### π sha256sum | |
$(awk '$2 ~ /(sysupgrade|factory|recovery)\.bin$/ {printf "`%s`\n**%s**\n\n", $1, $2}' $target_dir/sha256sums) | |
EOF | |
- name: Release | |
uses: softprops/action-gh-release@v2 | |
with: | |
tag_name: ${{ github.event.inputs.model }}-${{ env.openwrt_version }}-${{ env.version_buildinfo }} | |
body_path: build_info.md | |
files: ${{ env.image_path }}/* | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |