|
| 1 | +name: "delete" |
| 2 | +description: "Delete a file or directory from the orphaned 'gh-cache' branch" |
| 3 | + |
| 4 | +inputs: |
| 5 | + path: |
| 6 | + description: "Relative path to a file or directory to delete" |
| 7 | + required: true |
| 8 | + token: |
| 9 | + description: "Token with fine-grained permissions 'contents: write'" |
| 10 | + required: true |
| 11 | + |
| 12 | +runs: |
| 13 | + using: "composite" |
| 14 | + steps: |
| 15 | + - name: Validate path |
| 16 | + shell: bash |
| 17 | + run: | |
| 18 | + # Check for empty |
| 19 | + if [[ -z "${{ inputs.path }}" ]]; then |
| 20 | + echo "Invalid 'path' input (empty)" |
| 21 | + exit 1 |
| 22 | + fi |
| 23 | + # Check for absolute paths |
| 24 | + if [[ "${{ inputs.path }}" == /* ]]; then |
| 25 | + echo "Invalid 'path' input (absolute path): ${{ inputs.path }}" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | + # Check for directory traversal |
| 29 | + if [[ |
| 30 | + "${{ inputs.path }}" == "~"* || |
| 31 | + "${{ inputs.path }}" =~ (^|/)\.\.(/|$) |
| 32 | + ]]; then |
| 33 | + echo "Invalid 'path' input (directory traversal): ${{ inputs.path }}" |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + # Check for disallowed characters (to ensure portability) |
| 37 | + if [[ ! "${{ inputs.path }}" =~ ^[A-Za-z0-9._/-]+$ ]]; then |
| 38 | + echo "Invalid 'path' input (disallowed characters): ${{ inputs.path }}" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +
|
| 42 | + - name: Checkout repository to temporary directory |
| 43 | + uses: actions/checkout@v5 |
| 44 | + with: |
| 45 | + token: ${{ inputs.token }} |
| 46 | + fetch-depth: 0 |
| 47 | + path: .gh-cache-${{ github.run_id }} |
| 48 | + show-progress: 'false' |
| 49 | + |
| 50 | + - name: Switch to gh-cache branch (or create orphan) |
| 51 | + shell: bash |
| 52 | + working-directory: .gh-cache-${{ github.run_id }} |
| 53 | + run: | |
| 54 | + if git ls-remote --exit-code --heads origin gh-cache >/dev/null; then |
| 55 | + git checkout gh-cache >/dev/null 2>&1 |
| 56 | + echo "Checked out existing 'gh-cache' branch" |
| 57 | + else |
| 58 | + git checkout --orphan gh-cache >/dev/null 2>&1 |
| 59 | + git rm -rfq . # Clear files from the initial checkout, to avoid adding them to the orphaned branch |
| 60 | + echo "Created new orphaned 'gh-cache' branch" |
| 61 | + fi |
| 62 | +
|
| 63 | + - name: Copy file to repo |
| 64 | + shell: bash |
| 65 | + run: | |
| 66 | + if [ -f "${{ inputs.path }}" ]; then |
| 67 | + rm -Rf "${{ inputs.path }}" |
| 68 | + rm -Rf ".gh-cache-${{ github.run_id }}/${{ inputs.path }}" |
| 69 | + echo "Deleted '${{ inputs.path }}' from 'gh-cache' branch" |
| 70 | + fi |
| 71 | +
|
| 72 | + - name: Commit and push |
| 73 | + shell: bash |
| 74 | + working-directory: .gh-cache-${{ github.run_id }} |
| 75 | + run: | |
| 76 | + if [ -f "${{ inputs.path }}" ]; then |
| 77 | + git add "${{ inputs.path }}" |
| 78 | + git config user.name "github-actions[bot]" |
| 79 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 80 | + git commit -m "$(printf 'Delete artifact: %s' "${{ inputs.path }}")" \ |
| 81 | + && echo "Committed '${{ inputs.path }}' to 'gh-cache' branch" \ |
| 82 | + || echo "No changes to commit" |
| 83 | + git push origin gh-cache |
| 84 | + else |
| 85 | + echo "'${{ inputs.path }}' does not exist" |
| 86 | + echo "Skipping commit and push" |
| 87 | + fi |
| 88 | +
|
| 89 | + - name: Clean up temporary directory |
| 90 | + shell: bash |
| 91 | + run: | |
| 92 | + rm -rf .gh-cache-${{ github.run_id }} |
| 93 | +
|
| 94 | +branding: |
| 95 | + icon: "trash" |
| 96 | + color: "red" |
0 commit comments