-
-
Notifications
You must be signed in to change notification settings - Fork 0
81 lines (65 loc) · 2.42 KB
/
Backup to Gitlab.yml
File metadata and controls
81 lines (65 loc) · 2.42 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
name: Backup to GitLab
on:
push:
branches: [main]
workflow_dispatch:
jobs:
backup:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Compute repo names
run: |
REPO_DISPLAY_NAME="${GITHUB_REPOSITORY#*/}"
REPO_PATH=$(echo "$REPO_DISPLAY_NAME" | tr '[:upper:]' '[:lower:]')
echo "REPO_DISPLAY_NAME=$REPO_DISPLAY_NAME" >> $GITHUB_ENV
echo "REPO_PATH=$REPO_PATH" >> $GITHUB_ENV
- name: Fetch GitLab token from KeyManagement
run: |
TS=$(date +%s%3N)
TOKEN=$(curl -s -X POST https://keymanagement.joeljollyhere.workers.dev \
-H "Content-Type: application/json" \
-d "{
\"accessKey\": \"${{ secrets.ACCESS_KEY }}\",
\"key\": \"gitlab\",
\"ts\": $TS
}")
if [ -z "$TOKEN" ]; then
echo "Failed to fetch GitLab token"
exit 1
fi
echo "GITLAB_TOKEN=$TOKEN" >> $GITHUB_ENV
- name: Ensure GitLab repo exists
run: |
PROJECT_PATH="withinjoel/${REPO_PATH}"
ENCODED_PATH=$(echo "$PROJECT_PATH" | sed 's/\//%2F/g')
STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
-H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
"https://gitlab.com/api/v4/projects/${ENCODED_PATH}")
if [ "$STATUS" = "404" ]; then
echo "GitLab repo does not exist. Creating..."
USER_ID=$(curl -s \
-H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
https://gitlab.com/api/v4/user | jq '.id')
curl -s -X POST https://gitlab.com/api/v4/projects \
-H "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"${REPO_DISPLAY_NAME}\",
\"path\": \"${REPO_PATH}\",
\"namespace_id\": ${USER_ID},
\"visibility\": \"private\"
}"
else
echo "GitLab repo already exists."
fi
- name: Push to GitLab
run: |
git config --global user.name "GitHub Backup Bot"
git config --global user.email "bot@example.com"
git remote add gitlab \
https://oauth2:${GITLAB_TOKEN}@gitlab.com/withinjoel/${REPO_PATH}.git
git push gitlab main --force