Skip to content

Commit

Permalink
Fix Setting build outputs on cache hits
Browse files Browse the repository at this point in the history
Also adds a test, which simulates behavior on cache-hits.
Signed-off-by: Jonathan Schwender <[email protected]>
  • Loading branch information
jschwe committed Oct 8, 2024
1 parent 0ee4782 commit cf99cdf
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 91 deletions.
52 changes: 48 additions & 4 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,33 @@ jobs:
with:
cache: false
version: ${{ matrix.version }}
fixup-path: 'false'
- name: Test
run: bash -c '"${OHOS_SDK_NATIVE}/llvm/bin/clang" --version'
shell: bash
run: |
"${OHOS_SDK_NATIVE}/llvm/bin/clang" --version
${{steps.initial-setup.outputs.ohos_sdk_native}}/llvm/bin/clang --version
- name: Simulate action on cache-hit
run: ./install_ohos_sdk.sh
shell: bash
env:
INPUT_VERSION: "${{ matrix.version }}"
INPUT_COMPONENTS: "all"
INPUT_FIXUP_PATH: "false"
INPUT_MIRROR: "true"
INPUT_CACHE: "true"
INPUT_WAS_CACHED: "true"
GH_TOKEN: "${{ github.token }}"
- name: Test
shell: bash
run: |
"${OHOS_SDK_NATIVE}/llvm/bin/clang" --version
${{steps.initial-setup.outputs.ohos_sdk_native}}/llvm/bin/clang --version
- name: Remove previous SDK installation
shell: bash
env:
SDK_PATH: "${{ steps.initial-setup.outputs.sdk-path }}"
run: bash -c 'rm -rf "${SDK_PATH}"'
run: rm -rf "${SDK_PATH}"
- name: Setup SDK with Components
id: sdk-with-components
uses: ./
Expand All @@ -49,9 +70,10 @@ jobs:
exit 1
fi
- name: Remove previous SDK installation
shell: bash
env:
SDK_PATH: "${{ steps.sdk-with-components.outputs.sdk-path }}"
run: bash -c 'rm -rf "${SDK_PATH}"'
run: rm -rf "${SDK_PATH}"
- name: Setup SDK and fixup the path
id: sdk-with-fixup
uses: ./
Expand All @@ -65,7 +87,29 @@ jobs:
SDK_PATH: "${{ steps.sdk-with-fixup.outputs.sdk-path }}"
API_VERSION: "${{ steps.sdk-with-fixup.outputs.api-version }}"
run: |
${SDK_PATH}/*/${API_VERSION}/native/llvm/bin/clang --version
cd "${SDK_PATH}/*/${API_VERSION}/native/llvm/bin" && clang --version && cd -
"${OHOS_SDK_NATIVE}/llvm/bin/clang" --version
- name: Simulate action on cache-hit
id: on_cache_hit_with_fixup
run: ./install_ohos_sdk.sh
shell: bash
env:
INPUT_VERSION: "${{ matrix.version }}"
INPUT_COMPONENTS: "all"
INPUT_FIXUP_PATH: "true"
INPUT_MIRROR: "true"
INPUT_CACHE: "true"
INPUT_WAS_CACHED: "true"
GH_TOKEN: "${{ github.token }}"
- name: Test
shell: bash
env:
SDK_PATH: "${{ steps.on_cache_hit_with_fixup.outputs.sdk-path }}"
API_VERSION: "${{ steps.on_cache_hit_with_fixup.outputs.api-version }}"
run: |
cd "${SDK_PATH}/*/${API_VERSION}/native/llvm/bin" && clang --version && cd -
"${OHOS_SDK_NATIVE}/llvm/bin/clang" --version
${{steps.on_cache_hit_with_fixup.outputs.ohos_sdk_native}}/llvm/bin/clang --version
build_result:
Expand Down
3 changes: 2 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ runs:
id: install_ohos_sdk
run: install_ohos_sdk.sh
shell: bash
if: ${{ inputs.cache != 'true' || steps.cache.outputs.cache-hit != 'true' }}
env:
INPUT_VERSION: "${{ inputs.version }}"
INPUT_COMPONENTS: "${{ inputs.components }}"
INPUT_FIXUP_PATH: ${{ inputs.fixup-path }}
INPUT_MIRROR: "${{ inputs.mirror }}"
INPUT_CACHE: "${{ inputs.cache }}"
INPUT_WAS_CACHED: "${{ steps.cache.outputs.cache-hit }}"
GH_TOKEN: "${{ github.token }}"
195 changes: 109 additions & 86 deletions install_ohos_sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,109 +56,132 @@ function select_version() {
fi
}

