Deploy Chatbot #15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Handle deployment commands in PR Comments | |
| on: | |
| issue_comment: | |
| types: | |
| - created | |
| jobs: | |
| handle-deployment-on-pr-comment: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Parse Deployment Command | |
| if: ${{ !env.ACT }} | |
| id: parse_command | |
| run: | | |
| COMMENT_BODY="${{ github.event.comment.body }}" | |
| if [[ "$env.ACT" ]]; then | |
| echo "Setting the command as a mock since it's running locally with ACT" | |
| COMMENT_BODY='/deploy --environment pre --project ag-summoners-sync' | |
| fi | |
| 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:]]+([a-zA-Z0-9_-]+) ]]; then | |
| PROJECT="${BASH_REMATCH[1]}" | |
| echo "Project: $PROJECT" | |
| # TODO: else stopping the workflow if no env present | |
| 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 | |
| - name: Get GH Zero Day Code APP token | |
| uses: actions/create-github-app-token@v1 | |
| id: zdc-auth-app-token | |
| with: | |
| app-id: ${{ vars.ZDC_AUTH_APP_ID }} | |
| private-key: ${{ secrets.ZDC_AUTH_PRIVATE_KEY }} | |
| owner: ${{ github.repository_owner }} | |
| - name: Notify the user | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const environment = `${{ steps.parse_command.outputs.environment }}`; | |
| const project = `${{ steps.parse_command.outputs.project }}`; | |
| const infra = `${{ steps.parse_command.outputs.infra }}`; | |
| let message = "🚀 Deployment action received:\n"; | |
| if (project) { | |
| message += `- Project: \`${project}\`\n`; | |
| } | |
| if (environment) { | |
| message += `- Environment: \`${environment}\`\n`; | |
| } | |
| if (infra) { | |
| message += `- Infrastructure: \`${infra}\`\n`; | |
| } | |
| const localRun = `${{ env.ACT }}` === "true"; | |
| if (localRun) { | |
| console.log(`Action is being runned locally by 'ACT'. | |
| Skipping the notify user on PR, but output would have been: | |
| ${message}`); | |
| return; | |
| } | |
| const prNumber = context.payload.issue.number; | |
| github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: message, | |
| }); | |
| # TODO: from here, isolate them in independent workflows | |
| # and just call them depending on the input by having just | |
| # a simple and unique trigger deployment | |
| - name: Trigger Deployment Workflow | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ steps.zdc-auth-app-token.outputs.token }} | |
| 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: context.repo.owner, | |
| repo: project, | |
| workflow_id: workflowId, | |
| ref: 'main', // TODO: Change branch to ref | |
| }); | |
| - 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 | |