|
| 1 | +name: Start GitLab CI |
| 2 | +on: |
| 3 | + # Use pull_request_target to run the workflow from the base branch (e.g., main) |
| 4 | + # This ensures the trusted workflow logic executes, even for PRs from forks. |
| 5 | + # It also grants access to secrets needed for the trigger. |
| 6 | + pull_request_target: |
| 7 | + types: [opened, synchronize, reopened] |
| 8 | +jobs: |
| 9 | + trigger-gitlab: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Start Gitlab Pipeline |
| 13 | + env: |
| 14 | + # Get trigger config from secrets |
| 15 | + GL_TRIGGER_TOKEN: ${{ secrets.GL_TRIGGER_TOKEN }} |
| 16 | + GL_TRIGGER_URL: ${{ secrets.GL_TRIGGER_URL }} |
| 17 | + # Use a specific ref from secrets if provided, otherwise default to the PR's head branch name |
| 18 | + GL_TRIGGER_REF: ${{ secrets.GL_TRIGGER_REF || github.event.pull_request.head.ref }} |
| 19 | + # --- Variables to pass to GitLab --- |
| 20 | + # The commit SHA in the GitHub PR |
| 21 | + GITHUB_PR_SHA: ${{ github.event.pull_request.head.sha }} |
| 22 | + # The ref (branch name) of the PR head |
| 23 | + GITHUB_PR_REF: ${{ github.event.pull_request.head.ref }} |
| 24 | + # The repository name (e.g., 'your-org/your-repo') |
| 25 | + GITHUB_REPO: ${{ github.repository }} |
| 26 | + # The GitHub token for reporting status back |
| 27 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + GITHUB_REPO_URL: ${{ github.event.pull_request.head.repo.clone_url }} |
| 29 | + run: | |
| 30 | + # --- Safety Checks --- |
| 31 | + # Ensure critical secrets are actually available (they should be with pull_request_target) |
| 32 | + if [ -z "$GL_TRIGGER_TOKEN" ]; then |
| 33 | + echo "::error::GL_TRIGGER_TOKEN secret is missing or unavailable!" |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + if [ -z "$GITHUB_TOKEN" ]; then |
| 37 | + echo "::error::GITHUB_TOKEN is empty. Secrets may not be properly accessed." |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | + # Ensure URL is set |
| 41 | + if [ -z "$GL_TRIGGER_URL" ]; then |
| 42 | + echo "::error::GL_TRIGGER_URL secret is missing or unavailable!" |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +
|
| 46 | + echo "Triggering GitLab pipeline for SHA: ${GITHUB_PR_SHA}" |
| 47 | + curl --fail --silent --show-error --request POST \ |
| 48 | + --form token="${GL_TRIGGER_TOKEN}" \ |
| 49 | + --form ref="${GL_TRIGGER_REF}" \ |
| 50 | + --form "variables[GITHUB_PR_SHA]=${GITHUB_PR_SHA}" \ |
| 51 | + --form "variables[GITHUB_PR_REF]=${GITHUB_PR_REF}" \ |
| 52 | + --form "variables[GITHUB_REPO]=${GITHUB_REPO}" \ |
| 53 | + --form "variables[GITHUB_REPO_URL]=${GITHUB_REPO_URL}" \ |
| 54 | + "${GL_TRIGGER_URL}" > /dev/null |
| 55 | + echo "GitLab pipeline triggered." |
0 commit comments