|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +--- |
| 19 | +# apache/infrastructure-actions/stash/restore on GitHub-hosted runners. On RunsOn (the only |
| 20 | +# self-hosted runners) multi-GB image tarballs stay in the RunsOn stack's in-region S3 cache |
| 21 | +# bucket instead of leaving AWS as GitHub artifacts. |
| 22 | +# |
| 23 | +# Every RunsOn job can write anywhere in that bucket, so the bucket is not trusted: objects are |
| 24 | +# named by their sha256, and the digest itself travels as a small ASF stash artifact, which |
| 25 | +# GitHub scopes to the run / branch exactly like a regular stash. A blob that does not match the |
| 26 | +# digest the ASF restore found for this run, branch and repository is never extracted. The bucket's |
| 27 | +# lifecycle rule (S3CacheExpirationInDays) must outlive the longest retention-days of a digest |
| 28 | +# (6 days today), or a digest hit finds no blob. |
| 29 | +name: 'Restore stash' |
| 30 | +description: 'Restore a stash from GitHub artifacts, or from S3 on RunsOn runners.' |
| 31 | +inputs: |
| 32 | + key: |
| 33 | + description: 'Stash key.' |
| 34 | + required: true |
| 35 | + path: |
| 36 | + description: > |
| 37 | + Directory to restore into (GitHub-hosted only; the S3 tarball restores entries to the |
| 38 | + paths they were saved from, and the restore then fails if any of them is outside this directory). |
| 39 | + required: false |
| 40 | + default: ${{ github.workspace }} |
| 41 | + only-current-branch: |
| 42 | + description: 'Do not fall back to the base branch stash.' |
| 43 | + required: false |
| 44 | + default: 'false' |
| 45 | + fail-on-download: |
| 46 | + description: 'Fail when a stash was found but could not be restored.' |
| 47 | + required: false |
| 48 | + default: 'false' |
| 49 | +outputs: |
| 50 | + stash-hit: |
| 51 | + description: "'true' if a stash was restored, 'false' otherwise." |
| 52 | + value: ${{ steps.s3.outputs.stash-hit || steps.artifact.outputs.stash-hit }} |
| 53 | +runs: |
| 54 | + using: 'composite' |
| 55 | + steps: |
| 56 | + - name: "Restore stash from GitHub artifacts" |
| 57 | + id: artifact |
| 58 | + if: runner.environment != 'self-hosted' |
| 59 | + # yamllint disable-line rule:line-length |
| 60 | + uses: apache/infrastructure-actions/stash/restore@c968777141a2afc98d1a09c9f6979455bfebaae3 # restore/v1.0.1 |
| 61 | + with: |
| 62 | + key: ${{ inputs.key }} |
| 63 | + path: ${{ inputs.path }} |
| 64 | + only-current-branch: ${{ inputs.only-current-branch }} |
| 65 | + fail-on-download: ${{ inputs.fail-on-download }} |
| 66 | + - name: "Restore stash digest from GitHub artifacts" |
| 67 | + id: digest |
| 68 | + if: runner.environment == 'self-hosted' |
| 69 | + # yamllint disable-line rule:line-length |
| 70 | + uses: apache/infrastructure-actions/stash/restore@c968777141a2afc98d1a09c9f6979455bfebaae3 # restore/v1.0.1 |
| 71 | + with: |
| 72 | + key: ${{ inputs.key }}-s3 |
| 73 | + path: ${{ runner.temp }}/stash-digest-restore/${{ inputs.key }} |
| 74 | + only-current-branch: ${{ inputs.only-current-branch }} |
| 75 | + fail-on-download: ${{ inputs.fail-on-download }} |
| 76 | + - name: "Restore stash from S3" |
| 77 | + id: s3 |
| 78 | + if: runner.environment == 'self-hosted' |
| 79 | + shell: bash |
| 80 | + env: |
| 81 | + STASH_KEY: ${{ inputs.key }} |
| 82 | + STASH_PATH: ${{ inputs.path }} |
| 83 | + DIGEST_HIT: ${{ steps.digest.outputs.stash-hit }} |
| 84 | + DIGEST_FILE: ${{ runner.temp }}/stash-digest-restore/${{ inputs.key }}/sha256 |
| 85 | + FAIL_ON_DOWNLOAD: ${{ inputs.fail-on-download }} |
| 86 | + run: | |
| 87 | + set -uo pipefail |
| 88 | + payload="${RUNNER_TEMP}/stash-restore-$$.tar" |
| 89 | + entries="${RUNNER_TEMP}/stash-restore-$$.entries" |
| 90 | + miss() { |
| 91 | + rm -f "${payload}" "${entries}" |
| 92 | + echo "stash-hit=false" >> "${GITHUB_OUTPUT}" |
| 93 | + if [[ "${FAIL_ON_DOWNLOAD}" == "true" && "${DIGEST_HIT}" == "true" ]]; then |
| 94 | + echo "::error::$1" |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + echo "$1" |
| 98 | + exit 0 |
| 99 | + } |
| 100 | + if [[ "${DIGEST_HIT}" != "true" ]]; then |
| 101 | + miss "No stash '${STASH_KEY}'" |
| 102 | + fi |
| 103 | + digest="$(cat "${DIGEST_FILE}")" |
| 104 | + if [[ ! "${digest}" =~ ^[0-9a-f]{64}$ ]]; then |
| 105 | + miss "Malformed digest for stash '${STASH_KEY}'" |
| 106 | + fi |
| 107 | + bucket="${RUNS_ON_S3_BUCKET_CACHE:?not running on a RunsOn runner}" |
| 108 | + export AWS_DEFAULT_REGION="${RUNS_ON_AWS_REGION:?not running on a RunsOn runner}" |
| 109 | + object="s3://${bucket}/cache/airflow-stash/sha256/${digest}.tar" |
| 110 | + if ! aws s3 cp --only-show-errors "${object}" "${payload}"; then |
| 111 | + miss "Could not download ${object} for stash '${STASH_KEY}'" |
| 112 | + fi |
| 113 | + if ! echo "${digest} ${payload}" | sha256sum --check --status; then |
| 114 | + miss "${object} does not match the digest recorded for stash '${STASH_KEY}'" |
| 115 | + fi |
| 116 | + # GNU tar detects the compression stash-save chose. |
| 117 | + if ! tar -xPvf "${payload}" > "${entries}"; then |
| 118 | + miss "Could not extract ${object} for stash '${STASH_KEY}'" |
| 119 | + fi |
| 120 | + rm -f "${payload}" |
| 121 | + # The S3 tarball ignores `path`: fail loudly if a caller saves and restores different |
| 122 | + # directories, which would work on GitHub-hosted runners only. |
| 123 | + restore_dir="$(realpath -ms "${STASH_PATH}")" |
| 124 | + restore_dir="${restore_dir%/}" |
| 125 | + outside=() |
| 126 | + while IFS= read -r entry; do |
| 127 | + [[ "${entry}" == /* ]] || entry="${GITHUB_WORKSPACE}/${entry}" |
| 128 | + [[ "${entry}" == "${restore_dir}/"* ]] || outside+=("${entry}") |
| 129 | + done < "${entries}" |
| 130 | + rm -f "${entries}" |
| 131 | + if [[ ${#outside[@]} -gt 0 ]]; then |
| 132 | + printf '%s\n' "${outside[@]}" |
| 133 | + echo "::error::Stash '${STASH_KEY}' restored the entries above outside of ${restore_dir}/" |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | + echo "stash-hit=true" >> "${GITHUB_OUTPUT}" |
| 137 | + echo "Restored stash '${STASH_KEY}' from ${object}" |
0 commit comments