|
| 1 | +# This workflow runs on success of the GitLab workflow. |
| 2 | +# If we run it on PR or merge_group, then we can't use secrets. |
| 3 | +--- |
| 4 | +name: Start GitLab CI |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ["GitLab"] |
| 9 | + types: [completed] |
| 10 | + |
| 11 | +jobs: |
| 12 | + trigger-gitlab: |
| 13 | + # this should never fail, but check it anyway in case we ever change it |
| 14 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 15 | + runs-on: ubuntu-24.04 |
| 16 | + env: |
| 17 | + IMAGEBUILDER_BOT_GITLAB_SSH_KEY: ${{ secrets.IMAGEBUILDER_BOT_GITLAB_SSH_KEY }} |
| 18 | + steps: |
| 19 | + - name: Report status |
| 20 | + uses: haya14busa/action-workflow_run-status@v1 |
| 21 | + |
| 22 | + - name: Apt update |
| 23 | + run: sudo apt update |
| 24 | + |
| 25 | + - name: Install Dependencies |
| 26 | + run: | |
| 27 | + sudo apt install -y jq |
| 28 | +
|
| 29 | + - name: Clone repository |
| 30 | + uses: actions/checkout@v6 |
| 31 | + with: |
| 32 | + ref: ${{ github.event.workflow_run.head_sha }} |
| 33 | + fetch-depth: 0 |
| 34 | + |
| 35 | + - name: Get open PRs |
| 36 | + # Since this workflow doesn't run on a PR trigger, we need to find the |
| 37 | + # PR number by querying the GH API and selecting on the commit ID |
| 38 | + uses: octokit/request-action@v2.x |
| 39 | + id: fetch_pulls |
| 40 | + env: |
| 41 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 42 | + with: |
| 43 | + route: GET /repos/${{ github.repository }}/pulls |
| 44 | + per_page: 100 |
| 45 | + |
| 46 | + - name: Checkout branch |
| 47 | + id: pr_data |
| 48 | + env: |
| 49 | + BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 50 | + run: | |
| 51 | + PR_DATA=$(mktemp) |
| 52 | + # use uuid as a file terminator to avoid conflicts with data content |
| 53 | + cat > "$PR_DATA" <<'a21b3e7f-d5eb-44a3-8be0-c2412851d2e6' |
| 54 | + ${{ steps.fetch_pulls.outputs.data }} |
| 55 | + a21b3e7f-d5eb-44a3-8be0-c2412851d2e6 |
| 56 | +
|
| 57 | + PR=$(jq -rc '.[] | select(.head.sha | contains("${{ github.event.workflow_run.head_sha }}")) | select(.state | contains("open"))' "$PR_DATA" | jq -r .number) |
| 58 | + if [ ! -z "$PR" ]; then |
| 59 | + # Create branch named PR-<number> to push to GitLab |
| 60 | + echo "pr_branch=PR-$PR" >> "$GITHUB_OUTPUT" |
| 61 | + git checkout -b PR-$PR |
| 62 | + else |
| 63 | + git checkout "${BRANCH}" |
| 64 | + fi |
| 65 | +
|
| 66 | + - name: Push to GitLab |
| 67 | + run: | |
| 68 | + mkdir -p ~/.ssh |
| 69 | + echo "${IMAGEBUILDER_BOT_GITLAB_SSH_KEY}" > ~/.ssh/id_rsa |
| 70 | + chmod 400 ~/.ssh/id_rsa |
| 71 | + touch ~/.ssh/known_hosts |
| 72 | + ssh-keyscan -t rsa gitlab.com >> ~/.ssh/known_hosts |
| 73 | + git remote add ci git@gitlab.com:redhat/services/products/image-builder/ci/bootc-foundry.git |
| 74 | + git push -f ci |
0 commit comments