-
Notifications
You must be signed in to change notification settings - Fork 0
139 lines (115 loc) · 5.72 KB
/
preview.yaml
File metadata and controls
139 lines (115 loc) · 5.72 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
name: Create Codesphere Workspace on PR
on:
pull_request:
types: [opened, reopened, synchronize] # Triggers when a PR is opened, reopened, or new commits are pushed to it
jobs:
create-workspace:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ github.token }}
CS_TOKEN: ${{ secrets.CS_TOKEN }}
CS_TEAM_ID: ${{ vars.CS_TEAM_ID }}
CS_API: ${{ vars.CS_API }}
WORKSPACE_NAME: "${{ github.event.pull_request.head.repo.name }}-preview-#${{ github.event.pull_request.number }}"
REPO_URL: ${{ github.event.pull_request.head.repo.clone_url }}
BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
IS_PRIVATE_REPO: true
CODESPHERE_PLAN_ID: ${{ vars.CODESPHERE_DEFAULT_PLAN_ID || '8' }}
WORKSPACE_TIMEOUT: '5m'
steps:
- name: Checkout repository (optional, for context)
uses: actions/checkout@v4
- name: Install Codesphere CLI
run: |
echo "Downloading Codesphere CLI"
gh release download -R codesphere-cloud/cs-go -O /usr/local/bin/cs -p "*linux_amd64"
chmod +x /usr/local/bin/cs
cs version
- name: Check and Get Existing Workspace ID
id: check_get_workspace # Give this step an ID to reference its outputs
run: |
WORKSPACE_NAME_TO_FIND="${{ env.WORKSPACE_NAME }}"
echo "Checking for existing workspace with name: '$WORKSPACE_NAME_TO_FIND'"
EXISTING_WORKSPACES_OUTPUT=$(cs list workspaces)
echo "--- cs list workspaces output ---"
echo "$EXISTING_WORKSPACES_OUTPUT"
echo "---------------------------------"
# Search for the line containing the exact workspace name.
# Use head -n 1 in case multiple lines might contain the substring (unlikely for exact name, but safe).
MATCHING_LINE=$(echo "$EXISTING_WORKSPACES_OUTPUT" | grep -F "${WORKSPACE_NAME_TO_FIND}" | head -n 1)
FOUND_ID=""
if [ -n "$MATCHING_LINE" ]; then
# Extract ID (which is the third pipe-separated field from the table body) and trim whitespace.
# Example line: "| 30809 | 64976 | codesphere-community/FlowiseAI |"
# Field 1: TEAM ID, Field 2: ID, Field 3: NAME
FOUND_ID=$(echo "$MATCHING_LINE" | awk -F'|' '{print $3}' | xargs) # xargs trims leading/trailing whitespace
echo "Found existing workspace '$WORKSPACE_NAME_TO_FIND' with ID: '$FOUND_ID'"
echo "exists=true" >> $GITHUB_OUTPUT
echo "workspace_id=$FOUND_ID" >> $GITHUB_OUTPUT
else
echo "Workspace '$WORKSPACE_NAME_TO_FIND' does not exist."
echo "exists=false" >> $GITHUB_OUTPUT
echo "workspace_id=" >> $GITHUB_OUTPUT # Explicitly empty if not found
fi
- name: Maybe Create Codesphere Workspace
id: create_workspace_step
if: ${{ steps.check_get_workspace.outputs.exists == 'false' }}
run: |
WORKSPACE_OUTPUT=$(
cs create workspace "${{ env.WORKSPACE_NAME }}" \
-r "${{ env.REPO_URL }}" \
-b "${{ env.BRANCH_NAME }}" \
-P \
-p ${{ env.CODESPHERE_PLAN_ID }} \
--timeout ${{ env.WORKSPACE_TIMEOUT }}
)
echo "$WORKSPACE_OUTPUT"
WORKSPACE_ID=$(echo "$WORKSPACE_OUTPUT" | grep -oP '^ID: \K\d+' | tr -d '\n\r ')
echo "Parsed Workspace ID for check: '$WORKSPACE_ID'"
if [ -z "$WORKSPACE_ID" ]; then
echo "Error: Could not parse a valid Workspace ID from command output. Output was:"
echo "$WORKSPACE_OUTPUT"
exit 1
fi
echo "workspace_id=$WORKSPACE_ID" >> $GITHUB_OUTPUT
- name: Determine Workspace ID
id: get_final_workspace_id
run: |
WORKSPACE_ID_FOR_USE="${{ steps.check_get_workspace.outputs.workspace_id }}"
if [ -z "$WORKSPACE_ID_FOR_USE" ]; then
WORKSPACE_ID_FOR_USE="${{ steps.create_workspace_step.outputs.workspace_id }}"
fi
if [ -z "$WORKSPACE_ID_FOR_USE" ]; then
echo "Error: Final Workspace ID could not be determined for subsequent operations."
exit 1
fi
echo "workspace_id=$WORKSPACE_ID_FOR_USE" >> $GITHUB_OUTPUT
- name: Update Workspace to Upstream Revision
run: |
WORKSPACE_ID_FOR_GIT="${{ steps.get_final_workspace_id.outputs.workspace_id }}"
TARGET_BRANCH="${{ env.BRANCH_NAME }}"
if [ -z "$WORKSPACE_ID_FOR_GIT" ]; then
echo "Error: Cannot determine Workspace ID to update."
exit 1
fi
if [ -z "$TARGET_BRANCH" ]; then
echo "Error: Branch name not available for git operations."
exit 1
fi
# Update branch of the Workspace
cs git --branch $TARGET_BRANCH -w "$WORKSPACE_ID_FOR_GIT"
echo "Updated branch of Workspace $WORKSPACE_ID_FOR_GIT to $TARGET_BRANCH"
- name: Start Codesphere Pipeline
run: |
# Prioritize the ID from the 'check_get_workspace' step if it existed,
# otherwise use the ID from the 'create_workspace_step' if it was newly created.
WORKSPACE_ID_FOR_PIPELINE="${{ steps.get_final_workspace_id.outputs.workspace_id }}"
if [ -z "$WORKSPACE_ID_FOR_PIPELINE" ]; then
WORKSPACE_ID_FOR_PIPELINE="${{ steps.create_workspace_step.outputs.workspace_id }}"
fi
if [ -z "$WORKSPACE_ID_FOR_PIPELINE" ]; then
echo "Error: Cannot determine Workspace ID to start pipeline. Was it created/found?"
exit 1
fi
echo "Starting pipeline for workspace ID: $WORKSPACE_ID_FOR_PIPELINE"
cs start pipeline -p demo prepare run -w "$WORKSPACE_ID_FOR_PIPELINE"