|
| 1 | +name: Handle PRE env deployment commands in PR Comments |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: |
| 6 | + - created |
| 7 | + |
| 8 | +jobs: |
| 9 | + handle-slash-command: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + # Step 1: Parse Deployment Command |
| 14 | + - name: Parse Deployment Command |
| 15 | + id: parse_command |
| 16 | + run: | |
| 17 | + COMMENT_BODY="${{ github.event.comment.body }}" |
| 18 | + echo "Comment received: $COMMENT_BODY" |
| 19 | + |
| 20 | + # Initialize variables |
| 21 | + ENVIRONMENT="" |
| 22 | + PROJECT="" |
| 23 | + INFRA="" |
| 24 | +
|
| 25 | + # Parse environment |
| 26 | + if [[ "$COMMENT_BODY" =~ --environment[[:space:]]+([a-zA-Z0-9_-]+) ]]; then |
| 27 | + ENVIRONMENT="${BASH_REMATCH[1]}" |
| 28 | + echo "Environment: $ENVIRONMENT" |
| 29 | + fi |
| 30 | +
|
| 31 | + # Parse project |
| 32 | + if [[ "$COMMENT_BODY" =~ --project[[:space:]]+\"([^\"]+)\" ]]; then |
| 33 | + PROJECT="${BASH_REMATCH[1]}" |
| 34 | + echo "Project: $PROJECT" |
| 35 | + fi |
| 36 | +
|
| 37 | + # Parse infra |
| 38 | + if [[ "$COMMENT_BODY" =~ --infra[[:space:]]+([a-zA-Z0-9_-]+) ]]; then |
| 39 | + INFRA="${BASH_REMATCH[1]}" |
| 40 | + echo "Infra: $INFRA" |
| 41 | + fi |
| 42 | +
|
| 43 | + # Output parsed values |
| 44 | + echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT |
| 45 | + echo "project=$PROJECT" >> $GITHUB_OUTPUT |
| 46 | + echo "infra=$INFRA" >> $GITHUB_OUTPUT |
| 47 | +
|
| 48 | + # Step 2: Validate and Trigger Deploy Workflow |
| 49 | + - name: Trigger Deployment Workflow |
| 50 | + if: steps.parse_command.outputs.environment != '' && steps.parse_command.outputs.project != '' |
| 51 | + uses: actions/github-script@v6 |
| 52 | + with: |
| 53 | + script: | |
| 54 | + const environment = `${{ steps.parse_command.outputs.environment }}`; |
| 55 | + const project = `${{ steps.parse_command.outputs.project }}`; |
| 56 | + const workflowId = `deploy-${environment}.yml`; |
| 57 | +
|
| 58 | + console.log(`Triggering workflow: ${workflowId} for project: ${project}`); |
| 59 | +
|
| 60 | + github.rest.actions.createWorkflowDispatch({ |
| 61 | + owner: 'zerodaycode', |
| 62 | + repo: project, |
| 63 | + workflow_id: workflowId, |
| 64 | + ref: 'main', // TODO: Change branch to ref |
| 65 | + }); |
| 66 | +
|
| 67 | + # Step 3: Deploy Infra (if requested) |
| 68 | + - name: Deploy Infra |
| 69 | + if: steps.parse_command.outputs.infra != '' |
| 70 | + |
| 71 | + with: |
| 72 | + host: ${{ secrets.SSH_HOST }} |
| 73 | + username: ${{ secrets.SSH_USERNAME }} |
| 74 | + key: ${{ secrets.SSH_KEY }} |
| 75 | + script: | |
| 76 | + case "${{ steps.parse_command.outputs.infra }}" in |
| 77 | + postgres) |
| 78 | + echo "Deploying Postgres..." |
| 79 | + docker run -d --name postgres --restart always -e POSTGRES_PASSWORD=mysecretpassword postgres |
| 80 | + ;; |
| 81 | + redis) |
| 82 | + echo "Deploying Redis..." |
| 83 | + docker run -d --name redis --restart always redis |
| 84 | + ;; |
| 85 | + all) |
| 86 | + echo "Deploying Postgres and Redis..." |
| 87 | + docker run -d --name postgres --restart always -e POSTGRES_PASSWORD=mysecretpassword postgres |
| 88 | + docker run -d --name redis --restart always redis |
| 89 | + ;; |
| 90 | + *) |
| 91 | + echo "Unknown infra: ${{ steps.parse_command.outputs.infra }}" |
| 92 | + ;; |
| 93 | + esac |
| 94 | +
|
| 95 | + # Step 4: Post Confirmation Comment |
| 96 | + - name: Post Comment to PR |
| 97 | + uses: actions/github-script@v6 |
| 98 | + with: |
| 99 | + script: | |
| 100 | + const environment = `${{ steps.parse_command.outputs.environment }}`; |
| 101 | + const project = `${{ steps.parse_command.outputs.project }}`; |
| 102 | + const infra = `${{ steps.parse_command.outputs.infra }}`; |
| 103 | + const prNumber = context.payload.issue.number; |
| 104 | + |
| 105 | + let message = "🚀 Deployment triggered:\n"; |
| 106 | + if (project) { |
| 107 | + message += `- Project: \`${project}\`\n`; |
| 108 | + } |
| 109 | + if (environment) { |
| 110 | + message += `- Environment: \`${environment}\`\n`; |
| 111 | + } |
| 112 | + if (infra) { |
| 113 | + message += `- Infrastructure: \`${infra}\`\n`; |
| 114 | + } |
| 115 | +
|
| 116 | + github.rest.issues.createComment({ |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + issue_number: prNumber, |
| 120 | + body: message, |
| 121 | + }); |
| 122 | +
|
0 commit comments