MIRROR_DOWNLOAD_SUCCESS=false
if [[ "${INPUT_MIRROR}" == "true" || "${INPUT_MIRROR}" == "force" ]]; then
RESOLVED_MIRROR_VERSION_TAG="v${INPUT_VERSION}"
select_version # This will update RESOLVED_MIRROR_VERSION_TAG.
gh release download "${RESOLVED_MIRROR_VERSION_TAG}" --pattern "${OS_FILENAME}*" --repo openharmony-rs/ohos-sdk && MIRROR_DOWNLOAD_SUCCESS=true
if [[ "${MIRROR_DOWNLOAD_SUCCESS}" == "true" ]]; then
# The mirror may have split the archives due to the Github releases size limits.
# First rename the sha256 file, so we don't glob it.
mv "${OS_FILENAME}.sha256" "sha256.${OS_FILENAME}"
# Now get all the .aa .ab etc. output of the split command for our filename
shopt -s nullglob
split_files=("${OS_FILENAME}".*)
if [ ${#split_files[@]} -ne 0 ]; then
cat "${split_files[@]}" > "${OS_FILENAME}"
rm "${split_files[@]}"
# Assumption: cwd contains the zipped components.
# Outputs: API_VERSION
function extract_sdk_components() {
if [[ "${INPUT_COMPONENTS}" == "all" ]]; then
COMPONENTS=(*.zip)
else
IFS=";" read -ra COMPONENTS <<< "${INPUT_COMPONENTS}"
resolved_components=()
for COMPONENT in "${COMPONENTS[@]}"
do
resolved_components+=("${COMPONENT}"-*.zip)
done
COMPONENTS=(${resolved_components[@]})
fi
# Rename the shafile back again to the original name
mv "sha256.${OS_FILENAME}" "${OS_FILENAME}.sha256"
elif [[ "${INPUT_MIRROR}" == "force" ]]; then
echo "Downloading from mirror failed, and mirror=force. Failing the job."
echo "Note: mirror=force is for internal test purposes, and should not be selected by users."
exit 1
else
echo "Failed to download SDK from mirror. Falling back to downloading from upstream."
fi
fi
if [[ "${MIRROR_DOWNLOAD_SUCCESS}" != "true" ]]; then
DOWNLOAD_URL="${URL_BASE}/${INPUT_VERSION}-Release/${OS_FILENAME}"
echo "Downloading OHOS SDK from ${DOWNLOAD_URL}"
curl --fail -L -O "${DOWNLOAD_URL}"
curl --fail -L -O "${DOWNLOAD_URL}.sha256"
fi

VERSION_MAJOR=${INPUT_VERSION%%.*}
for COMPONENT in "${COMPONENTS[@]}"
do
echo "Extracting component ${COMPONENT}"
echo "::group::Unzipping archive"
#shellcheck disable=SC2144
if [[ -f "${COMPONENT}" ]]; then
unzip "${COMPONENT}"
else
echo "Failed to find component ${COMPONENT}"
ls -la
exit 1
fi
echo "::endgroup::"
# Removing everything after the first dash should give us the component dir
component_dir=${COMPONENT%%-*}
API_VERSION=$(jq -r '.apiVersion' < "${component_dir}/oh-uni-package.json")
if [ "$INPUT_FIXUP_PATH" = "true" ]; then
mkdir -p "${API_VERSION}"
mv "${component_dir}" "${API_VERSION}/"
fi
done
rm ./*.zip
}

if [[ "${OS}" == "mac" ]]; then
echo "$(cat "${OS_FILENAME}".sha256) ${OS_FILENAME}" | shasum -a 256 --check --status
tar -xf "${OS_FILENAME}" --strip-components=3
else
echo "$(cat "${OS_FILENAME}".sha256) ${OS_FILENAME}" | sha256sum --check --status
if (( VERSION_MAJOR >= 5 )); then
tar -xf "${OS_FILENAME}"
function download_and_extract_sdk() {
MIRROR_DOWNLOAD_SUCCESS=false
if [[ "${INPUT_MIRROR}" == "true" || "${INPUT_MIRROR}" == "force" ]]; then
RESOLVED_MIRROR_VERSION_TAG="v${INPUT_VERSION}"
select_version # This will update RESOLVED_MIRROR_VERSION_TAG.
gh release download "${RESOLVED_MIRROR_VERSION_TAG}" --pattern "${OS_FILENAME}*" --repo openharmony-rs/ohos-sdk && MIRROR_DOWNLOAD_SUCCESS=true
if [[ "${MIRROR_DOWNLOAD_SUCCESS}" == "true" ]]; then
# The mirror may have split the archives due to the Github releases size limits.
# First rename the sha256 file, so we don't glob it.
mv "${OS_FILENAME}.sha256" "sha256.${OS_FILENAME}"
# Now get all the .aa .ab etc. output of the split command for our filename
shopt -s nullglob
split_files=("${OS_FILENAME}".*)
if [ ${#split_files[@]} -ne 0 ]; then
cat "${split_files[@]}" > "${OS_FILENAME}"
rm "${split_files[@]}"
fi
# Rename the shafile back again to the original name
mv "sha256.${OS_FILENAME}" "${OS_FILENAME}.sha256"
elif [[ "${INPUT_MIRROR}" == "force" ]]; then
echo "Downloading from mirror failed, and mirror=force. Failing the job."
echo "Note: mirror=force is for internal test purposes, and should not be selected by users."
exit 1
else
echo "Failed to download SDK from mirror. Falling back to downloading from upstream."
fi
fi
if [[ "${MIRROR_DOWNLOAD_SUCCESS}" != "true" ]]; then
DOWNLOAD_URL="${URL_BASE}/${INPUT_VERSION}-Release/${OS_FILENAME}"
echo "Downloading OHOS SDK from ${DOWNLOAD_URL}"
curl --fail -L -O "${DOWNLOAD_URL}"
curl --fail -L -O "${DOWNLOAD_URL}.sha256"
fi

VERSION_MAJOR=${INPUT_VERSION%%.*}

if [[ "${OS}" == "mac" ]]; then
echo "$(cat "${OS_FILENAME}".sha256) ${OS_FILENAME}" | shasum -a 256 --check --status
tar -xf "${OS_FILENAME}" --strip-components=3
else
tar -xf "${OS_FILENAME}" --strip-components=1
echo "$(cat "${OS_FILENAME}".sha256) ${OS_FILENAME}" | sha256sum --check --status
if (( VERSION_MAJOR >= 5 )); then
tar -xf "${OS_FILENAME}"
else
tar -xf "${OS_FILENAME}" --strip-components=1
fi
fi
fi
rm "${OS_FILENAME}" "${OS_FILENAME}.sha256"
echo "sdk-path=$PWD" >> "${GITHUB_OUTPUT}"
rm "${OS_FILENAME}" "${OS_FILENAME}.sha256"

if [[ "${OS}" == "linux" ]]; then
rm -rf windows
cd linux
elif [[ "${OS}" == "windows" ]]; then
rm -rf linux
cd windows
else
cd darwin
fi
if [[ "${OS}" == "linux" ]]; then
rm -rf windows
cd linux
elif [[ "${OS}" == "windows" ]]; then
rm -rf linux
cd windows
else
cd darwin
fi
OHOS_BASE_SDK_HOME="$PWD"
extract_sdk_components
}

OHOS_BASE_SDK_HOME="$PWD"
echo "sdk-path=$PWD" >> "${GITHUB_OUTPUT}"

if [[ "${INPUT_COMPONENTS}" == "all" ]]; then
COMPONENTS=(*.zip)
if [[ "${INPUT_CACHE}" != "true" || "${INPUT_WAS_CACHED}" != "true" ]]; then
download_and_extract_sdk
else
IFS=";" read -ra COMPONENTS <<< "${INPUT_COMPONENTS}"
resolved_components=()
for COMPONENT in "${COMPONENTS[@]}"
do
resolved_components+=("${COMPONENT}"-*.zip)
done
COMPONENTS=(${resolved_components[@]})
fi

for COMPONENT in "${COMPONENTS[@]}"
do
echo "Extracting component ${COMPONENT}"
echo "::group::Unzipping archive"
#shellcheck disable=SC2144
if [[ -f "${COMPONENT}" ]]; then
unzip "${COMPONENT}"
if [[ "${OS}" == "linux" ]]; then
cd linux
elif [[ "${OS}" == "windows" ]]; then
cd windows
else
echo "Failed to find component ${COMPONENT}"
ls -la
exit 1
cd darwin
fi
echo "::endgroup::"
# Removing everything after the first dash should give us the component dir
component_dir=${COMPONENT%%-*}
API_VERSION=$(cat "${component_dir}/oh-uni-package.json" | jq -r '.apiVersion')
if [ "$INPUT_FIXUP_PATH" = "true" ]; then
mkdir -p "${API_VERSION}"
mv "${component_dir}" "${API_VERSION}/"
fi
done
rm ./*.zip
OHOS_BASE_SDK_HOME="$PWD"
fi

if [ "${INPUT_FIXUP_PATH}" = "true" ]; then
OHOS_NDK_HOME="${OHOS_BASE_SDK_HOME}/${API_VERSION}"
OHOS_SDK_NATIVE="${OHOS_BASE_SDK_HOME}/${API_VERSION}/native"
# When we are restoring from cache we don't know the API version, so we glob for now.
# In the future we should do something more robust, like copying `oh-uni-package.json` to the root.
OHOS_NDK_HOME=$(cd "${OHOS_BASE_SDK_HOME}"/* && pwd)
OHOS_SDK_NATIVE="${OHOS_NDK_HOME}"/native
else
OHOS_NDK_HOME="${OHOS_BASE_SDK_HOME}"
OHOS_SDK_NATIVE="${OHOS_BASE_SDK_HOME}/native"
fi

cd "${OHOS_SDK_NATIVE}"
SDK_VERSION="$(jq -r .version < oh-uni-package.json )"
API_VERSION="$(jq -r .apiVersion < oh-uni-package.json )"
Expand Down

0 comments on commit cf99cdf

Please sign in to comment.