Skip to content

fix: use Ubuntu 24.04 base image #22

fix: use Ubuntu 24.04 base image

fix: use Ubuntu 24.04 base image #22

Workflow file for this run

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
permissions:
contents: read
deployments: write
env:
GH_TOKEN: ${{ github.token }}
CS_TOKEN: ${{ secrets.CS_TOKEN }}
CS_TEAM_ID: ${{ vars.CS_TEAM_ID }}
CS_API: ${{ vars.CS_API }}
CS_DEVDOMAIN_BASE_URL: ${{ vars.CS_DEVDOMAIN_BASE_URL }}
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 || '201' }}
WORKSPACE_TIMEOUT: '20m'
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 }} \
--base-image 'ubuntu-24.04' \
--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: Create Preview Deployment
id: create_deployment
uses: chrnorm/deployment-action@v2
with:
token: '${{ github.token }}'
environment: Preview
environment-url: "https://${{ steps.get_final_workspace_id.outputs.workspace_id }}-3000.${{ env.CS_DEVDOMAIN_BASE_URL }}"
- 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 prepare step for workspace ID: $WORKSPACE_ID_FOR_PIPELINE"
cs start pipeline -p demo prepare -w "$WORKSPACE_ID_FOR_PIPELINE"
echo "Syncing landscape for workspace ID: $WORKSPACE_ID_FOR_PIPELINE"
cs sync landscape -p demo -w "$WORKSPACE_ID_FOR_PIPELINE"
echo "Starting run step for workspace ID: $WORKSPACE_ID_FOR_PIPELINE"
cs start pipeline -p demo run -w "$WORKSPACE_ID_FOR_PIPELINE"
- name: Update deployment status (success)
if: success()
uses: chrnorm/deployment-status@v2
with:
token: '${{ github.token }}'
environment-url: ${{ steps.create_deployment.outputs.environment_url }}
deployment-id: ${{ steps.create_deployment.outputs.deployment_id }}
state: 'success'
- name: Update deployment status (failure)
if: failure()
uses: chrnorm/deployment-status@v2
with:
token: '${{ github.token }}'
environment-url: ${{ steps.create_deployment.outputs.environment_url }}
deployment-id: ${{ steps.create_deployment.outputs.deployment_id }}
state: 'failure'