-
Notifications
You must be signed in to change notification settings - Fork 5
81 lines (81 loc) · 2.73 KB
/
cache.yaml
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
name: cache
on:
push:
branches: ["main"]
workflow_dispatch:
# tl;dr;
# push will diff with cache, and dispatch workflow.
# workflow_dispatch without any repo change will use cache, and not dispatch workflow.
env:
DISPATCH_REPO: guitarrapc/testtest
DIPATCH_WORKFLOW_NAME: test
DISPATCH_BRANCH: main
CACHE_VERSION: v1
jobs:
detect_library_change:
runs-on: ubuntu-latest
outputs:
cache-hit: ${{ steps.cache-sha.outputs.cache-hit }}
timeout-minutes: 10
steps:
- name: Dump GitHub context
run: echo "$CONTEXT"
env:
CONTEXT: ${{ toJson(github) }}
# get trigger repo references
- name: public repo reference
uses: actions/github-script@v3
id: set-public-result
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const result = await github.git.getRef({
owner: context.repo.owner,
repo: "githubactions-lab",
ref: "heads/main"
})
console.log(result)
return result.data.object.sha
- run: echo "${{ steps.set-public-result.outputs.result }}"
- name: private repo reference
uses: actions/github-script@v3
id: set-private-result
with:
github-token: ${{ secrets.SYNCED_GITHUB_TOKEN_REPO }} # must be PAT
script: |
const result = await github.git.getRef({
owner: context.repo.owner,
repo: "testtest",
ref: "heads/main"
})
console.log(result)
return result.data.object.sha
- run: echo "${{ steps.set-private-result.outputs.result }}"
# write all repo's sha to single file
- name: gen sha hash
run: |
mkdir ./cache/
cat <<EOF > ./cache/sha.txt
${{ steps.set-public-result.outputs.result }}
${{ steps.set-private-result.outputs.result }}
EOF
# cache validation if sha's file has been updated
- name: Cache sha
id: cache-sha
uses: actions/cache@v2
with:
path: ~/cache
key: ${{ runner.os }}-${{ env.CACHE_VERSION }}-sha-${{ hashFiles('**/sha.txt') }}
dispatch:
needs: [detect_library_change]
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && needs.detect_library_change.outputs.cache-hit != 'true'
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
# dispatch build if cache is not match with previous
- uses: benc-uk/workflow-dispatch@v1.1
with:
repo: ${{ env.DISPATCH_REPO }}
ref: ${{ env.DISPATCH_BRANCH }}
workflow: ${{ env.DIPATCH_WORKFLOW_NAME }}
token: ${{ secrets.SYNCED_GITHUB_TOKEN_REPO }}