Skip to content

Commit ca693b5

Browse files
commit remaining changes
1 parent 0890ad8 commit ca693b5

File tree

3 files changed

+138
-39
lines changed

3 files changed

+138
-39
lines changed

README.md

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,95 @@
1-
# git-deploy-poc
2-
Deploy code to remote server
1+
# GoDaddy GitHub Action for Wordpress deployment
2+
3+
## Overview
4+
5+
This GitHub Action automates WordPress deployment using `rsync` over SSH. It creates a tar archive of modified files, transfers it to the remote server, and executes a deployment script. The action supports post-deployment commands, WordPress health checks, and automatic rollback in case of failures.
6+
7+
## Features
8+
9+
- **Deploy only changed files** using `rsync --checksum`
10+
- **Remove deleted files** from the repository on the server
11+
- **Execute post-deployment commands**
12+
- **Perform WordPress health checks** and rollback if necessary
13+
- **Secure authentication** via SSH private key
14+
15+
## Usage
16+
17+
### 1. **Add the Action to Your Workflow**
18+
19+
Create a `.github/workflows/deploy.yml` file in your repository:
20+
21+
```yaml
22+
name: Deploy WordPress
23+
24+
on:
25+
workflow_dispatch:
26+
inputs:
27+
deployment_dest:
28+
description: 'Target server directory, leave blank for root directory'
29+
required: false
30+
enable_health_check:
31+
description: 'Enable wordpress health check?'
32+
type: choice
33+
required: false
34+
default: "yes"
35+
options:
36+
- "yes"
37+
- "no"
38+
jobs:
39+
deploy:
40+
runs-on: ubuntu-latest
41+
steps:
42+
- name: Checkout repository
43+
uses: actions/checkout@v3
44+
45+
- name: Deploy using GitHub Action
46+
uses: your-org/your-action-repo@v1
47+
with:
48+
remote_host: ${{ secrets.REMOTE_HOST }}
49+
ssh_user: ${{ secrets.SSH_USER }}
50+
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
51+
deployment_dest: ${{ github.event.inputs.deployment_dest }}
52+
enable_health_check: ${{ github.event.inputs.enable_health_check }}
53+
```
54+
55+
## Inputs
56+
57+
| Name | Description | Required | Default |
58+
| ----------------------- | ------------------------------------ | -------- | ------- |
59+
| `remote_host` | The remote server IP or domain | ✅ Yes | - |
60+
| `ssh_user` | SSH username for authentication | ✅ Yes | - |
61+
| `ssh_private_key` | SSH private key for authentication | ✅ Yes | - |
62+
| `deployment_dest` | Remote WordPress directory | ❌ No | `.` |
63+
| `post_deploy_commands` | Commands to run after deployment | ❌ No | `''` |
64+
| `cleanup_deleted_files` | Remove deleted files from the server | ❌ No | `yes` |
65+
| `enable_health_check` | Perform a WordPress health check | ❌ No | `yes` |
66+
67+
## Requirements
68+
69+
- **Enable Git Deployment** for site from GoDaddy interface
70+
- **GitHub secrets configured** for `REMOTE_HOST`, `SSH_USER`, and `SSH_PRIVATE_KEY`
71+
72+
## Troubleshooting
73+
74+
### SSH Key Issues
75+
76+
Ensure the private key format is correct and matches the server's authorized keys:
77+
78+
```bash
79+
cat ~/.ssh/id_rsa | base64
80+
```
81+
82+
Set the output as `SSH_PRIVATE_KEY` in GitHub Secrets.
83+
84+
## License
85+
86+
This action is licensed under the MIT License.
87+
88+
## Contributing
89+
90+
Feel free to open issues or submit PRs for improvements!
91+
92+
## Support
93+
94+
For help, open an issue in the repository.
95+

action.yml

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,12 @@ inputs:
2626
default: ''
2727
cleanup_deleted_files:
2828
description: 'Delete files from the server that have been removed from the repository.'
29-
type: choice
3029
required: false
31-
default: true
32-
options:
33-
- "yes"
34-
- "no"
30+
default: "yes"
3531
enable_health_check:
3632
description: 'Check WordPress health after deployment and roll back if it fails. Disable to skip this step.'
37-
type: choice
3833
required: false
3934
default: "yes"
40-
options:
41-
- "yes"
42-
- "no"
4335

