riazj.com

source code for this website
git clone https://riazj.com/git/riazj.com
Log | Files | Refs | LICENSE

efficiently-publish-to-website.html (4789B)


      1 <!DOCTYPE html>
      2 <html lang="en">
      3 <head>
      4 <meta charset="utf-8">
      5 <title>Efficiently Publish to a Website | Riaz's Website</title>
      6 <link rel="stylesheet" href="/style.css">
      7 <link rel="icon" href="data:,">
      8 <meta name="description" content="Alternatives to typing out the full path of the host for publishing to a website (upload, rsync, and git)">
      9 <meta name="viewport" content="width=device-width, initial-scale=1">
     10 </head>
     11 <body>
     12 <h1>Efficiently Publish to a Website</h1>
     13 <hr>
     14 <article>
     15 <p>Summary: <code>alias publish="rsync -rtvzP --filter=':- .gitignore' --exclude='.git*' ~/website_path/ user@hostname.com:/path/to/destination"</code></p>
     16 <h2>Abbreviated SCP</h2>
     17 <p>I used to run the following commands to upload files to this website:</p>
     18 <pre>
     19 scp file user@hostname.com:/home/public
     20 scp articles/article-name user@hostname.com:/home/public/articles</pre>
     21 <p>Typing out the path of the host is tedious, so I wrote a shell script to do it.</p>
     22 <pre>
     23 #!/bin/sh
     24 
     25 [ "$1" = "-a" ] &amp;& shift &amp;& scp "$@" user@hostname.com:/home/public/articles &amp;& exit
     26 scp "$@" user@hostname.com:/home/public</pre>
     27 <p><code>[ "$1" = "-a" ]</code> checks if the first argument is <code>-a</code>. <code>&amp;&</code> causes the following command to run only if the previous statement succeeded. <code>shift</code> removes the first argument. Then, the files (<code>$@</code>) are uploaded to the articles' folder. If the <code>-a</code> option is not present, the files will be uploaded to the public folder.</p>
     28 <p>Now, the process of uploading files looks like this:</p>
     29 <pre>
     30 upload file
     31 upload -a articles/article-name</pre>
     32 <h2>Rsync</h2>
     33 <p>The problem with that method is that the files need to be stated. To avoid this, rsync can be used.</p>
     34 <pre>rsync -rtvzP --filter=':- .gitignore' --exclude='.git*' ~/riazj.com/ user@hostname.com:/home/public</pre>
     35 <p>Those options mean the following:</p>
     36 <ul>
     37 <li>-r: recursively</li>
     38 <li>-t: transfer modification times, allowing unmodified files to be skipped</li>
     39 <li>-v: increase verbosity, showing the files uploaded</li>
     40 <li>-z: compress files for uploading</li>
     41 <li>-P: if a large file fails to be uploaded, continue from where it was instead of uploading its entirety</li>
     42 <li>--filter':- .gitignore': do not upload files that are listed in .gitignore</li>
     43 <li>--exclude='.git*': do not upload files or folders starting with ".git" in the name</li>
     44 </ul>
     45 <p>The slash after the folder name (<code>riazj.com/</code>) is there for uploading the contents of the folder and not the folder itself to the location. Without the slash, it would result in <code>/home/public/riazj.com</code>.</p>
     46 <h2>Git and SCP</h2>
     47 <p>I do not recommend using this method unless there is a reason to not use rsync. rsync is faster than this script. I wrote this before I decided to use rsync for my website.</p>
     48 <p>An <a href="https://docs.codeberg.org/security/ssh-key/">SSH key</a> to automatically sign in to the host is required.</p>
     49 <pre>
     50 #!/bin/sh
     51 
     52 changed_files="$(git diff-tree --no-commit-id --name-only origin/main..HEAD -r)"
     53 
     54 for file in "$changed_files"; do
     55   [ -e "$file" ] || { ssh user@hostname.com "rm -f $file"; continue; }
     56   [ "${file#*articles/}" != "$file" ] &amp;& scp "$file" user@hostname.com:/home/public/articles &amp;& continue
     57   scp "$file" user@hostname.com:/home/public
     58 done
     59 
     60 git push</pre>
     61 <p>This script is meant to be run before <code>git push</code> so that it can see the files that were changed in the local commits.</p>
     62 <p><code>[ -e "$file" ]</code> checks if the file exists. If it does not, it is removed. <code>continue</code> skips to the next iteration of the loop.</p>
     63 <p><code>[ "${file#*articles/}" != "$file" ]</code> checks if the file's path is not the same after trying to remove the string "articles/" from the start of it. If it is not the same as before, it must mean that the file path starts with "articles/". The file is then uploaded to the articles' folder.</p>
     64 <p>If the previous commands did not run, the file is uploaded to the public folder.</p>
     65 <p>This script does not account for removing an old file that has been renamed. It can be expanded on by using <code>git diff-tree --no-commit-id --name-status origin/main..HEAD -r</code> and grep to find the renamed files.</p>
     66 <h2>Update: 2025 Feb 25th</h2>
     67 <p>I added the following to <code>.ssh/config</code>, allowing me to run <code>ssh riazj</code> or <code>rsync file riazj:</code>.</p>
     68 <pre>
     69 Host riazj
     70   Hostname nfsnssh.com
     71   User riazpy_riazj
     72   Port 22</pre>
     73 <h2>Update: 2025 Jul 2nd</h2>
     74 <p>I added the rsync command to <code>.git/hooks/pre-push</code> so that I can publish to the website with <code>git push</code> instead of using a script to run both commands.</p>
     75 </article>
     76 <footer>
     77 <hr>
     78 <a href="/">Home Page</a></footer>
     79 </body>
     80 </html>