-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
95 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,110 @@ | ||
name: Sync to GitLab | ||
name: Backup to GitLab | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- master | ||
schedule: | ||
- cron: "0 2 * * *" # Optional: Runs daily at 2 AM UTC | ||
- cron: "0 2 * * *" | ||
|
||
jobs: | ||
sync: | ||
backup: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout the repository | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Configure Git | ||
run: | | ||
git config --global user.name "github-actions[bot]" | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
- name: Add GitLab as remote | ||
run: | | ||
git remote add gitlab "${{ secrets.GITLAB_URL }}/${{ secrets.GITLAB_NAMESPACE }}/$GITHUB_REPOSITORY.git" | ||
- name: Push to GitLab | ||
- name: Setup GitLab Backup | ||
env: | ||
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }} | ||
GITLAB_URL: ${{ secrets.GITLAB_URL }} | ||
GITLAB_NAMESPACE: ${{ secrets.GITLAB_NAMESPACE }} | ||
run: | | ||
git push --mirror "https://oauth2:${GITLAB_TOKEN}@${{ secrets.GITLAB_URL }}/${{ secrets.GITLAB_NAMESPACE }}/$GITHUB_REPOSITORY.git" | ||
# Configure git | ||
git config --global user.name "github-actions[bot]" | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
# Extract repository name | ||
REPO_NAME=${GITHUB_REPOSITORY#*/} | ||
# Function to get project ID | ||
get_project_id() { | ||
curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ | ||
"https://${GITLAB_URL}/api/v4/projects/${GITLAB_NAMESPACE}%2F${REPO_NAME}" | \ | ||
jq -r '.id' | ||
} | ||
# Function to configure project settings | ||
configure_project() { | ||
local project_id=$1 | ||
echo "Configuring project settings..." | ||
# Configure project settings | ||
curl -s --request PUT \ | ||
--header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ | ||
--header "Content-Type: application/json" \ | ||
--data '{ | ||
"only_allow_merge_if_pipeline_succeeds": false, | ||
"allow_merge_on_skipped_pipeline": true, | ||
"restrict_user_defined_variables": false | ||
}' \ | ||
"https://${GITLAB_URL}/api/v4/projects/${project_id}" | ||
# Remove all existing branch protection | ||
curl -s --request DELETE \ | ||
--header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ | ||
"https://${GITLAB_URL}/api/v4/projects/${project_id}/protected_branches/main" | ||
# Add updated branch protection | ||
curl -s --request POST \ | ||
--header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ | ||
--header "Content-Type: application/json" \ | ||
--data '{ | ||
"name": "main", | ||
"push_access_level": 40, | ||
"merge_access_level": 40, | ||
"unprotect_access_level": 40, | ||
"allow_force_push": true | ||
}' \ | ||
"https://${GITLAB_URL}/api/v4/projects/${project_id}/protected_branches" | ||
} | ||
# Get or create project | ||
PROJECT_ID=$(get_project_id) | ||
if [ "$PROJECT_ID" = "null" ] || [ -z "$PROJECT_ID" ]; then | ||
echo "Creating new project..." | ||
curl -s --request POST \ | ||
--header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ | ||
--header "Content-Type: application/json" \ | ||
--data "{ | ||
\"name\": \"${REPO_NAME}\", | ||
\"path\": \"${REPO_NAME}\", | ||
\"visibility\": \"private\", | ||
\"initialize_with_readme\": false | ||
}" \ | ||
"https://${GITLAB_URL}/api/v4/projects" | ||
sleep 5 # Wait for project creation | ||
PROJECT_ID=$(get_project_id) | ||
fi | ||
echo "Project ID: $PROJECT_ID" | ||
# Configure project settings | ||
configure_project "$PROJECT_ID" | ||
# Setup and push to GitLab | ||
echo "Setting up Git remote..." | ||
GITLAB_REPO_URL="https://oauth2:${GITLAB_TOKEN}@${GITLAB_URL}/${GITLAB_NAMESPACE}/${REPO_NAME}.git" | ||
git remote add gitlab "$GITLAB_REPO_URL" || git remote set-url gitlab "$GITLAB_REPO_URL" | ||
echo "Fetching from GitLab..." | ||
git fetch gitlab || true | ||
echo "Pushing to GitLab..." | ||
# Push all refs without '--mirror' to avoid hidden ref issues | ||
git push -f gitlab refs/remotes/origin/*:refs/heads/* refs/tags/*:refs/tags/* |