4436
runs:
4537
using: 'composite'
@@ -49,14 +41,16 @@ runs:
4941
shell: bash
5042
run: |
5143
sudo apt-get update -q && sudo apt-get install -y rsync sshpass
52-
53-
OUTPUT_DIR="output"
54-
SSH_ROOT_DIR="html"
55-
TEMP_KEY_FILE="private_key"
44+
45+
DEPLOYER_APP_DIR="html/git-deployer"
46+
WP_ROOT_DIR="html"
47+
UPLOAD_DIR="html/git-deployer-upload"
48+
5649
ACTION_DIR="action_dir"
50+
OUTPUT_DIR="output"
5751
IGNORE_FILES="$ACTION_DIR/files_to_ignore.txt"
5852
SYNC_FILES="$OUTPUT_DIR/files_to_sync.txt"
59-
REMOTE_DIR="$SSH_ROOT_DIR/deployer"
53+
TEMP_KEY_FILE="private_key"
6054
SSH_AUTH="-i $TEMP_KEY_FILE -o StrictHostKeyChecking=no"
6155
TAR_NAME="repository_$(openssl rand -hex 4).tar"
6256
@@ -70,6 +64,11 @@ runs:
7064
DEST_DIR="${DEST_DIR%/}" # Remove trailing slash
7165
echo "DEST_DIR=$DEST_DIR" >> $GITHUB_ENV
7266
67+
WP_DEST_DIR="$WP_ROOT_DIR/$DEST_DIR"
68+
WP_DEST_DIR="${WP_DEST_DIR#/}" # Remove leading slash
69+
WP_DEST_DIR="${WP_DEST_DIR%/}" # Remove trailing slash
70+
echo "WP_DEST_DIR=$WP_DEST_DIR" >> $GITHUB_ENV
71+
7372
ACTION_PATH="${{ github.action_path }}"
7473
TAG_NAME=$(basename "$ACTION_PATH")
7574
echo "ACTION_TAG=$TAG_NAME" >> $GITHUB_ENV
@@ -80,8 +79,9 @@ runs:
8079
echo "OUTPUT_DIR=$OUTPUT_DIR" >> $GITHUB_ENV
8180
echo "SYNC_FILES=$SYNC_FILES" >> $GITHUB_ENV
8281
echo "DEL_FILE_LIST=$CHECKOUT_DIR/deleted_files.txt" >> $GITHUB_ENV
83-
echo "REMOTE_DIR=$REMOTE_DIR" >> $GITHUB_ENV
84-
echo "SSH_ROOT_DIR=$SSH_ROOT_DIR" >> $GITHUB_ENV
82+
echo "UPLOAD_DIR=$UPLOAD_DIR" >> $GITHUB_ENV
83+
echo "DEPLOYER_APP_DIR=$DEPLOYER_APP_DIR" >> $GITHUB_ENV
84+
echo "WP_ROOT_DIR=$WP_ROOT_DIR" >> $GITHUB_ENV
8585
echo "SSH_USER=${{ inputs.ssh_user }}" >> $GITHUB_ENV
8686
echo "SSH_HOST=${{ inputs.remote_host }}" >> $GITHUB_ENV
8787
echo "SSH_AUTH=$SSH_AUTH" >> $GITHUB_ENV
@@ -101,10 +101,10 @@ runs:
101101
echo "The source directory does not exists."
102102
exit 1
103103
fi
104-
105-
if [[ "$DEST_DIR" != "." ]]; then
106-
if ! ssh "$SSH_AUTH" "$SSH_USER@$SSH_HOST" "[ -d '/$SSH_ROOT_DIR/$DEST_DIR' ]"; then
107-
echo "Error: Directory '/$SSH_ROOT_DIR/$DEST_DIR' does not exist on the server."
104+
105+
if [[ "$WP_DEST_DIR" != "$WP_ROOT_DIR" ]]; then
106+
if ! ssh "$SSH_AUTH" "$SSH_USER@$SSH_HOST" "[ -d '/$WP_DEST_DIR' ]"; then
107+
echo "Error: Directory '/$WP_DEST_DIR' does not exist on the server."
108108
exit 1
109109
fi
110110
fi
@@ -127,15 +127,15 @@ runs:
127127
echo "ignore list: $EXCLUDE_ARGS"
128128
echo "Preparing a changed file list..."
129129
echo "Source: $CHECKOUT_DIR/"
130-
echo "Destination: /$SSH_ROOT_DIR/$DEST_DIR/"
130+
echo "Destination: /$WP_DEST_DIR/"
131131
echo "current dir: $(pwd)"
132132
ls -lha
133133
echo "rsync command: "
134-
echo "rsync -av --dry-run --checksum -e ssh $SSH_AUTH $CHECKOUT_DIR/ $SSH_USER@$SSH_HOST:/$SSH_ROOT_DIR/$DEST_DIR/"
134+
echo "rsync -av --dry-run --checksum -e ssh $SSH_AUTH $CHECKOUT_DIR/ $SSH_USER@$SSH_HOST:/$WP_DEST_DIR/"
135135
136136
rsync -av --dry-run --checksum $EXCLUDE_ARGS \
137137
-e "ssh $SSH_AUTH" \
138-
$CHECKOUT_DIR/ "$SSH_USER@$SSH_HOST:/$SSH_ROOT_DIR/$DEST_DIR/" \
138+
$CHECKOUT_DIR/ "$SSH_USER@$SSH_HOST:/$WP_DEST_DIR/" \
139139
| grep -v '/$' > $SYNC_FILES
140140
141141
if [[ $? -ne 0 ]]; then
@@ -151,18 +151,17 @@ runs:
151151
if [ "$CLEAN_UP" = "yes" ]; then
152152
touch $DEL_FILE_LIST
153153
echo "Preparing a deleted file list..."
154-
echo "rsync -av --dry-run --delete --ignore-existing -e 'ssh $SSH_AUTH' $CHECKOUT_DIR/ '$SSH_USER@$SSH_HOST:/$SSH_ROOT_DIR/$DEST_DIR/' | grep '^deleting ' | awk '{print $2}' | grep -v '/$' > $DEL_FILE_LIST"
155-
154+
156155
rsync -av --dry-run --delete --ignore-existing $EXCLUDE_ARGS \
157156
-e "ssh $SSH_AUTH" \
158-
$CHECKOUT_DIR/ "$SSH_USER@$SSH_HOST:/$SSH_ROOT_DIR/$DEST_DIR/" \
157+
$CHECKOUT_DIR/ "$SSH_USER@$SSH_HOST:/$WP_DEST_DIR/" \
159158
| grep '^deleting ' | awk '{print $2}' | grep -v '/$' > $DEL_FILE_LIST
160-
159+
161160
if [[ $? -ne 0 ]]; then
162161
echo "Error: Failed to prepare the list of files to delete."
163162
exit 1
164163
fi
165-
164+
166165
echo $(basename "$DEL_FILE_LIST") >> "$SYNC_FILES"
167166
echo "Files to be DELETED:"
168167
cat "$DEL_FILE_LIST"
@@ -182,11 +181,11 @@ runs:
182181
- name: Upload tar file to server
183182
shell: bash
184183
run: |
185-
echo "Creating remote directory: $REMOTE_DIR"
186-
ssh $SSH_AUTH "$SSH_USER@$SSH_HOST" "mkdir -p '/$REMOTE_DIR' || exit 1"
184+
echo "Creating remote directory: $UPLOAD_DIR"
185+
ssh $SSH_AUTH "$SSH_USER@$SSH_HOST" "mkdir -p '/$UPLOAD_DIR' || exit 1"
187186
188-
echo "Uploading tar file to $REMOTE_DIR"
189-
rsync -avz -e "ssh $SSH_AUTH" $TAR_FILE "$SSH_USER@$SSH_HOST:/$REMOTE_DIR/"
187+
echo "Uploading tar file to $UPLOAD_DIR"
188+
rsync -avz -e "ssh $SSH_AUTH" $TAR_FILE "$SSH_USER@$SSH_HOST:/$UPLOAD_DIR/"
190189
echo "Tar file uploaded successfully."
191190
192191
- name: Clean up
@@ -202,9 +201,14 @@ runs:
202201
echo "Run Server deployer"
203202
POST_COMMAND=${{inputs.post_deploy_commands}}
204203
HEALTH_CHECK=${{inputs.enable_health_check}}
205-
DEPLOYER_SH="/$SSH_ROOT_DIR/deployer.sh"
204+
DEPLOYER_SH="php /$DEPLOYER_APP_DIR/bin/init mwp:deployer"
205+
206+
OPTIONS="--destDir=$DEST_DIR --postDeploymentCommand=$POST_COMMAND"
207+
if [ "$HEALTH_CHECK" == "no" ]; then
208+
OPTIONS="$OPTIONS --skipHealthCheck"
209+
fi
210+
211+
echo "SSH Command: ssh $SSH_AUTH "$SSH_USER@$SSH_HOST" $DEPLOYER_SH $TAR_NAME $OPTIONS";
212+
ssh $SSH_AUTH "$SSH_USER@$SSH_HOST" $DEPLOYER_SH $TAR_NAME $OPTIONS
206213
207-
ssh $SSH_AUTH "$SSH_USER@$SSH_HOST" \
208-
$DEPLOYER_SH $TAR_NAME $DEST_DIR $HEALTH_CHECK $POST_COMMAND
209-
210214
rm -f $TEMP_KEY_FILE

files_to_ignore.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ wp-admin/
2727
wp-includes/
2828
wp-content/mu-plugins/
2929
wp-content/object-cache.php
30-
wp-content/uploads/
30+
wp-content/uploads/
31+
git-deployer/
32+
git-deployer-upload/

0 commit comments

Comments
 (0)