From 7fb9d24d3efe407f1bbf97a4018980e671ddab1c Mon Sep 17 00:00:00 2001 From: pande Date: Mon, 22 Sep 2025 18:09:12 +0800 Subject: [PATCH] feat: implement OrchestratorCI workflow with environment setup and droplet creation script --- .github/workflows/orchestratorci.yml | 23 ++++-- .gitignore | 1 + Dockerfile | 6 ++ actions/orchestrating.sh | 103 +++++++++++++++++++++++++++ docker-compose.yml | 13 ++++ 5 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100755 actions/orchestrating.sh create mode 100644 docker-compose.yml diff --git a/.github/workflows/orchestratorci.yml b/.github/workflows/orchestratorci.yml index fbfa245..1167beb 100644 --- a/.github/workflows/orchestratorci.yml +++ b/.github/workflows/orchestratorci.yml @@ -6,22 +6,35 @@ on: spec: required: true type: string - outputs: runner_id: + required: true + type: string + + secrets: + DO_TOKEN: + required: true + + outputs: + ready: description: "The result of the orchestrator job" - value: ${{ jobs.provisioning.outputs.runner_id }} # Gunakan jobs + value: ${{ jobs.provisioning.outputs.ready }} # Gunakan jobs jobs: provisioning: runs-on: gitea-orchestrator name: OrchestratorCI Job outputs: - runner_id: ${{ steps.provisioning.outputs.runner_id }} # Sesuaikan nama + ready: ${{ steps.provisioning.outputs.ready }} # Sesuaikan nama steps: - name: Run OrchestratorCI id: provisioning + container: digitalocean/doctl + env: + SPEC: ${{ inputs.spec }} + RUNNER_ID: ${{ inputs.runner_id }} + DO_TOKEN: ${{ secrets.DO_TOKEN }} run: | - echo "Running OrchestratorCI with spec: ${{ inputs.spec }}" + echo "Preparing Script..." # Placeholder for actual OrchestratorCI commands # Simulate a result output - echo "runner_id=pandeganteng" >> $GITHUB_OUTPUT \ No newline at end of file + echo "ready=true" >> $GITHUB_OUTPUT \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..67929cc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM alpine:latest +RUN apk --no-cache add curl bash git doctl + +WORKDIR /app + +CMD ["sleep", "3600"] \ No newline at end of file diff --git a/actions/orchestrating.sh b/actions/orchestrating.sh new file mode 100755 index 0000000..1f6b93f --- /dev/null +++ b/actions/orchestrating.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +# Record start time +START_TIME=$(date +%s) + +# Function to check list of required environment variables +check_env_vars() { + local missing_vars=() + for var in "$@"; do + if [ -z "${!var}" ]; then + missing_vars+=("$var") + fi + done + if [ ${#missing_vars[@]} -ne 0 ]; then + echo "Error: The following environment variables are not set: ${missing_vars[*]}" + exit 1 + fi +} + +# Function to check if doctl is available +check_doctl() { + if ! command -v doctl &> /dev/null; then + echo "Error: doctl is not installed or not in PATH" + echo "Please install doctl: https://docs.digitalocean.com/reference/doctl/how-to/install/" + exit 1 + fi +} + +# Function to setup SSH private key +setup_ssh_key() { + if [ -z "$SSH_PRIVATE_KEY" ]; then + echo "Error: SSH_PRIVATE_KEY environment variable is not set" + exit 1 + fi + + # Create SSH directory if it doesn't exist + mkdir -p ~/.ssh + chmod 700 ~/.ssh + + # Write private key to file (decode if base64) + if echo "$SSH_PRIVATE_KEY" | base64 -d > /dev/null 2>&1; then + echo "$SSH_PRIVATE_KEY" | base64 -d > ~/.ssh/id_rsa + else + echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + fi + chmod 600 ~/.ssh/id_rsa + + echo "SSH private key has been set up successfully" +} + +# Function to create droplet +create_droplet() { + local name="${1:-runner-${RUNNER_ID}}" + local size="${2:-s-1vcpu-1gb}" + local image="${3:-ubuntu-22-04-x64}" + local region="${4:-sgp1}" + + echo "Creating droplet: $name" + echo "Size: $size, Image: $image, Region: $region" + + # Authenticate doctl + doctl auth init --access-token "$DO_TOKEN" + + # Create droplet + local droplet_id=$(doctl compute droplet create "$name" \ + --size "$size" \ + --image "$image" \ + --region "$region" \ + --ssh-keys $(doctl compute ssh-key list --format ID --no-header | tr '\n' ',' | sed 's/,$//' || echo "") \ + --format ID \ + --no-header \ + --wait) + + if [ -n "$droplet_id" ]; then + echo "Droplet created successfully with ID: $droplet_id" + echo "Droplet name: $name" + + # Get droplet IP + local droplet_ip=$(doctl compute droplet get "$droplet_id" --format PublicIPv4 --no-header) + echo "Droplet IP: $droplet_ip" + + return 0 + else + echo "Error: Failed to create droplet" + return 1 + fi +} + +# Check required environment variables +check_env_vars "SPEC" "RUNNER_ID" "DO_TOKEN" "SSH_PRIVATE_KEY" + +# Check if doctl is available +check_doctl + +# Setup SSH private key +setup_ssh_key + +create_droplet + +# Calculate and display execution time +END_TIME=$(date +%s) +EXECUTION_TIME=$((END_TIME - START_TIME)) +echo "Script execution time: ${EXECUTION_TIME} seconds" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c5cb414 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +services: + app: + build: + context: . + dockerfile: Dockerfile + entrypoint: "" + command: ["sleep", "3600"] + environment: + - DO_TOKEN=dop_v1_edf8bb32e734cf0dbbb40ce9d2b7ad494277caced8636a4823aede610b6c31fa + - RUNNER_ID=pandeganteng + - SPEC=default + volumes: + - ./actions:/actions \ No newline at end of file