-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
116 lines (99 loc) · 4.31 KB
/
action.yml
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
112
113
114
115
116
name: "Smart Checkout Action"
description: "Perform an optimized checkout a repository. In case of checkout-type=branch, it will calculate and fetch the minimum commits for head and base refs to ensure the merge base commit is included. In case of checkout-type=commit, it will checkout the specific commit SHA on the main branch, guaranteeing HEAD is pointing to the commit."
inputs:
checkout-type:
description: 'The type of the checkout. Either "branch" or "commit"'
required: true
default: "branch"
base-branch:
description: "The base branch name. Required if checkout-type=branch"
required: true
default: "main"
head-branch:
description: "The head branch name. Required if checkout-type=branch"
required: true
default: ${{ github.head_ref || github.ref_name }}
commit:
description: "The commit SHA to checkout. Required if checkout-type=commit"
required: false
default: ${{ github.sha }}
main-branch:
description: "The main branch name. Required if checkout-type=commit"
required: false
default: "main"
lfs:
description: "Whether to use LFS caching"
required: true
default: "false"
token:
description: "The PAT GitHub token. Required to run authenticated git commands in other steps"
required: false
default: ${{ github.token }}
runs:
using: "composite"
steps:
- name: "[checkout-type=commit] Calculate the fetch depth for the head and base branches"
if: ${{ inputs.checkout-type == 'branch' }}
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
BASE_BRANCH_NAME: ${{ inputs.base-branch }}
HEAD_BRANCH_NAME: ${{ inputs.head-branch }}
GH_TOKEN: ${{ github.token }}
run: |
compare_output=$(gh api repos/${GITHUB_REPOSITORY}/compare/${BASE_BRANCH_NAME}...${HEAD_BRANCH_NAME} --jq '{ahead_by: .ahead_by, behind_by: .behind_by}')
# The number of commits the head branch is behind the base branch.
base_depth=$(echo $compare_output | jq -r '. | .behind_by + 1')
# The number of commits the head branch is ahead of the base branch.
head_depth=$(echo $compare_output | jq -r '. | .ahead_by + 1')
echo "BASE_FETCH_DEPTH=${base_depth}" >> "$GITHUB_ENV"
echo "HEAD_FETCH_DEPTH=${head_depth}" >> "$GITHUB_ENV"
- name: "[checkout-type=branch] Checkout the head branch without LFS caching"
if: ${{ inputs.checkout-type == 'branch' && inputs.lfs == 'false' }}
uses: actions/[email protected]
with:
ref: ${{ inputs.head-branch }}
fetch-depth: ${{ env.HEAD_FETCH_DEPTH }}
lfs: false
token: ${{ inputs.token }}
- name: "[checkout-type=branch] Checkout the head branch with LFS caching"
if: ${{ inputs.checkout-type == 'branch' && inputs.lfs == 'true' }}
uses: nschloe/[email protected]
with:
ref: ${{ inputs.head-branch }}
fetch-depth: ${{ env.HEAD_FETCH_DEPTH }}
token: ${{ inputs.token }}
- name: "[checkout-type=commit] Checkout the main branch without LFS caching"
if: ${{ inputs.checkout-type == 'commit' && inputs.lfs == 'false' }}
uses: actions/[email protected]
with:
ref: ${{ inputs.main-branch }}
fetch-depth: 0
lfs: false
token: ${{ inputs.token }}
- name: "[checkout-type=commit] Checkout the main branch with LFS caching"
if: ${{ inputs.checkout-type == 'commit' && inputs.lfs == 'true' }}
uses: nschloe/[email protected]
with:
ref: ${{ inputs.main-branch }}
fetch-depth: 0
token: ${{ inputs.token }}
- name: "[checkout-type=branch] Fetch the base branch"
if: ${{ inputs.checkout-type == 'branch'}}
shell: bash
env:
BASE_BRANCH_NAME: ${{ inputs.base-branch }}
BASE_FETCH_DEPTH: ${{ env.BASE_FETCH_DEPTH }}
run: git fetch origin ${BASE_BRANCH_NAME} --depth=${BASE_FETCH_DEPTH}
- name: "[checkout-type=commit] Reset the head to the commit SHA"
if: ${{ inputs.checkout-type == 'commit'}}
shell: bash
env:
COMMIT_SHA: ${{ inputs.commit }}
run: git reset --hard ${COMMIT_SHA}
- name: "List fetched branches"
shell: bash
run: git branch -r
- name: "List fetched commits"
shell: bash
run: git log --oneline --decorate --graph --all