From 5fa8ed5187abcd331822e0a02ea89e99b9cffa34 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Tue, 8 Oct 2024 11:12:55 +0200 Subject: [PATCH] Fix Setting build outputs on cache hits Also adds a test, which simulates behavior on cache-hits. Signed-off-by: Jonathan Schwender --- .github/workflows/workflow.yml | 51 ++++++++- action.yml | 3 +- install_ohos_sdk.sh | 195 ++++++++++++++++++--------------- 3 files changed, 158 insertions(+), 91 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 205a66f..586c739 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -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: ./ @@ -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: ./ @@ -65,7 +87,28 @@ 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 + "${SDK_PATH}/*/${API_VERSION}/native/llvm/bin/clang" --version + "${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 + env: + SDK_PATH: "${{ steps.on_cache_hit_with_fixup.outputs.sdk-path }}" + API_VERSION: "${{ steps.on_cache_hit_with_fixup.outputs.api-version }}" + run: | + "${SDK_PATH}/*/${API_VERSION}/native/llvm/bin/clang" --version + "${OHOS_SDK_NATIVE}/llvm/bin/clang" --version + ${{steps.on_cache_hit_with_fixup.outputs.ohos_sdk_native}}/llvm/bin/clang --version build_result: diff --git a/action.yml b/action.yml index b58b5b3..8613bf3 100644 --- a/action.yml +++ b/action.yml @@ -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 }}" diff --git a/install_ohos_sdk.sh b/install_ohos_sdk.sh index 510c9ee..2a71e8e 100755 --- a/install_ohos_sdk.sh +++ b/install_ohos_sdk.sh @@ -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 )"