diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..08a2773 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,6 @@ +########################################## +# code ownership +########################################## + +# default ownership: default owners for everything in the repo +* @zerodaycode/admins diff --git a/.github/workflows/deploy-pre-chatbot.yml b/.github/workflows/deploy-pre-chatbot.yml index 6437209..c0d20ff 100644 --- a/.github/workflows/deploy-pre-chatbot.yml +++ b/.github/workflows/deploy-pre-chatbot.yml @@ -1,4 +1,4 @@ -name: Handle Deploy Commands in PR Comments +name: Handle PRE env deployment commands in PR Comments on: issue_comment: @@ -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}\`.`, + }); diff --git a/.github/workflows/deploy-pre.yml b/.github/workflows/deploy-pre.yml index 0185fbf..c6e05ef 100644 --- a/.github/workflows/deploy-pre.yml +++ b/.github/workflows/deploy-pre.yml @@ -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/scp-action@v0.1.4 + # Step 4: Transfer compressed Docker image to the remote server + - name: Transfer Docker Image to Droplet + uses: appleboy/scp-action@v0.1.7 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/ssh-action@v0.1.5 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 diff --git a/docker/Dockerfile b/docker/Dockerfile index 8087574..ba3c172 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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 @@ -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"] \ No newline at end of file +CMD ["java", "-jar", "ag-summoners-sync.jar"] \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 77853b0..1e98358 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -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