Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test GitHub token #63

Merged
merged 85 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
4274d32
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
7f04320
Update pr-deploy.yml
ThePrimeJnr Aug 2, 2024
53c5d9a
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
0561207
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
1e8d19e
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
16cee9a
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
24326af
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
d7f04d3
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
0692b8b
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
0c7271c
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
0083104
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
091a5c0
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
4ee8a8e
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
f6783e8
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
f41cef8
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
0170f85
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
3cc7468
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
f78448e
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
3bc8aae
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
030e693
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
ab56d38
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
22946e9
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
bcc9be5
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
1bab8cb
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
343c86c
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
2835f5b
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
5182e13
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
4da48e1
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
267dfc0
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
571691f
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
e0fe221
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
2a1885d
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
b4a1948
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
224b1d8
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
7cb6177
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
6018ed2
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
be7f836
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
50f3aeb
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
8b946c0
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
e9602a7
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
e98b72f
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
cff6a31
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
19f95ad
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
7c19dcb
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
7a3ac66
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
f9ef7f9
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
8a6c1d6
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
756757f
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
4af5c77
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
73f5d76
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
d9ddd0b
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
bc6b5e7
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
352113d
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
7dacd3f
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
fa708bb
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
2375a9e
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
a85f321
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
e586816
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
bca89d2
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
10f42d1
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
c7ce753
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
f82118b
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
9f7657e
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
e67e211
Update Dockerfile
ThePrimeJnr Aug 2, 2024
2ffa540
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
8d3bcbb
Update Dockerfile
ThePrimeJnr Aug 2, 2024
43d24bc
Update Dockerfile
ThePrimeJnr Aug 2, 2024
b8a339f
Update Dockerfile
ThePrimeJnr Aug 2, 2024
f33995a
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
46eba7e
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
4fec9b0
Update pr-deploy.yml
ThePrimeJnr Aug 2, 2024
6e205e9
Update pr-deploy.yml
ThePrimeJnr Aug 2, 2024
f52fef1
Update pr-deploy.yml
ThePrimeJnr Aug 2, 2024
bdcfbac
Update pr-deploy.yml
ThePrimeJnr Aug 2, 2024
8baa50c
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
458683e
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
a4ad43a
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
6ca375f
Update entrypoint.sh
ThePrimeJnr Aug 2, 2024
d8f4347
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
c58bf16
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
cf27ff3
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
1a772e9
Update pr-deploy.sh
ThePrimeJnr Aug 2, 2024
63c8b7c
Delete .github directory
ThePrimeJnr Aug 2, 2024
c6f63ea
feat: improved codebase
ThePrimeJnr Aug 3, 2024
71cc5ed
Merge branch 'main' into test-github-token
ThePrimeJnr Aug 3, 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
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ inputs:
context:
description: Directory in the repository where the Dockerfile or start command is located
required: false
default: "./"
dockerfile:
description: Path to the Dockerfile (optional)
required: false
default: "./Dockerfile"
exposed_port:
description: Port to expose in the container
Expand Down
116 changes: 17 additions & 99 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,22 @@
# Exit immediately if a command exits with a non-zero status.
set -e

function handle_error {
comment "Failed ❌" "#" && exit 1
}

# Set up trap to handle errors
trap 'handle_error' ERR

