Skip to content

Commit b6de134

Browse files
authored
Merge pull request #19 from zerodaycode/develop
Develop to main
2 parents 005519c + d8c808a commit b6de134

File tree

5 files changed

+104
-56
lines changed

5 files changed

+104
-56
lines changed

.github/CODEOWNERS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
##########################################
2+
# code ownership
3+
##########################################
4+
5+
# default ownership: default owners for everything in the repo
6+
* @zerodaycode/admins
Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Handle Deploy Commands in PR Comments
1+
name: Handle PRE env deployment commands in PR Comments
22

33
on:
44
issue_comment:
@@ -10,37 +10,68 @@ jobs:
1010
runs-on: ubuntu-latest
1111

1212
steps:
13-
# Step 1: Check if the command is `/deploy-pre`
13+
# Step 1: Verify Command in Comment
1414
- name: Check for `/deploy-pre` Command
1515
id: check_command
16-
uses: peter-evans/slash-command-action@v4
17-
with:
18-
token: ${{ secrets.GITHUB_TOKEN }}
19-
command: /deploy-pre
16+
run: |
17+
COMMENT_BODY="${{ github.event.comment.body }}"
18+
echo "Comment received: $COMMENT_BODY"
19+
if [[ "$COMMENT_BODY" == "/deploy-pre" ]]; then
20+
echo "Command `/deploy-pre` found."
21+
echo "valid=true" >> $GITHUB_OUTPUT
22+
else
23+
echo "No valid command found."
24+
echo "valid=false" >> $GITHUB_OUTPUT
25+
fi
2026
21-
# Step 2: Extract Branch Reference from PR
22-
- name: Get Pull Request Details
23-
if: steps.check_command.outputs.command == 'true'
27+
# Step 2: Extract PR Branch
28+
- name: Extract PR Branch
29+
if: steps.check_command.outputs.valid == 'true'
2430
id: pr_details
31+
run: |
32+
PR_NUMBER="${{ github.event.issue.number }}"
33+
REPO_OWNER="${{ github.repository_owner }}"
34+
REPO_NAME="${{ github.event.repository.name }}"
35+
36+
echo "Fetching PR details for PR #$PR_NUMBER"
37+
38+
# Use GitHub REST API to fetch the PR details
39+
PR_DETAILS=$(curl -s \
40+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
41+
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER")
42+
# Extract the branch name (head.ref)
43+
BRANCH=$(echo "$PR_DETAILS" | jq -r '.head.ref')
44+
45+
# Output the branch name
46+
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
47+
48+
# Step 3: Trigger Deploy Workflow
49+
- name: Trigger Deploy Workflow
50+
if: steps.check_command.outputs.valid == 'true'
2551
uses: actions/github-script@v6
2652
with:
2753
script: |
28-
const pr = context.payload.issue.pull_request;
29-
if (!pr) {
30-
throw new Error("The /deploy-pre command must be used in a pull request comment.");
31-
}
32-
return pr.head.ref;
33-
result-encoding: string
54+
const branch = `${{ steps.pr_details.outputs.branch }}`;
55+
console.log("Triggering deploy-pre.yml for branch:", branch);
56+
github.rest.actions.createWorkflowDispatch({
57+
owner: context.repo.owner,
58+
repo: context.repo.repo,
59+
workflow_id: "deploy-pre.yml",
60+
ref: branch,
61+
});
3462
35-
# Step 3: Trigger Deploy Workflow for the PR Branch
36-
- name: Trigger Deploy Workflow
37-
if: steps.check_command.outputs.command == 'true'
63+
# Step 4: Post Confirmation Comment
64+
- name: Post Comment to PR
65+
if: steps.check_command.outputs.valid == 'true'
3866
uses: actions/github-script@v6
3967
with:
4068
script: |
41-
github.rest.actions.createWorkflowDispatch({
69+
const branch = `${{ steps.pr_details.outputs.branch }}`;
70+
const prNumber = context.payload.issue.number;
71+
console.log(`Commenting back on PR #${prNumber}`);
72+
github.rest.issues.createComment({
4273
owner: context.repo.owner,
4374
repo: context.repo.repo,
44-
workflow_id: "deploy-pre.yml", # Replace with your workflow filename
45-
ref: "${{ steps.pr_details.outputs.result }}"
46-
})
75+
issue_number: prNumber,
76+
body: `✅ Workflow \`deploy-pre.yml\` has been triggered for branch \`${branch}\`.`,
77+
});

.github/workflows/deploy-pre.yml

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,50 @@ jobs:
88
runs-on: ubuntu-latest
99

1010
steps:
11+
# Step 1: Checkout the code
1112
- name: Checkout Code
12-
uses: actions/checkout@v3
13+
uses: actions/checkout@v4
1314

14-
- name: Set up Docker
15-
uses: docker/setup-buildx-action@v2
16-
17-
- name: Build Docker Image
15+
# Step 2: Build the Docker image using Docker Compose
16+
- name: Build Docker Image with Docker Compose
1817
run: |
19-
docker build -t api-gateway:latest ./docker
18+
docker compose -f ./docker/docker-compose.yml build
2019
21-
- name: Save Docker Image
20+
# Step 3: Save and compress the Docker image
21+
- name: Save and Compress Docker Image
2222
run: |
23-
docker save api-gateway:latest | gzip > api-gateway.tar.gz
23+
docker save api-gateway:latest > api-gateway.tar.gz
2424
25-
- name: Copy Docker Image and Compose File to Droplet
26-
uses: appleboy/[email protected]
25+
# Step 4: Transfer compressed Docker image to the remote server
26+
- name: Transfer Docker Image to Droplet
27+
uses: appleboy/[email protected]
2728
with:
2829
host: ${{ secrets.SSH_HOST }}
29-
username: ${{ secrets.SSH_USER }}
30+
username: ${{ secrets.SSH_USERNAME }}
3031
key: ${{ secrets.SSH_KEY }}
31-
source: |
32-
./api-gateway.tar.gz
33-
./docker-compose.yml
34-
target: /opt/api-gateway/
32+
source: api-gateway.tar.gz
33+
target: /opt/summoners-sync/api-gateway
3534

36-
- name: Deploy with Docker Compose
35+
# Step 5: Load and run the Docker image on the remote server
36+
- name: Deploy Docker Image on Remote
3737
uses: appleboy/[email protected]
3838
with:
3939
host: ${{ secrets.SSH_HOST }}
4040
username: ${{ secrets.SSH_USERNAME }}
4141
key: ${{ secrets.SSH_KEY }}
4242
script: |
43-
cd /opt/api-gateway
43+
set -e
4444
45-
# Stop and remove old services
46-
docker-compose down || true
45+
# Navigate to deployment directory
46+
cd /opt/summoners-sync/api-gateway
47+
48+
# Stop and remove existing container if it exists
49+
docker stop api-gateway || true
50+
docker rm api-gateway || true
51+
docker rmi api-gateway || true
4752
48-
# Load the new Docker image
49-
gunzip -c api-gateway.tar.gz | docker load
53+
# Load the Docker image
54+
docker load < api-gateway.tar.gz
5055
51-
# Start the service with the new image
52-
docker-compose up -d
56+
# Run the new container
57+
docker run -d --name api-gateway -e SPRING_PROFILES_ACTIVE=dev --restart always api-gateway:latest

docker/Dockerfile

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
FROM maven:3.9.9-ibm-semeru-23-jammy AS build
33
WORKDIR /app
44

5-
COPY ../mvnw ./
6-
COPY ../.mvn .mvn
7-
COPY ../pom.xml ./
8-
COPY ../src/ ./src
5+
COPY mvnw ./
6+
COPY .mvn .mvn
7+
COPY pom.xml ./
8+
COPY src/ ./src
99

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

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

2322
# Expose the port the app runs on
2423
EXPOSE 8080
2524

2625
# Run Spring Boot app
27-
CMD ["java", "-jar", "app.jar"]
26+
CMD ["java", "-jar", "ag-summoners-sync.jar"]

docker/docker-compose.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
version: "3.8"
12
services:
23
api-gateway:
3-
container_name: api-gateway
44
build:
55
context: ..
66
dockerfile: docker/Dockerfile
7+
image: api-gateway:latest
78
ports:
89
- "8080:8080"
10+
networks:
11+
api_gateway_network:
12+
ipv4_address: 172.17.0.2
913
environment:
10-
SPRING_DATASOURCE_URL: jdbc:postgresql://database:5432/gatewaydb
11-
SPRING_DATASOURCE_USERNAME: admin
12-
SPRING_DATASOURCE_PASSWORD: admin
1314
SPRING_PROFILES_ACTIVE: dev
15+
networks:
16+
api_gateway_network:
17+
driver: bridge
18+
ipam:
19+
config:
20+
- subnet: 172.17.0.2/16
1421

0 commit comments

Comments
 (0)