This repository has been archived by the owner on May 19, 2024. It is now read-only.
Develop Server Deployer (CD) #143
This file contains 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: Develop Server Deployer (CD) | |
on: | |
workflow_run: | |
workflows: ["Develop Server Integrator (CI)"] | |
branches: [main] | |
types: | |
- completed | |
workflow_dispatch: | |
inputs: | |
version: | |
description: 'Image version tag (e.g., 0.2411.135 or latest)' | |
required: true | |
default: 'latest' | |
jobs: | |
deploy: | |
needs: [validate_version, check_ci_success] | |
runs-on: ubuntu-latest | |
environment: DEV | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Get triggered workflow run | |
uses: actions/github-script@v6 | |
id: triggered_workflow_run | |
with: | |
script: | | |
const eventPayload = require(process.env.GITHUB_EVENT_PATH); | |
const workflowRun = eventPayload.workflow_run; | |
return { | |
head_branch: workflowRun.head_branch, | |
head_sha: workflowRun.head_sha, | |
version: `${workflowRun.head_branch}.${workflowRun.id}` | |
}; | |
- name: Set version from triggered workflow | |
if: github.event_name == 'workflow_run' | |
run: echo "version=${{ steps.triggered_workflow_run.outputs.version }}" >> $GITHUB_OUTPUT | |
- name: Send discord notification (develop server deploy start) | |
uses: appleboy/discord-action@master | |
with: | |
webhook_id: ${{ vars.SERVER_RELEASE_DISCORD_NOTI_ID }} | |
webhook_token: ${{ secrets.SERVER_RELEASE_DISCORD_NOTI_TOKEN_SECRET }} | |
message: | | |
> **🛰️ Server Deployment Start (Dev)** | |
> | |
> 🛢️ Repository : ${{ github.repository }} | |
> 🎋 Branch : ${{ github.ref }} | |
> 📐 Version : ${{ steps.triggered_workflow_run.outputs.version || github.event.inputs.version }} | |
> 🔁 Run Attempt : ${{ github.run_attempt }} | |
> 🤗 Actor : ${{ github.triggering_actor }} | |
- name: Copy Docker Compose file to server | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ vars.INSTANCE_HOST }} | |
username: ${{ vars.INSTANCE_USERNAME }} | |
key: ${{ secrets.INSTANCE_PEM_KEY }} | |
source: "./bootstrap/http/compose-dev.yaml" | |
target: "~/app" | |
overwrite: true | |
- name: Install Docker if not present | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ vars.INSTANCE_HOST }} | |
username: ${{ vars.INSTANCE_USERNAME }} | |
key: ${{ secrets.INSTANCE_PEM_KEY }} | |
script: | | |
if ! command -v docker >/dev/null 2>&1; then | |
echo "Installing Docker..." | |
sudo apt-get update | |
sudo apt-get install -y docker.io | |
else | |
echo "Docker already installed." | |
fi | |
if ! command -v docker-compose >/dev/null 2>&1; then | |
echo "Installing Docker Compose..." | |
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose | |
sudo chmod +x /usr/local/bin/docker-compose | |
else | |
echo "Docker Compose already installed." | |
fi | |
- name: Configuration Env file | |
uses: appleboy/ssh-action@master | |
env: | |
VARS_CONTEXT: ${{ toJson(vars) }} | |
SECRETS_CONTEXT: ${{ toJson(secrets) }} | |
with: | |
host: ${{ vars.INSTANCE_HOST }} | |
username: ${{ vars.INSTANCE_USERNAME }} | |
key: ${{ secrets.INSTANCE_PEM_KEY }} | |
envs: VARS_CONTEXT,SECRETS_CONTEXT | |
script: | | |
cd ~/app/bootstrap/http | |
jq -s '.[0] * .[1]' <(echo "$VARS_CONTEXT") <(echo "$SECRETS_CONTEXT") \ | |
| jq -r 'to_entries | map("\(.key)=\(.value)") | .[]' > .env | |
- name: Set deployed image version | |
id: set_version | |
run: echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT | |
- name: Run Docker Compose up | |
id: deploy | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ vars.INSTANCE_HOST }} | |
username: ${{ vars.INSTANCE_USERNAME }} | |
key: ${{ secrets.INSTANCE_PEM_KEY }} | |
script: | | |
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
sudo docker-compose -f ~/app/bootstrap/http/compose-dev.yaml pull | |
sudo VERSION=${{ github.event.inputs.version }} docker-compose -f ~/app/bootstrap/http/compose-dev.yaml up -d --force-recreate | |
- name: Send discord notification (develop server deploy failed) | |
if: failure() && steps.deploy.outcome == 'failure' | |
uses: appleboy/discord-action@master | |
with: | |
webhook_id: ${{ vars.SERVER_RELEASE_DISCORD_NOTI_ID }} | |
webhook_token: ${{ secrets.SERVER_RELEASE_DISCORD_NOTI_TOKEN_SECRET }} | |
message: | | |
> **🚨 Server Deployment Failed (Dev)** | |
> | |
> 🛢️ Repository : ${{ github.repository }} | |
> 🎋 Branch : ${{ github.ref }} | |
> 📐 Version : ${{ github.event.inputs.version }} | |
> 🔁 Run Attempt : ${{ github.run_attempt }} | |
> 🤗 Actor : ${{ github.triggering_actor }} | |
- name: Create tag | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.RELEASE_TOKEN }} | |
script: | | |
const version = '${{ steps.set_version.outputs.version }}'; | |
const tag = `dev-${version}`; | |
try { | |
await github.rest.git.createRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: `refs/tags/${tag}`, | |
sha: context.sha | |
}); | |
console.log(`Tag ${tag} created successfully.`); | |
} catch (error) { | |
if (error.status === 422) { | |
console.log(`Tag ${tag} already exists. Skipping tag creation.`); | |
} else { | |
throw error; | |
} | |
} | |
- name: Send discord notification (develop server deploy complete) | |
uses: appleboy/discord-action@master | |
with: | |
webhook_id: ${{ vars.SERVER_RELEASE_DISCORD_NOTI_ID }} | |
webhook_token: ${{ secrets.SERVER_RELEASE_DISCORD_NOTI_TOKEN_SECRET }} | |
message: | | |
> **🛰️ Server Deployment Complete (Dev)** | |
> | |
> 🛢️ Repository : ${{ github.repository }} | |
> 🎋 Branch : ${{ github.ref }} | |
> 📐 Version : ${{ github.event.inputs.version }} | |
> 🔁 Run Attempt : ${{ github.run_attempt }} | |
> 🤗 Actor : ${{ github.triggering_actor }} | |
validate_version: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'workflow_dispatch' | |
steps: | |
- name: Validate version format | |
run: | | |
if [[ "${{ github.event.inputs.version }}" =~ ^(latest|[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then | |
echo "Version format is valid" | |
else | |
echo "Invalid version format. Use 'latest' or semver format (e.g., 0.2411.135)" | |
exit 1 | |
fi | |
check_ci_success: | |
needs: validate_version | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check CI | |
if: ${{ github.event.workflow_run != null && github.event.workflow_run.conclusion != 'success' }} | |
run: | | |
echo "CI failed" | |
exit 1 |