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