-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
111 lines (98 loc) · 4.51 KB
/
Copy pathaction.yml
File metadata and controls
111 lines (98 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
name: 'Save actions cache'
description: 'Save actions cache and clean up old files'
branding:
icon: at-sign
color: purple
inputs:
deleteAfter:
description: 'Number of days after which files should be deleted'
required: false
default: "180"
actionsFolder:
description: 'Folder that actions are downloaded to'
required: false
default: "/actions-runner/_work/_actions"
runs:
using: 'composite'
steps:
- shell: bash
if: runner.os != 'Windows'
run: |
#for each tar.gz in ${{ inputs.actionsFolder }}, see if it is in $ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE
#if not, copy it over
for dir in ${{ inputs.actionsFolder }}/*/*/*; do
if [ -d "$dir" ]; then
owner=$(basename "$(dirname "$(dirname "$dir")")")
repo=$(basename "$(dirname "$dir")")
ref=$(basename "$dir")
# Construct the destination directory and file name:
# Destination directory: $ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE/owner_repo
# Destination file: ref.tar.gz within that directory
dest_dir="$ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE/${owner}_${repo}"
dest_file="$dest_dir/${ref}.tar.gz"
# Check if the tarball file already exists
if [ ! -f "$dest_file" ]; then
echo "Tarball for $owner/$repo ($ref) not found. Downloading..."
# Create the destination directory if it does not exist
mkdir -p "$dest_dir"
# Download the tarball from GitHub API
curl -sL "https://api.github.com/repos/${owner}/${repo}/tarball/${ref}" -o "$dest_file"
if [ $? -eq 0 ]; then
echo "Downloaded tarball to $dest_file"
else
echo "Failed to download tarball for $owner/$repo ($ref)" >&2
fi
else
echo "Tarball $dest_file already exists. Skipping download."
fi
fi
done
find "$ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE" -type f -mtime +${{ inputs.deleteAfter }} -exec rm -f {} \;
- shell: pwsh
if: runner.os == 'Windows'
run: |
$actionsFolder = "${{ inputs.actionsFolder }}"
$deleteAfter = ${{ inputs.deleteAfter }}
$cacheFolder = $env:ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE
# Ensure actionsFolder is in Windows format and prepend C: if necessary
if ($actionsFolder -notmatch "^[A-Za-z]:\\") {
if ($actionsFolder.StartsWith("/")) {
# If it starts with a slash, treat it as a relative path and prepend C:\
$actionsFolder = "C:" + $actionsFolder
}
}
$actionsFolder = $actionsFolder -replace '/', '\\'
# Search for files matching the pattern "actionsFolder\owner\repo\ref.completed"
Get-ChildItem -Path "$actionsFolder\*\*\*.completed" -File | ForEach-Object {
$file = $_
# Split the full path and take the last 3 parts (owner, repo, ref)
$parts = $file.FullName -split '\\'
# Extract owner, repo, and ref (last three parts of the path)
$owner = $parts[-3]
$repo = $parts[-2]
$ref = $file.BaseName # Filename without .completed extension
# Define the destination directory and file path
$destDir = Join-Path -Path $cacheFolder -ChildPath "${owner}_${repo}"
$destFile = Join-Path -Path $destDir -ChildPath "${ref}.zip"
# Ensure the directory exists
if (-Not (Test-Path -Path $destDir)) {
New-Item -ItemType Directory -Force -Path $destDir | Out-Null
}
# If the zip file doesn't exist, download it
if (-Not (Test-Path -Path $destFile)) {
Write-Host "Zip file for $owner/$repo ($ref) not found. Downloading..."
$url = "https://api.github.com/repos/$owner/$repo/zipball/$ref"
Invoke-WebRequest -Uri $url -OutFile $destFile
if ($?) {
Write-Host "Downloaded zip file to $destFile"
} else {
Write-Error "Failed to download zip file for $owner/$repo ($ref)"
}
} else {
Write-Host "Zip file $destFile already exists. Skipping download."
}
}
# Cleanup older files
Get-ChildItem -Path $cacheFolder -Recurse -File | Where-Object {
$_.LastWriteTime -lt (Get-Date).AddDays(-$deleteAfter)
} | Remove-Item -Force