comment() {
local status_message=$1
local preview_url=$2

echo $status_message

local comment_body=$(jq -n --arg body "<strong>Here are the latest updates on your deployment.</strong> Explore the action and ⭐ star our project for more insights! 🔍
<table>
<thead>
<tr>
<th>Deployed By</th>
<th>Status</th>
<th>Preview URL</th>
<th>Updated At (UTC)</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href='https://github.com/marketplace/actions/pull-request-deploy'>PR Deploy</a></td>
<td>${status_message}</td>
<td><a href='${preview_url}'>Visit Preview</a></td>
<td>$(date +'%b %d, %Y %I:%M%p')</td>
</tr>
</tbody>
</table>" '{body: $body}')

if [ -z "$COMMENT_ID" ]; then
# Create a new comment
COMMENT_ID=$(curl -s -H "Authorization: token $GITHUB_TOKEN" -X POST \
-d "$comment_body" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues/${PR_NUMBER}/comments" | jq -r '.id')

elif [ "$COMMENT_ID" == "null" ]; then
# Create a new comment
COMMENT_ID=$(curl -s -H "Authorization: token $GITHUB_TOKEN" -X POST \
-d "$comment_body" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues/${PR_NUMBER}/comments" | jq -r '.id')
else
# Update an existing comment
curl -s -H "Authorization: token $GITHUB_TOKEN" -X PATCH \
-d "$comment_body" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues/comments/${COMMENT_ID}" > /dev/null
fi
}

REPO_ID=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME} | jq -r '.id')

# Checks if the action is opened
if [ "$PR_ACTION" == "opened" ]; then
comment "Deploying ⏳" "#"
fi

# Formats the environment variables into a comma-separated string
ENV_ARGS=$(echo "$ENVS" | tr '\n' ',' )

# Copy the pr-deploy.sh script to the remote server.
# Copy the script to the remote server.
sshpass -p "$SERVER_PASSWORD" scp -o StrictHostKeyChecking=no -P $SERVER_PORT pr-deploy.sh $SERVER_USERNAME@$SERVER_HOST:/srv/pr-deploy.sh

# Run the pr-deploy.sh script on the remote server and capture the output from the remote script
REMOTE_OUTPUT=$(sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p $SERVER_PORT $SERVER_USERNAME@$SERVER_HOST bash /srv/pr-deploy.sh $CONTEXT $DOCKERFILE $EXPOSED_PORT $REPO_URL $REPO_ID $GITHUB_HEAD_REF $PR_ACTION $PR_NUMBER "${ENV_ARGS}" $COMMENT_ID | tail -n 1)

# Ensure the output is valid JSON by escaping problematic characters
SANITIZED_OUTPUT=$(echo "$REMOTE_OUTPUT" | sed 's/[[:cntrl:]]//g')

# Parse the sanitized JSON
COMMENT_ID=$(echo "$SANITIZED_OUTPUT" | jq -r '.COMMENT_ID')
DEPLOYED_URL=$(echo "$SANITIZED_OUTPUT" | jq -r '.DEPLOYED_URL')

if [ "$COMMENT_ID" == "null" ]; then
# Checks if the action is opened
if [[ "$PR_ACTION" == "opened" || "$PR_ACTION" == "synchronize" || "$PR_ACTION" == "reopened" ]]; then
comment "Deploying ⏳" "#"
elif [ "$PR_ACTION" == "closed" ]; then
comment "Terminated 🛑" "#"
fi

# Run the pr-deploy.sh script on the remote server and capture the output from the remote script
NEW_REMOTE_OUTPUT=$(sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p $SERVER_PORT $SERVER_USERNAME@$SERVER_HOST bash /srv/pr-deploy.sh $CONTEXT $DOCKERFILE $EXPOSED_PORT $REPO_URL $REPO_ID $GITHUB_HEAD_REF $PR_ACTION $PR_NUMBER "${ENV_ARGS}" $COMMENT_ID | tail -n 1)
exit 0
fi

if [ -z "$DEPLOYED_URL" ]; then
if [ "$PR_ACTION" == "closed" ]; then
comment "Terminated 🛑" "#" && exit 0
fi
comment "Failed ❌" "#" && exit 1
fi
comment "Deployed 🎉" $DEPLOYED_URL
# Run the script on the remote server with environment variables.
sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p $SERVER_PORT $SERVER_USERNAME@$SERVER_HOST \
"GITHUB_TOKEN='$GITHUB_TOKEN' \
CONTEXT='$CONTEXT' \
DOCKERFILE='$DOCKERFILE' \
EXPOSED_PORT='$EXPOSED_PORT' \
ENVS='$ENVS' \
REPO_OWNER='$REPO_OWNER' \
REPO_NAME='$REPO_NAME' \
REPO_URL='$REPO_URL' \
REPO_ID='$REPO_ID' \
BRANCH='$GITHUB_HEAD_REF' \
PR_ACTION='$PR_ACTION' \
PR_NUMBER='$PR_NUMBER' \
COMMENT_ID='$COMMENT_ID' \
bash -c 'echo \"$SERVER_PASSWORD\" | sudo -SE bash /srv/pr-deploy.sh'"
158 changes: 83 additions & 75 deletions pr-deploy.sh
Original file line number Diff line number Diff line change
@@ -1,65 +1,103 @@
#!/bin/bash

set -e
trap 'comment "Failed ❌" "#"; exit 1' ERR

CONTEXT=$1
DOCKERFILE=$2
EXPOSED_PORT=$3
REPO_URL=$4
REPO_ID=$5
BRANCH=$6
PR_ACTION=$7
PR_NUMBER=$8
ENVS="${9}"
COMMENT_ID="${10}"
PR_ID="pr_${REPO_ID}${PR_NUMBER}"
# JSON file to store PIDs
DEPLOY_FOLDER="/srv/pr-deploy"
PID_FILE="/srv/pr-deploy/nohup.json"
COMMENT_ID_FILE="/srv/pr-deploy/comments.json"
DEPLOY_FOLDER="/srv/pr-deploy"

function handle_error {
echo "{\"COMMENT_ID\": \"$COMMENT_ID\", \"DEPLOYED_URL\": \"\"}"
exit 1
comment() {
local status_message=$1
local preview_url=$2
echo $status_message

local comment_body=$(jq -n --arg body "<strong>Here are the latest updates on your deployment.</strong> Explore the action and ⭐ star our project for more insights! 🔍
<table>
<thead>
<tr>
<th>Deployed By</th>
<th>Status</th>
<th>Preview URL</th>
<th>Updated At (UTC)</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href='https://github.com/marketplace/actions/pull-request-deploy'>PR Deploy</a></td>
<td>${status_message}</td>
<td><a href='${preview_url}'>Visit Preview</a></td>
<td>$(date +'%b %d, %Y %I:%M%p')</td>
</tr>
</tbody>
</table>" '{body: $body}')

if [ -z "$COMMENT_ID" ]; then
# Create a new comment
COMMENT_ID=$(curl -s -H "Authorization: token $GITHUB_TOKEN" -X POST \
-d "$comment_body" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues/${PR_NUMBER}/comments" | jq -r '.id')
jq --arg pr_id "$PR_ID" --arg cid "$COMMENT_ID" '.[$pr_id] = $cid' "$COMMENT_ID_FILE" > "${PID_FILE}.tmp" && mv "${PID_FILE}.tmp" "$COMMENT_ID_FILE"
else
# Update the existing comment
curl -s -H "Authorization: token $GITHUB_TOKEN" -X PATCH \
-d "$comment_body" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues/comments/${COMMENT_ID}" > /dev/null
fi
}

# This helps to kill the process created by nohup using the process id
function kill_process_with_pid() {
# serveo
local key=$1
ID=$(jq -r --arg key "$key" '.[$key]' "${PID_FILE}")
if [ -n $ID ]; then
kill -9 $ID
jq --arg key "$key" 'del(.[$key])' "${PID_FILE}" > tmp && mv tmp "${PID_FILE}"
cleanup() {
PID=$(jq -r --arg key "$PR_ID" '.[$key] // ""' "${PID_FILE}")

if [ -n "$PID" ]; then
kill -9 "$PID" || true
jq --arg key "$PR_ID" 'del(.[$key])' "${PID_FILE}" > "${PID_FILE}.tmp" && mv "${PID_FILE}.tmp" "${PID_FILE}"
fi

CONTAINER_ID=$(docker ps -aq --filter "name=${PR_ID}")
[ -n "$CONTAINER_ID" ] && sudo docker stop -t 0 "$CONTAINER_ID" && sudo docker rm -f "$CONTAINER_ID"

IMAGE_ID=$(docker images -q --filter "reference=${PR_ID}")
[ -n "$IMAGE_ID" ] && sudo docker rmi -f "$IMAGE_ID"
sleep 1
}

REPO_ID=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME} | jq -r '.id')

PR_ID="pr_${REPO_ID}${PR_NUMBER}"

# Setup directory
mkdir -p ${DEPLOY_FOLDER}/
mkdir -p ${DEPLOY_FOLDER}

# Initialize the JSON file for nohup if it doesn't exist
if [ ! -f "$PID_FILE" ]; then
echo "{}" > "$PID_FILE"
echo {} > $PID_FILE
fi

# Initialize the JSON file for comment if it doesn't exist
if [ ! -f "$COMMENT_ID_FILE" ]; then
echo "{}" > "$COMMENT_ID_FILE"
echo {} > $COMMENT_ID_FILE
fi

# Set up trap to handle errors
trap 'handle_error' ERR
# Handle COMMENT_ID
COMMENT_ID=$(jq -r --arg key $PR_ID '.[$key] // ""' ${COMMENT_ID_FILE})
comment "Deploying ⏳" "#"

# Ensure docker is installed
if [ ! command -v docker &> /dev/null ]; then
sudo apt-get update
sudo apt-get install docker.io -y
apt-get update
apt-get install docker.io -y
fi

# Ensure python is installed
if [ ! command -v python3 &> /dev/null ]; then
sudo apt-get update
sudo apt-get install python3 -y
apt-get update
apt-get install python3 -y
fi

# Free port
Expand All @@ -68,63 +106,33 @@ FREE_PORT=$(python3 -c 'import socket; s = socket.socket(); s.bind(("", 0)); pri
cd ${DEPLOY_FOLDER}
rm -rf $PR_ID

# Handle COMMENT_ID
if [ -n "$COMMENT_ID" ]; then
# echo "$COMMENT_ID" > "${PR_ID}.txt"
jq --arg pr_id "$PR_ID" --arg cid "$COMMENT_ID" '.[$pr_id] = $cid' "$COMMENT_ID_FILE" > tmp.$$.json && mv tmp.$$.json "$COMMENT_ID_FILE"
else
if [ -f "$COMMENT_ID_FILE" ]; then
COMMENT_ID=$(jq -r --arg key "$PR_ID" '.[$key]' "${COMMENT_ID_FILE}")

else
COMMENT_ID=""
fi
fi

# Get container and image IDs
CONTAINER_ID=$(docker ps -aq --filter "name=${PR_ID}")
IMAGE_ID=$(docker images -q --filter "reference=${PR_ID}")

# Handle different PR actions
case $PR_ACTION in
reopened | synchronize | closed)
# Stop and force remove containers if they exist
[ -n "$CONTAINER_ID" ] && sudo docker stop -t 0 $CONTAINER_ID && sudo docker rm -f $CONTAINER_ID

# Force remove images if they exist
[ -n "$IMAGE_ID" ] && sudo docker rmi -f $IMAGE_ID

# Exit early for 'closed' action
[ "$PR_ACTION" == "closed" ] && echo "{\"COMMENT_ID\": \"$COMMENT_ID\", \"DEPLOYED_URL\": \"\"}" && kill_process_with_pid $PR_ID && exit 0
cleanup
[ "$PR_ACTION" == "closed" ] && comment "Terminated 🛑" "#" && exit 0
;;
esac

# Git clone and Docker operations
echo "Git Clone ..."
git clone -b $BRANCH $REPO_URL $PR_ID
cd $PR_ID

echo "Building docker image..."
sudo docker build -t $PR_ID -f $DOCKERFILE .
cd $PR_ID/$CONTEXT

echo "Running docker container..."
echo "$ENVS" | tr ',' '\n' > .env
sudo docker run -d --env-file .env -p $FREE_PORT:$EXPOSED_PORT --name $PR_ID $PR_ID
# Build and run Docker Container
docker build -t $PR_ID -f $DOCKERFILE .
echo $ENVS > ${PR_ID}.env
docker run -d --env-file ${PR_ID}.env -p $FREE_PORT:$EXPOSED_PORT --name $PR_ID $PR_ID

echo "Start SSH session..."
# Start SSH Tunnel
nohup ssh -tt -o StrictHostKeyChecking=no -R 80:localhost:$FREE_PORT serveo.net > serveo_output.log 2>&1 &
SERVEO_PID=$!
sleep 3

# Check if Serveo tunnel was set up successfully
DEPLOYED_URL=$(grep "Forwarding HTTP traffic from" serveo_output.log | tail -n 1 | awk '{print $5}')

# update the nohup ids
if [ -n DEPLOYED_URL ]; then
# jq --arg pid "$SERVEO_PID" '.serveo = $pid' "$PID_FILE" > tmp.$$.json && mv tmp.$$.json "$PID_FILE"
jq --arg pr_id "$PR_ID" --arg pid "$SERVEO_PID" '.[$pr_id] = $pid' "$PID_FILE" > tmp.$$.json && mv tmp.$$.json "$PID_FILE"
jq --arg pr_id "$PR_ID" --arg pid "$SERVEO_PID" '.[$pr_id] = $pid' "$PID_FILE" > "${PID_FILE}.tmp" && mv "${PID_FILE}.tmp" "$PID_FILE"

if [ -z "$DEPLOYED_URL" ]; then
comment "Failed ❌" "#" && exit 1
fi

# Output the final JSON
echo "{\"COMMENT_ID\": \"$COMMENT_ID\", \"DEPLOYED_URL\": \"$DEPLOYED_URL\"}"
comment "Deployed 🎉" $DEPLOYED_URL