Skip to content

Initial general CI workflows #32

Initial general CI workflows

Initial general CI workflows #32

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
id: parse-command
run: |
WORKFLOW_LOCAL_RUN="${{ !github.event.act }}"
COMMENT_BODY="${{ github.event.comment.body }}"
if [[ WORKFLOW_LOCAL_RUN ]]; 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"
else
echo "No environment specified. Aborting workflow."
exit 1 # Aborts the workflow # TODO: pass it as an output to the user notifier step
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
if: ${{ !github.event.act }}
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 }}
- uses: actions/checkout@v4
- name: Notify the user
uses: actions/github-script@v7
id: notify-user
with:
script: |
const environment = `${{ steps.parse-command.outputs.environment }}`;
const project = `${{ steps.parse-command.outputs.project }}`;
const infra = `${{ steps.parse-command.outputs.infra }}`;
const script = require('./actions_scripts/notify_user.js')
return await script({github, context, environment, project, infra})
# 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
id: trigger-deployment-workflow
with:
github-token: ${{ steps.zdc-auth-app-token.outputs.token || github.token }}
script: |
const environment = `${{ steps.parse-command.outputs.environment }}`;
const project = `${{ steps.parse-command.outputs.project }}`;
const workflowId = `deploy-${environment}.yml`;
let result = "";
let details = "";
try {
await github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: project,
workflow_id: workflowId,
ref: 'main', // TODO: Change branch to ref if pre, otherwise PRO should be only on main
});
status = 'OK';
details = 'succedeed';
} catch (ex) {
console.log(`FAILED TO TRIGGER WORKFLOW:\n${ex}`);
status = 'ERR';
details = `Failed to trigger the workflow ${workflowId} on repo: ${project}`;
}
// Return triggered details
return {
workflowId: workflowId,
status: status,
details: details,
};
- 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
- name: Update Deployment Status
uses: actions/github-script@v7
id: update-comment-with-deployment-status
with:
script: |
const workflowLocalRun = `${{ github.event.act }}` === 'true';
const commentOnPr = ${{ steps.notify-user.outputs.result }};
const commentId = commentOnPr.comment_id;
const workflowDetails = ${{ steps.trigger-deployment-workflow.outputs.result }};
const owner = `${{ github.event.repository.owner.login }}`;
const project = `${{ steps.parse-command.outputs.project }}`;
const runUrl = `https://github.com/${owner}/${project}/actions`;
const statusIcon = workflowDetails.status === 'OK' ? '✅' : '❌';
const statusMsg = workflowDetails.details;
const previousMsgData = commentOnPr.message;
const message = `${previousMsgData}\n${statusIcon} Deployment ${statusMsg}. [View Workflow](${runUrl})`;
console.log(message);
if (!workflowLocalRun) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: commentId,
body: message,
});
}