Skip to content

Commit a7a3b04

Browse files
Create mirror_github_gitlabird.yml
1 parent 3f1cfe6 commit a7a3b04

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
name: GitHub to GitLab IRD mirror with release assets
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
tags:
8+
- '**'
9+
pull_request:
10+
branches:
11+
- '**'
12+
delete:
13+
branches:
14+
- '**'
15+
tags:
16+
- '**'
17+
release:
18+
types:
19+
- created
20+
- published
21+
- edited
22+
- deleted
23+
24+
jobs:
25+
mirror:
26+
runs-on: ubuntu-latest
27+
28+
steps:
29+
- name: Clone repository as bare
30+
run: |
31+
git clone --bare https://github.com/umr-marbec/den_pages.git my-github-repository.git
32+
33+
- name: Set up Git
34+
run: |
35+
git config --global user.name "GitHub Actions"
36+
git config --global user.email "github-actions@users.noreply.github.com"
37+
38+
- name: Add forge remote
39+
run: |
40+
cd my-github-repository.git
41+
git remote add mirror https://oauth2:${{ secrets.TOKEN_GITLAB_IRD_DEN_PAGES_BACKUP }}@forge.ird.fr/marbec/den/den_pages_backup.git
42+
43+
- name: Push to forge
44+
run: |
45+
cd my-github-repository.git
46+
git push --mirror mirror
47+
48+
download-release-assets:
49+
runs-on: ubuntu-latest
50+
needs: mirror
51+
52+
steps:
53+
- name: Set up Git (Authentication)
54+
run: |
55+
git config --global user.name "GitHub Actions"
56+
git config --global user.email "github-actions@users.noreply.github.com"
57+
58+
- name: Fetch release(s) from GitHub
59+
id: fetch_releases
60+
run: |
61+
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
62+
"https://api.github.com/repos/umr-marbec/den_pages/releases")
63+
64+
RELEASE_IDS_NAMES=$(echo "$RESPONSE" | jq -r '.[] | "\(.id) \(.name)"')
65+
66+
if [ -z "$RELEASE_IDS_NAMES" ]; then
67+
echo "No release found. No action required."
68+
echo "SKIP_NEXT_STEP=true" >> $GITHUB_ENV
69+
exit 0
70+
fi
71+
72+
NUM_RELEASES=$(echo "$RELEASE_IDS_NAMES" | wc -l)
73+
echo "Number of releases found: $NUM_RELEASES"
74+
echo "NUM_RELEASES=$NUM_RELEASES" >> $GITHUB_ENV
75+
76+
RELEASE_IDS=""
77+
RELEASE_NAMES=""
78+
79+
while IFS= read -r line; do
80+
RELEASE_ID=$(echo "$line" | awk '{print $1}')
81+
RELEASE_NAME=$(echo "$line" | awk '{print $2}')
82+
RELEASE_IDS="$RELEASE_IDS$RELEASE_ID,"
83+
RELEASE_NAMES="$RELEASE_NAMES$RELEASE_NAME,"
84+
done <<< "$RELEASE_IDS_NAMES"
85+
86+
RELEASE_IDS=${RELEASE_IDS%,}
87+
RELEASE_NAMES=${RELEASE_NAMES%,}
88+
89+
echo "RELEASE_IDS=$RELEASE_IDS" >> $GITHUB_ENV
90+
echo "RELEASE_NAMES=$RELEASE_NAMES" >> $GITHUB_ENV
91+
92+
- name: Download release(s) asset(s) from GitHub
93+
id: download_assets
94+
if: ${{ env.SKIP_NEXT_STEP != 'true' }}
95+
run: |
96+
ASSETS_FOUND=false
97+
NUM_RELEASES=${{ env.NUM_RELEASES }}
98+
RELEASE_IDS=${{ env.RELEASE_IDS }}
99+
RELEASE_NAMES=${{ env.RELEASE_NAMES }}
100+
IFS=',' read -ra RELEASE_IDS_ARRAY <<< "$RELEASE_IDS"
101+
IFS=',' read -ra RELEASE_NAMES_ARRAY <<< "$RELEASE_NAMES"
102+
for num_release in $(seq 0 $((NUM_RELEASES - 1))); do
103+
RELEASE_ID="${RELEASE_IDS_ARRAY[$num_release]}"
104+
RELEASE_NAME="${RELEASE_NAMES_ARRAY[$num_release]}"
105+
echo "Processing release ID: $RELEASE_ID with Name: $RELEASE_NAME"
106+
ASSETS=$(curl -s \
107+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
108+
"https://api.github.com/repos/umr-marbec/den_pages/releases/$RELEASE_ID/assets" \
109+
| jq -r '.[].browser_download_url')
110+
if [ -z "$ASSETS" ]; then
111+
echo "No assets found for release $RELEASE_ID ($RELEASE_NAME). Skipping download step."
112+
continue
113+
else
114+
ASSETS_FOUND=true
115+
mkdir -p "release-assets/$RELEASE_ID"_"$RELEASE_NAME"
116+
cd "release-assets/$RELEASE_ID"_"$RELEASE_NAME"
117+
118+
for URL in $ASSETS; do
119+
echo "Downloading $URL"
120+
curl -L -o "$(basename "$URL")" -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$URL"
121+
done
122+
123+
cd -
124+
fi
125+
done
126+
if [ "$ASSETS_FOUND" = false ]; then
127+
echo "No assets found for any release. Exiting."
128+
echo "SKIP_NEXT_STEP=true" >> $GITHUB_ENV
129+
exit 0
130+
fi
131+
132+
- name: Push asset(s) to mirror repository
133+
id: push_mirror
134+
if: ${{ env.SKIP_NEXT_STEP != 'true' }}
135+
run: |
136+
git clone https://oauth2:${{ secrets.TOKEN_GITLAB_IRD_DEN_PAGES_BACKUP }}@forge.ird.fr/marbec/den/den_pages_backup.git
137+
cd test_miroir_github
138+
139+
if [ -d "release-assets" ]; then
140+
echo "Removing existing release-assets directory from the mirror repository."
141+
rm -rf release-assets
142+
fi
143+
144+
echo "Copying local release-assets directory to the mirror repository."
145+
cp -r "../release-assets" .
146+
147+
git add .
148+
git commit -m "Add release assets from GitHub releases"
149+
150+
BRANCH_NAME=$(git symbolic-ref --short HEAD)
151+
152+
git push origin "$BRANCH_NAME"

0 commit comments

Comments
 (0)