在 GitHub Action 上构建 HarmonyOS 应用
source link: https://www.phodal.com/blog/github-action-for-harmonyos/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
在 GitHub Action 上构建 HarmonyOS 应用
Posted by: Phodal Huang Feb. 28, 2024, 8:05 p.m.
最近在给 AutoDev 添加 HarmonyOS 相关功能的时候,在添加了一些功能之后,发现还需要添加 CI/CD 相关的功能。于是,便花了半个下午的时间,添加了对应的示例,以在 GitHub 上构建出 HarmonyOS 的 hap 应用。
最后代码见:https://github.com/harmonyos-dev/harmonyos-github-action-example
HarmonyOS 构建流程分析
在 DevEco Studio 构建应用时,会执行:
./node_modules/.bin/hvigor --mode module -p product=default assembleHap
所以,我的初次判断只需要探索如何结合 Node.js 来构建即可。
- 查找依赖。结合
oh-package.json5
找到了一些依赖信息。 - 构建工具。OpenHarmony 的文档:https://ohpm.openharmony.cn/#/cn/help/quickstart
- 详细依赖信息。在官方文档找到了两个对应的构建工具信息:
@ohos/hvigor
,@ohos/hvigor-ohos-plugin
。
然后就开始使用 Node.js 手动构建项目了。
步骤一:获取 HarmonyOS SDK
由于 HarmonyOS SDK 需要登陆后下载 SDK,所以我上传一份到 GitHub:https://github.com/harmonyos-dev/hos-sdk
随后,在 GitHub Action 下载 SDK。感谢 OpenAI 生成的下载代码:
# 获取 release 的信息
RELEASE_JSON=$(curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/harmonyos-dev/hos-sdk/releases/latest)
# 从 release 的信息中提取需要的 asset URL
ASSET_URL=$(echo "$RELEASE_JSON" | jq -r '.assets[] | select(.name | test("commandline-tools-mac-2.0.0.2.zip")) | .browser_download_url')
# 输出 asset URL
echo "Asset URL: $ASSET_URL"
# 下载 release asset
curl -LJO "$ASSET_URL"
步骤二:安装 HarmonyOS SDK
在这个步骤,遇到两个问题:
- GitHub Action 在国外,没有 country 相关配置,所以会下载不到 SDK。
- 需要
--accept-license
参数来自动同意 license。
# for handle Failed to request url https://devecostudio-dre.op.hicloud.com/sdkmanager/v5/hos/getSdkList
#[ ]0%
export JAVA_TOOL_OPTIONS="$JAVA_TOOL_OPTIONS -Duser.country=CN"
chmod +x command-line-tools/sdkmanager/bin/sdkmgr
# 安装 HarmonyOS SDK
command-line-tools/sdkmanager/bin/sdkmgr install toolchains:9 OpenHarmony/toolchains:9 --accept-license
步骤三:Node.js 构建 HarmonyOS 应用
在这个步骤,遇到两个问题:
- 需要配置好 npm registry
- 需要配置 SDK 路径。
npm init -y
npm config set @ohos:registry=https://repo.harmonyos.com/npm/
npm install @ohos/[email protected] @ohos/[email protected]
# for ERROR: Unable to find 'hwsdk.dir' in 'local.properties' or 'HOS_SDK_HOME' in the system environment path.
export HOS_SDK_HOME=$(pwd)/hwsdk
./node_modules/.bin/hvigor --mode module -p product=default assembleHap --no-daemon
最后再代码:https://github.com/harmonyos-dev/harmonyos-github-action-example
或许您还需要下面的文章:
围观我的Github Idea墙, 也许,你会遇到心仪的项目
QQ技术交流群: 321689806Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK