blob: d32d0e6b9a637e36016164f1f15fb331f679f8de [file] [edit]
# Copyright (C) 2025 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: run install-build-deps (with caching)
description: Restore and conditionally save the buildtools cache
inputs:
install-flags:
required: false
description: Optional flags to pass to install-build-deps (e.g., --android)
runs:
using: "composite"
steps:
- name: Compute cache key
id: cache_key
env:
INSTALL_FLAGS: ${{ inputs.install-flags }}
shell: bash
run: |
# Create buildtools cache key.
flags="${INSTALL_FLAGS:-default}"
echo "key=buildtools-$flags" >> "$GITHUB_OUTPUT"
# When restoring the buildtools cache, we don't want to overwrite files or
# folders that are stored in git.
# We also use this file to prevent writing such files back to cache at the
# "Save buildtools cache" step.
- name: Generate list of files to ignore
id: files_to_ignore
shell: bash
run: |
# Generate list of files in 'buildtools/' stored in git.
FILES_TO_IGNORE_PATH=$(mktemp /tmp/files-to-ignore-XXXXX.txt)
git ls-tree HEAD:buildtools --name-only > "$FILES_TO_IGNORE_PATH"
echo "file_path=$FILES_TO_IGNORE_PATH" >> "$GITHUB_OUTPUT"
- name: Save local buildtools hash
id: local_buildtools_hash
env:
BUILDTOOLS_HASH: ${{ hashFiles('buildtools/**', 'tools/install-build-deps') }}
shell: bash
run: echo "hash=$BUILDTOOLS_HASH" >> "$GITHUB_OUTPUT"
- name: Restore buildtools from cache
id: restore_buildtools_cache
uses: ./.github/actions/cache-on-google-cloud-storage/restore
with:
cache_key: ${{ steps.cache_key.outputs.key }}
directory: buildtools
exclude_files: ${{ steps.files_to_ignore.outputs.file_path }}
- name: Check if need to update buildtools cache
id: check_if_need_update
env:
LOCAL_BUILDTOOLS_HASH: ${{ steps.local_buildtools_hash.outputs.hash }}
CACHED_BUILDTOOLS_HASH_FILE_PATH: buildtools/buildtools_hash_revision_for_cache_on_ci.txt
shell: bash
run: |
# Compare cached buildtools hash with the local (stored in git) hash.
# Content of 'buildtools/' rarely changes, so we don't want to re-upload
# unchanged cache on each build.
# It changes when the 'tools/install-build-deps' or the files added to
# git inside `buildtools/' changes, so we calculate and save the sha256
# hash of that files.
if [ -f "$CACHED_BUILDTOOLS_HASH_FILE_PATH" ]; then
CACHED_BUILDTOOLS_HASH=$(cat "$CACHED_BUILDTOOLS_HASH_FILE_PATH")
else
CACHED_BUILDTOOLS_HASH="0"
fi
if [[ "$CACHED_BUILDTOOLS_HASH" != "$LOCAL_BUILDTOOLS_HASH" ]]; then
# Rewrite the hash file, it is saved as part of 'buildtools/' cache.
echo "$LOCAL_BUILDTOOLS_HASH" > "$CACHED_BUILDTOOLS_HASH_FILE_PATH"
echo "update_cache=true" >> "$GITHUB_OUTPUT"
fi
- name: Run install-build-deps
shell: bash
# 'tools/install-build-deps' script will check the hashes and download the
# correct deps if the restored cache is too old or too new.
run: tools/install-build-deps ${{ inputs.install-flags }}
- name: Save buildtools cache (only on main)
if: |
github.ref == 'refs/heads/main' &&
steps.check_if_need_update.outputs.update_cache == 'true'
uses: ./.github/actions/cache-on-google-cloud-storage/save
with:
cache_key: ${{ steps.cache_key.outputs.key }}
directory: buildtools
exclude_files: ${{ steps.files_to_ignore.outputs.file_path }}