Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1236f42
Merge pull request #6 from zerodaycode/main
TheRustifyer Dec 21, 2024
389fc4b
Update deploy-pre-chatbot.yml
TheRustifyer Dec 21, 2024
ca18158
Update deploy-pre-chatbot.yml
TheRustifyer Dec 21, 2024
bd71c4e
Update deploy-pre-chatbot.yml
TheRustifyer Dec 21, 2024
c77da06
fix: Update deploy-pre-chatbot.yml
TheRustifyer Dec 21, 2024
6459e94
fix: Update deploy-pre-chatbot.yml
TheRustifyer Dec 21, 2024
d295cab
fix: Update deploy-pre-chatbot.yml
TheRustifyer Dec 21, 2024
b73bc09
fix(deploy-pre): Change on workflow
gbm25 Dec 23, 2024
819b3bb
fix(deploy-pre): Change on workflow
gbm25 Dec 23, 2024
35b275e
fix(deploy-pre): Change on workflow
gbm25 Dec 23, 2024
a38dd29
fix(deploy-pre): Change on workflow
gbm25 Dec 23, 2024
736d9e0
fix(deploy-pre): Change on workflow
gbm25 Dec 23, 2024
453d1b0
fix(deploy-pre): Simplify workflow
gbm25 Dec 23, 2024
2e76fee
chore(deploy-pre): Remove unneeded log.
gbm25 Dec 23, 2024
799bc1c
fix: incorrect JS syntax on github-script
TheRustifyer Dec 23, 2024
03f44c2
fix: yml syntax on JavaScript github script
TheRustifyer Dec 23, 2024
f6321c6
fix: gh env var outside context
TheRustifyer Dec 23, 2024
88d7d53
fix: retrieving the branch from within the host env vars
TheRustifyer Dec 23, 2024
a9e341e
fix: case sensitive var
TheRustifyer Dec 23, 2024
09ed0d1
fix: env var on context
TheRustifyer Dec 23, 2024
28fe167
fix: new try
TheRustifyer Dec 23, 2024
047f15d
chore: path of the docker files
TheRustifyer Dec 23, 2024
90d62ba
Merge pull request #10 from zerodaycode/main
TheRustifyer Dec 23, 2024
dd4a3e5
First deployment on PRE via GitHub actions triggered by a chat comman…
TheRustifyer Dec 23, 2024
99c5179
Create CODEOWNERS
TheRustifyer Dec 24, 2024
d8c808a
Docker static ip (#12)
TheRustifyer Dec 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
##########################################
# code ownership
##########################################

# default ownership: default owners for everything in the repo
* @zerodaycode/admins
75 changes: 53 additions & 22 deletions .github/workflows/deploy-pre-chatbot.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Handle Deploy Commands in PR Comments
name: Handle PRE env deployment commands in PR Comments

on:
issue_comment:
Expand All @@ -10,37 +10,68 @@ jobs:
runs-on: ubuntu-latest

steps:
# Step 1: Check if the command is `/deploy-pre`
# Step 1: Verify Command in Comment
- name: Check for `/deploy-pre` Command
id: check_command
uses: peter-evans/slash-command-action@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
command: /deploy-pre
run: |
COMMENT_BODY="${{ github.event.comment.body }}"
echo "Comment received: $COMMENT_BODY"
if [[ "$COMMENT_BODY" == "/deploy-pre" ]]; then
echo "Command `/deploy-pre` found."
echo "valid=true" >> $GITHUB_OUTPUT
else
echo "No valid command found."
echo "valid=false" >> $GITHUB_OUTPUT
fi

# Step 2: Extract Branch Reference from PR
- name: Get Pull Request Details
if: steps.check_command.outputs.command == 'true'
# Step 2: Extract PR Branch
- name: Extract PR Branch
if: steps.check_command.outputs.valid == 'true'
id: pr_details
run: |
PR_NUMBER="${{ github.event.issue.number }}"
REPO_OWNER="${{ github.repository_owner }}"
REPO_NAME="${{ github.event.repository.name }}"

echo "Fetching PR details for PR #$PR_NUMBER"

# Use GitHub REST API to fetch the PR details
PR_DETAILS=$(curl -s \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER")
# Extract the branch name (head.ref)
BRANCH=$(echo "$PR_DETAILS" | jq -r '.head.ref')

# Output the branch name
echo "branch=$BRANCH" >> $GITHUB_OUTPUT

# Step 3: Trigger Deploy Workflow
- name: Trigger Deploy Workflow
if: steps.check_command.outputs.valid == 'true'
uses: actions/github-script@v6
with:
script: |
const pr = context.payload.issue.pull_request;
if (!pr) {
throw new Error("The /deploy-pre command must be used in a pull request comment.");
}
return pr.head.ref;
result-encoding: string
const branch = `${{ steps.pr_details.outputs.branch }}`;
console.log("Triggering deploy-pre.yml for branch:", branch);
github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "deploy-pre.yml",
ref: branch,
});

