|
1 |
| -name: Sync to GitLab |
| 1 | +name: Backup to GitLab |
2 | 2 |
|
3 | 3 | on:
|
4 | 4 | push:
|
5 | 5 | branches:
|
6 | 6 | - main
|
| 7 | + - master |
7 | 8 | schedule:
|
8 |
| - - cron: "0 2 * * *" # Optional: Runs daily at 2 AM UTC |
| 9 | + - cron: "0 2 * * *" |
9 | 10 |
|
10 | 11 | jobs:
|
11 |
| - sync: |
| 12 | + backup: |
12 | 13 | runs-on: ubuntu-latest
|
13 |
| - |
14 | 14 | steps:
|
15 |
| - - name: Checkout the repository |
| 15 | + - name: Checkout repository |
16 | 16 | uses: actions/checkout@v3
|
| 17 | + with: |
| 18 | + fetch-depth: 0 |
17 | 19 |
|
18 |
| - - name: Configure Git |
19 |
| - run: | |
20 |
| - git config --global user.name "github-actions[bot]" |
21 |
| - git config --global user.email "github-actions[bot]@users.noreply.github.com" |
22 |
| -
|
23 |
| - - name: Add GitLab as remote |
24 |
| - run: | |
25 |
| - git remote add gitlab "${{ secrets.GITLAB_URL }}/${{ secrets.GITLAB_NAMESPACE }}/$GITHUB_REPOSITORY.git" |
26 |
| -
|
27 |
| - - name: Push to GitLab |
| 20 | + - name: Setup GitLab Backup |
28 | 21 | env:
|
29 | 22 | GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
|
| 23 | + GITLAB_URL: ${{ secrets.GITLAB_URL }} |
| 24 | + GITLAB_NAMESPACE: ${{ secrets.GITLAB_NAMESPACE }} |
30 | 25 | run: |
|
31 |
| - git push --mirror "https://oauth2:${GITLAB_TOKEN}@${{ secrets.GITLAB_URL }}/${{ secrets.GITLAB_NAMESPACE }}/$GITHUB_REPOSITORY.git" |
| 26 | + # Configure git |
| 27 | + git config --global user.name "github-actions[bot]" |
| 28 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 29 | + |
| 30 | + # Extract repository name |
| 31 | + REPO_NAME=${GITHUB_REPOSITORY#*/} |
| 32 | + |
| 33 | + # Function to get project ID |
| 34 | + get_project_id() { |
| 35 | + curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ |
| 36 | + "https://${GITLAB_URL}/api/v4/projects/${GITLAB_NAMESPACE}%2F${REPO_NAME}" | \ |
| 37 | + jq -r '.id' |
| 38 | + } |
| 39 | + |
| 40 | + # Function to configure project settings |
| 41 | + configure_project() { |
| 42 | + local project_id=$1 |
| 43 | + echo "Configuring project settings..." |
| 44 | + |
| 45 | + # Configure project settings |
| 46 | + curl -s --request PUT \ |
| 47 | + --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ |
| 48 | + --header "Content-Type: application/json" \ |
| 49 | + --data '{ |
| 50 | + "only_allow_merge_if_pipeline_succeeds": false, |
| 51 | + "allow_merge_on_skipped_pipeline": true, |
| 52 | + "restrict_user_defined_variables": false |
| 53 | + }' \ |
| 54 | + "https://${GITLAB_URL}/api/v4/projects/${project_id}" |
| 55 | + |
| 56 | + # Remove all existing branch protection |
| 57 | + curl -s --request DELETE \ |
| 58 | + --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ |
| 59 | + "https://${GITLAB_URL}/api/v4/projects/${project_id}/protected_branches/main" |
| 60 | + |
| 61 | + # Add updated branch protection |
| 62 | + curl -s --request POST \ |
| 63 | + --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ |
| 64 | + --header "Content-Type: application/json" \ |
| 65 | + --data '{ |
| 66 | + "name": "main", |
| 67 | + "push_access_level": 40, |
| 68 | + "merge_access_level": 40, |
| 69 | + "unprotect_access_level": 40, |
| 70 | + "allow_force_push": true |
| 71 | + }' \ |
| 72 | + "https://${GITLAB_URL}/api/v4/projects/${project_id}/protected_branches" |
| 73 | + } |
| 74 | + |
| 75 | + # Get or create project |
| 76 | + PROJECT_ID=$(get_project_id) |
| 77 | + |
| 78 | + if [ "$PROJECT_ID" = "null" ] || [ -z "$PROJECT_ID" ]; then |
| 79 | + echo "Creating new project..." |
| 80 | + curl -s --request POST \ |
| 81 | + --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ |
| 82 | + --header "Content-Type: application/json" \ |
| 83 | + --data "{ |
| 84 | + \"name\": \"${REPO_NAME}\", |
| 85 | + \"path\": \"${REPO_NAME}\", |
| 86 | + \"visibility\": \"private\", |
| 87 | + \"initialize_with_readme\": false |
| 88 | + }" \ |
| 89 | + "https://${GITLAB_URL}/api/v4/projects" |
| 90 | + |
| 91 | + sleep 5 # Wait for project creation |
| 92 | + PROJECT_ID=$(get_project_id) |
| 93 | + fi |
| 94 | + |
| 95 | + echo "Project ID: $PROJECT_ID" |
| 96 | + |
| 97 | + # Configure project settings |
| 98 | + configure_project "$PROJECT_ID" |
| 99 | + |
| 100 | + # Setup and push to GitLab |
| 101 | + echo "Setting up Git remote..." |
| 102 | + GITLAB_REPO_URL="https://oauth2:${GITLAB_TOKEN}@${GITLAB_URL}/${GITLAB_NAMESPACE}/${REPO_NAME}.git" |
| 103 | + git remote add gitlab "$GITLAB_REPO_URL" || git remote set-url gitlab "$GITLAB_REPO_URL" |
| 104 | + |
| 105 | + echo "Fetching from GitLab..." |
| 106 | + git fetch gitlab || true |
| 107 | + |
| 108 | + echo "Pushing to GitLab..." |
| 109 | + # Push all refs without '--mirror' to avoid hidden ref issues |
| 110 | + git push -f gitlab refs/remotes/origin/*:refs/heads/* refs/tags/*:refs/tags/* |
0 commit comments