Skip to content

fix github token

fix github token #12

name: build-native-nuget
on:
push:
branches:
- master
paths:
- .github/workflows/build-native-nuget.yml
workflow_dispatch:
permissions:
packages: write
jobs:
build-all:
runs-on: ubuntu-22.04
strategy:
matrix:
include:
- { title: win64, rid: win-x64, url: https://paddle-inference-lib.bj.bcebos.com/3.0.0/cxx_c/Windows/CPU/x86-64_avx-mkl-vs2019/paddle_inference_c.zip }
# - { title: win64-cu118, rid: win-x64, url: https://paddle-inference-lib.bj.bcebos.com/3.0.0/cxx_c/Windows/GPU/x86-64_cuda11.8_cudnn8.6.0_trt8.5.1.7_mkl_avx_vs2019/paddle_inference_c.zip }
# - { title: win64-cu126, rid: win-x64, url: https://paddle-inference-lib.bj.bcebos.com/3.0.0/cxx_c/Windows/GPU/x86-64_cuda12.6_cudnn9.5.1_trt10.5.0.18_mkl_avx_vs2019/paddle_inference_c.zip }
- { title: linux64, rid: linux-x64, url: https://paddle-inference-lib.bj.bcebos.com/3.0.0/cxx_c/Linux/CPU/gcc8.2_avx_mkl/paddle_inference_c.tgz }
# - { title: linux64-cu118, rid: linux-x64, url: https://paddle-inference-lib.bj.bcebos.com/3.0.0/cxx_c/Linux/GPU/x86-64_gcc11.2_avx_mkl_cuda11.8_cudnn8.9.7-trt8.6.1.6/paddle_inference_c.tgz }
# - { title: linux64-cu126, rid: linux-x64, url: https://paddle-inference-lib.bj.bcebos.com/3.0.0/cxx_c/Linux/GPU/x86-64_gcc11.2_avx_mkl_cuda12.6_cudnn9.5.1-trt10.5.0.18/paddle_inference_c.tgz }
# - { title: osx-x64, rid: osx-x64, url: https://paddle-inference-lib.bj.bcebos.com/3.0.0/cxx_c/MacOS/x86-64_clang_avx_accelerate_blas/paddle_inference_c.tgz }
# - { title: osx-arm64, rid: osx-arm64, url: https://paddle-inference-lib.bj.bcebos.com/3.0.0/cxx_c/MacOS/m1_clang_noavx_accelerate_blas/paddle_inference_c.tgz }
steps:
- name: Cache
id: cache-inference-lib
uses: actions/cache@v4
with:
path: paddle_inference_c
key: ${{ matrix.url }}
- name: Download
if: steps.cache-inference-lib.outputs.cache-hit != 'true'
run: |
wget -q -O archive_file.tmp ${{ matrix.url }}
if [[ "${{ matrix.rid }}" == "win-x64" ]]; then
unzip -q archive_file.tmp -d paddle_inference_c
else
mkdir paddle_inference_c && tar -xzf archive_file.tmp -C paddle_inference_c
fi
rm archive_file.tmp
- name: Show Downloaded
run: |
ls -lR paddle_inference_c
- name: Copy Native Dynamic Libs(.so/.dll/.dylib) to nuget
run: |
mkdir -p nuget
if [[ "${{ matrix.rid }}" == "win-x64" ]]; then
find paddle_inference_c -type f \( -iname "*.dll" \) -exec cp {} nuget/ \;
elif [[ "${{ matrix.rid }}" == "linux-x64" ]]; then
find paddle_inference_c -type f \( -iname "*.so*" \) -exec cp {} nuget/ \;
elif [[ "${{ matrix.rid }}" == "osx-x64" || "${{ matrix.rid }}" == "osx-arm64" ]]; then
find paddle_inference_c -type f \( -iname "*.dylib" \) -exec cp {} nuget/ \;
fi
ls -l nuget
- name: Build NuGet package
env:
VERSION: 3.0.0.${{ github.run_number }}
run: |
set -euo pipefail
# ---------- 基本元数据 ----------
PKG_ID="Sdcb.PaddleInference.runtime.${{ matrix.title }}"
AUTHOR="sdcb"
LICENSE="Apache-2.0"
GIT_URL="https://github.com/sdcb/PaddleSharp"
TAGS="Sdcb PaddleSharp AI Paddle OCR PaddleOCR linqpad-samples"
YEAR=$(date +%Y)
# ---------- 找到所有动态库 ----------
NUGET_LIB_DIR="${{ github.workspace }}/nuget"
# find 出来的是全路径
mapfile -t LIB_PATHS < <(find "$NUGET_LIB_DIR" -type f)
if [ ${#LIB_PATHS[@]} -eq 0 ]; then
echo "Error: no native libs found in $NUGET_LIB_DIR"
exit 1
fi
# ---------- 准备临时工作目录 ----------
WORK="$RUNNER_TEMP/pkg"
rm -rf "$WORK"
mkdir -p \
"$WORK/runtimes/${{ matrix.rid }}/native" \
"$WORK/lib/netstandard2.0" \
"$WORK/lib/net45" \
"$WORK/build/net45"
# ---------- 复制所有动态库 ----------
for libpath in "${LIB_PATHS[@]}"; do
cp "$libpath" "$WORK/runtimes/${{ matrix.rid }}/native/"
done
# 放两个占位文件
touch "$WORK/lib/netstandard2.0/_._"
cp "$WORK/lib/netstandard2.0/_._" "$WORK/lib/net45/_._"
# ---------- 生成 .props ----------
NORMALIZED_NAME="$(echo "${PKG_ID}" | tr '.-' '_' )_Dlls"
PROPS_FILE="$WORK/build/net45/${PKG_ID}.props"
{
cat <<EOF
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<${NORMALIZED_NAME}>\$(MSBuildThisFileDirectory)..\\..\\runtimes</${NORMALIZED_NAME}>
</PropertyGroup>
EOF
# 每个库都插一段 ItemGroup
for libpath in "${LIB_PATHS[@]}"; do
libname="$(basename "$libpath")"
cat <<EOF
<ItemGroup Condition="\$(TargetFrameworkVersion.StartsWith('v4')) Or \$(TargetFramework.StartsWith('net4'))">
<Content Include="\$(${NORMALIZED_NAME})\\${{ matrix.rid }}\\native\\${libname}">
<Link>dll\\${{ matrix.arch }}\\${libname}</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
EOF
done
echo "</Project>"
} > "$PROPS_FILE"
# ---------- 生成 .nuspec ----------
NUSPEC_FILE="$WORK/${PKG_ID}.nuspec"
{
cat <<EOF
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>${PKG_ID}</id>
<version>${VERSION}</version>
<title>${PKG_ID} native bindings</title>
<authors>${AUTHOR}</authors>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<description>${{ matrix.rid }} native libs for PaddleInference.</description>
<summary>${{ matrix.rid }} native libs for PaddleInference.</summary>
<copyright>Copyright ${YEAR}</copyright>
<license type="expression">${LICENSE}</license>
<projectUrl>${GIT_URL}</projectUrl>
<repository type="git" url="${GIT_URL}.git" />
<tags>${TAGS}</tags>
<dependencies />
<frameworkAssemblies />
</metadata>
<files>
<file src="lib\\netstandard2.0\\_._" target="lib\\netstandard2.0" />
<file src="lib\\net45\\_._" target="lib\\net45" />
EOF
# 每个库都加一行 file
for libpath in "${LIB_PATHS[@]}"; do
libname="$(basename "$libpath")"
cat <<EOF
<file src="runtimes\\${{ matrix.rid }}\\native\\${libname}" target="runtimes\\${{ matrix.rid }}\\native" />
EOF
done
# 最后再把 props 文件也加进来
cat <<EOF
<file src="build\\net45\\${PKG_ID}.props" target="build\\net45" />
</files>
</package>
EOF
} > "$NUSPEC_FILE"
# ---------- 打包 ----------
pushd "$WORK" >/dev/null
nuget pack "${NUSPEC_FILE}" -OutputDirectory "${{ github.workspace }}/nupkgs"
popd >/dev/null
echo "::group::生成的 nupkg"
ls -l "${{ github.workspace }}/nupkgs"
echo "::endgroup::"
- name: Upload nupkg artifact
uses: actions/upload-artifact@v4
with:
name: nuget-${{ matrix.rid }}
path: nupkgs/*.nupkg
if-no-files-found: error
- name: Push nupkg to Github Packages
run: |
for f in ${{ github.workspace }}/nupkgs/*.nupkg
do
dotnet nuget push "$f" --source "https://nuget.pkg.github.com/${{ github.repository_owner }}" --api-key "${{ secrets.GITHUB_TOKEN }}" --skip-duplicate
done