# Step 3: Trigger Deploy Workflow for the PR Branch
- name: Trigger Deploy Workflow
if: steps.check_command.outputs.command == 'true'
# Step 4: Post Confirmation Comment
- name: Post Comment to PR
if: steps.check_command.outputs.valid == 'true'
uses: actions/github-script@v6
with:
script: |
github.rest.actions.createWorkflowDispatch({
const branch = `${{ steps.pr_details.outputs.branch }}`;
const prNumber = context.payload.issue.number;
console.log(`Commenting back on PR #${prNumber}`);
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "deploy-pre.yml", # Replace with your workflow filename
ref: "${{ steps.pr_details.outputs.result }}"
})
issue_number: prNumber,
body: `✅ Workflow \`deploy-pre.yml\` has been triggered for branch \`${branch}\`.`,
});
51 changes: 28 additions & 23 deletions .github/workflows/deploy-pre.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,50 @@ jobs:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the code
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Docker
uses: docker/setup-buildx-action@v2

- name: Build Docker Image
# Step 2: Build the Docker image using Docker Compose
- name: Build Docker Image with Docker Compose
run: |
docker build -t api-gateway:latest ./docker
docker compose -f ./docker/docker-compose.yml build

- name: Save Docker Image
# Step 3: Save and compress the Docker image
- name: Save and Compress Docker Image
run: |
docker save api-gateway:latest | gzip > api-gateway.tar.gz
docker save api-gateway:latest > api-gateway.tar.gz

- name: Copy Docker Image and Compose File to Droplet
uses: appleboy/[email protected]
# Step 4: Transfer compressed Docker image to the remote server
- name: Transfer Docker Image to Droplet
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
source: |
./api-gateway.tar.gz
./docker-compose.yml
target: /opt/api-gateway/
source: api-gateway.tar.gz
target: /opt/summoners-sync/api-gateway

- name: Deploy with Docker Compose
# Step 5: Load and run the Docker image on the remote server
- name: Deploy Docker Image on Remote
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /opt/api-gateway
set -e

# Stop and remove old services
docker-compose down || true
# Navigate to deployment directory
cd /opt/summoners-sync/api-gateway

# Stop and remove existing container if it exists
docker stop api-gateway || true
docker rm api-gateway || true
docker rmi api-gateway || true

# Load the new Docker image
gunzip -c api-gateway.tar.gz | docker load
# Load the Docker image
docker load < api-gateway.tar.gz

# Start the service with the new image
docker-compose up -d
# Run the new container
docker run -d --name api-gateway -e SPRING_PROFILES_ACTIVE=dev --restart always api-gateway:latest
13 changes: 6 additions & 7 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
FROM maven:3.9.9-ibm-semeru-23-jammy AS build
WORKDIR /app

COPY ../mvnw ./
COPY ../.mvn .mvn
COPY ../pom.xml ./
COPY ../src/ ./src
COPY mvnw ./
COPY .mvn .mvn
COPY pom.xml ./
COPY src/ ./src

RUN chmod +x mvnw && \
# ./mvnw clean install
./mvnw install -DskipTests
# Package the application (this creates the JAR file)
RUN mvn clean package -DskipTests
Expand All @@ -18,10 +17,10 @@ FROM openjdk:23-jdk-slim
WORKDIR /app

# Copy the built JAR from the first stage
COPY --from=build /app/target/summonerssync.apigateway-0.0.1-SNAPSHOT.jar app.jar
COPY --from=build /app/target/summonerssync.apigateway-0.0.1-SNAPSHOT.jar ag-summoners-sync.jar

# Expose the port the app runs on
EXPOSE 8080

# Run Spring Boot app
CMD ["java", "-jar", "app.jar"]
CMD ["java", "-jar", "ag-summoners-sync.jar"]
15 changes: 11 additions & 4 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
version: "3.8"
services:
api-gateway:
container_name: api-gateway
build:
context: ..
dockerfile: docker/Dockerfile
image: api-gateway:latest
ports:
- "8080:8080"
networks:
api_gateway_network:
ipv4_address: 172.17.0.2
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://database:5432/gatewaydb
SPRING_DATASOURCE_USERNAME: admin
SPRING_DATASOURCE_PASSWORD: admin
SPRING_PROFILES_ACTIVE: dev
networks:
api_gateway_network:
driver: bridge
ipam:
config:
- subnet: 172.17.0.2/16

Loading