Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/GH-1-initial-general-wor…
Browse files Browse the repository at this point in the history
…kflows-tests'
  • Loading branch information
TheRustifyer committed Dec 24, 2024
2 parents acea12d + da92ce1 commit 28553f4
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
122 changes: 122 additions & 0 deletions .github/workflows/deploy-chatbot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Handle PRE env deployment commands in PR Comments

on:
issue_comment:
types:
- created

jobs:
handle-slash-command:
runs-on: ubuntu-latest

steps:
# Step 1: Parse Deployment Command
- name: Parse Deployment Command
id: parse_command
run: |
COMMENT_BODY="${{ github.event.comment.body }}"
echo "Comment received: $COMMENT_BODY"
# Initialize variables
ENVIRONMENT=""
PROJECT=""
INFRA=""
# Parse environment
if [[ "$COMMENT_BODY" =~ --environment[[:space:]]+([a-zA-Z0-9_-]+) ]]; then
ENVIRONMENT="${BASH_REMATCH[1]}"
echo "Environment: $ENVIRONMENT"
fi
# Parse project
if [[ "$COMMENT_BODY" =~ --project[[:space:]]+\"([^\"]+)\" ]]; then
PROJECT="${BASH_REMATCH[1]}"
echo "Project: $PROJECT"
fi
# Parse infra
if [[ "$COMMENT_BODY" =~ --infra[[:space:]]+([a-zA-Z0-9_-]+) ]]; then
INFRA="${BASH_REMATCH[1]}"
echo "Infra: $INFRA"
fi
# Output parsed values
echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT
echo "project=$PROJECT" >> $GITHUB_OUTPUT
echo "infra=$INFRA" >> $GITHUB_OUTPUT
# Step 2: Validate and Trigger Deploy Workflow
- name: Trigger Deployment Workflow
if: steps.parse_command.outputs.environment != '' && steps.parse_command.outputs.project != ''
uses: actions/github-script@v6
with:
script: |
const environment = `${{ steps.parse_command.outputs.environment }}`;
const project = `${{ steps.parse_command.outputs.project }}`;
const workflowId = `deploy-${environment}.yml`;
console.log(`Triggering workflow: ${workflowId} for project: ${project}`);
github.rest.actions.createWorkflowDispatch({
owner: 'zerodaycode',
repo: project,
workflow_id: workflowId,
ref: 'main', // TODO: Change branch to ref
});
# Step 3: Deploy Infra (if requested)
- name: Deploy Infra
if: steps.parse_command.outputs.infra != ''
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
case "${{ steps.parse_command.outputs.infra }}" in
postgres)
echo "Deploying Postgres..."
docker run -d --name postgres --restart always -e POSTGRES_PASSWORD=mysecretpassword postgres
;;
redis)
echo "Deploying Redis..."
docker run -d --name redis --restart always redis
;;
all)
echo "Deploying Postgres and Redis..."
docker run -d --name postgres --restart always -e POSTGRES_PASSWORD=mysecretpassword postgres
docker run -d --name redis --restart always redis
;;
*)
echo "Unknown infra: ${{ steps.parse_command.outputs.infra }}"
;;
esac
# Step 4: Post Confirmation Comment
- name: Post Comment to PR
uses: actions/github-script@v6
with:
script: |
const environment = `${{ steps.parse_command.outputs.environment }}`;
const project = `${{ steps.parse_command.outputs.project }}`;
const infra = `${{ steps.parse_command.outputs.infra }}`;
const prNumber = context.payload.issue.number;
let message = "🚀 Deployment triggered:\n";
if (project) {
message += `- Project: \`${project}\`\n`;
}
if (environment) {
message += `- Environment: \`${environment}\`\n`;
}
if (infra) {
message += `- Infrastructure: \`${infra}\`\n`;
}
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: message,
});
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ As an initial guideline, we intend this repo to look something similar to this
app-summoner-sync/
├── .github/
│ ├── workflows/ # GitHub Actions for deployment automation and repositories management
│ │ └── deploy-chatbot.yml
│ │ └── deploy-pre.yml
│ │ └── deploy-pro.yml
├── submodules/ # Includes microservice repositories
Expand Down

0 comments on commit 28553f4

Please sign in to comment.