forked from GuillaumeFalourd/clone-github-repo-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
78 lines (72 loc) · 2.63 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
name: "Clone Repositories"
description: "Github Action to clone a public or private Github repository and access its content on others repositories workflows."
inputs:
owner:
description: "Repository Owner"
required: true
useremail:
description: "Github user email"
required: true
root-repo:
description: "Root repository name"
required: true
repositories:
description: "Comma-separated list of repository names"
required: true
access-token:
description: "PAT with repository scope (https://github.com/settings/tokens)"
required: false
depth:
description: "Depth of the clone (default: full history)"
required: false
default: ""
branch:
description: "Branch name (default: main)"
required: false
default: "main"
submodule:
description: "Clone with submodules"
required: false
default: "false"
runs:
using: "composite"
steps:
- name: Set Git user
run: |
git config --global user.email ${{ inputs.useremail }}
git config --global user.name ${{ inputs.owner }}
shell: bash
- name: Clone root repository
run: |
git clone https://${{ inputs.access-token }}@github.com/${{ inputs.owner }}/${{ inputs.root-repo }}.git
cd ${{ inputs.root-repo }}
ls -la
shell: bash
- name: Clone or create and sync repositories
run: |
IFS=',' read -ra REPO_ARRAY <<< "${{ inputs.repositories }}"
for REPO in "${REPO_ARRAY[@]}"; do
STATUS_CODE=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token ${{ inputs.access-token }}" https://api.github.com/repos/${{ inputs.owner }}/$REPO)
if [ "$STATUS_CODE" = "404" ]; then
curl -X POST -H "Authorization: token ${{ inputs.access-token }}" \
-d "{\"name\":\"$REPO\", \"private\": true}" \
https://api.github.com/user/repos
fi
git clone https://${{ inputs.access-token }}@github.com/${{ inputs.owner }}/$REPO.git
cd $REPO
git checkout "${{ inputs.branch }}" 2>/dev/null || git checkout -b "${{ inputs.branch }}"
rm -rf *
cp -r ../${{ inputs.root-repo }}/* .
git add .
git commit -m "Sync with root repository"
git push origin "${{ inputs.branch }}"
cd ..
if [ -d "$REPO" ]; then
echo "Cloned and synced $REPO repository successfully."
echo "Access the repository content using \"cd $REPO.\""
else
echo "Error: Couldn't clone or sync $REPO repository. Check the inputs or the PAT scope."
exit 1
fi
done
shell: bash