update from sparkleup

This commit is contained in:
Madison Scott-Clary 2022-06-28 10:50:06 -07:00
parent b3641cc4d1
commit 6414531977
1 changed files with 29 additions and 26 deletions

View File

@ -1,4 +1,4 @@
# How To Deploy Projects from Gitea Using Git Hooks
# How To Deploy a Static Website from Gitea Using Git Hooks
### Introduction
@ -16,7 +16,10 @@ For this tutorial, we will be looking at using the `post-receive` hook. A common
Gitea is a lightweight and flexible source code management system that will be used for the examples here, so before beginning this tutorial, you should have the following:
* An Ubuntu 20.04 server with a non-root user configured with `sudo` privileges as described in the [Initial server setup for Ubuntu 20.94](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04) tutorial.
* An Ubuntu 20.04 server with a non-root user configured with `sudo` privileges as described in the [Initial server setup for Ubuntu 20.04](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04) tutorial.
* Docker installed on your server as described by **Steps 1 and 2** of our guide on [How to Install Docker on Ubuntu 20.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04).
* Docker Compose installed on your server. Follow **Step 1** of our guide on [How to Install and Use Docker Compose on Ubuntu 20.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04#step-1-installing-docker-compose).
* A domain name pointed at your server. If you are using a DigitalOcean Droplet, you can accomplish this by following our [Domains and DNS](https://docs.digitalocean.com/products/networking/dns/) documentation. This tutorial will use <^>your_domain<^> in examples throughout.
* Gitea installed on the server behind Nginx, ready to interact with using Git over SSH as described in [How To Install Gitea on Ubuntu Using Docker](https://www.digitalocean.com/community/tutorials/how-to-install-gitea-on-ubuntu-using-docker).
@ -26,16 +29,16 @@ To begin with, you will need a repository set up in Gitea. Create a new reposito
[![Creating a new repository](TODO)](TODO)
For this example, we'll be using the user `sammy`'s repository `static-sammy.com`, but in your case, use your own username and repository name. Now, wherever you plan on developing, check out that repository using `git clone`:
For this tutorial, you'll be setting up a static site at `static.<^>your_domain<^>`, but be sure to replace this with your final site's domain. Now, wherever you plan on developing, check out that repository using `git clone`:
```command
git clone git@<^>your_domain<^>:sammy/static-sammy.com
git clone git@<^>your_domain<^>:<^>username<^>/static.<^>your_domain<^>
```
Move into that directory using `cd`:
```command
cd static-sammy.com
cd static.<^>your_domain<^>
```
Now, create a file named `index.html` which will be served by Nginx. The following example uses `nano`. This file will contain a little bit of text that we can view in the browser.
@ -87,18 +90,18 @@ git push origin main
Now, let's set up Nginx to serve our static site. Create a new file in `/etc/nginx/sites-available` using your text editor:
```command
sudo nano /etc/nginx/sites-available/static-sammy
sudo nano /etc/nginx/sites-available/static.<^>your_domain<^>
```
In this file, enter the following:
```nginx
[label /etc/nginx/sites-available/static-sammy]
[label /etc/nginx/sites-available/static.<^>your_domain<^>]
server {
server_name static-sammy.com;
server_name static.<^>your_domain<^>;
listen 80;
root /var/www/static-sammy.com;
root /var/www/static.<^>your_domain<^>;
index index.html;
@ -112,14 +115,14 @@ server {
}
```
This `server` block instructs Nginx to listen on the standard HTTP port 80 for requests to `static-sammy.com`. When it receives a request, it tries to find a file matching that request in `/var/www/static-sammy.com`. It will first try looking for the file named (for instance, if you visit `static-sammy.com/shark.jpg`, it will try to find `/var/www/static-sammy.com/shark.jpg`). If that does not exist, it will append a `/` to the end of the file name to serve a directory or the file named `index.html`. Finally, if none of those work, it will serve a 404 Not Found error.
This `server` block instructs Nginx to listen on the standard HTTP port 80 for requests to `static.<^>your_domain<^>`. When it receives a request, it tries to find a file matching that request in `/var/www/static.<^>your_domain<^>`. It will first try looking for the file named (for instance, if you visit `static.<^>your_domain<^>/shark.jpg`, it will try to find `/var/www/static.<^>your_domain<^>/shark.jpg`). If that does not exist, it will append a `/` to the end of the file name to serve a directory or the file named `index.html`. Finally, if none of those work, it will serve a 404 Not Found error.
We also don't want anyone accessing our `.git` directory, so the next `location` block instructs Nginx to return a 404 should someone try.
Now, link this file from `sites-available` to `sites-enabled`:
```command
sudo ln -s /etc/nginx/sites-available/static-sammy /etc/nginx/sites-enabled/static-sammy
sudo ln -s /etc/nginx/sites-available/static.<^>your_domain<^> /etc/nginx/sites-enabled/static.<^>your_domain<^>
```
In order to make sure everything looks good, you can test the new configuration:
@ -151,26 +154,26 @@ cd /var/www
Make the directory you specified in your server block above:
```command
sudo mkdir static-sammy.com
sudo mkdir static.<^>your_domain<^>
```
You will need to change the permissions on this directory so that both you and the `git` user have write access.
```command
sudo chown <^>username<^>:git static-sammy.com
sudo chmod g+x static-sammy.com
sudo chown <^>username<^>:git static.<^>your_domain<^>
sudo chmod g+x static.<^>your_domain<^>
```
Move to that directory:
```command
cd static-sammy.com
cd static.<^>your_domain<^>
```
Now that you have the directory set up, you can clone the repository into it. Rather than using SSH, which would require a key, you can use the `file://` protocol for Git. This works because Nginx is running on the same server that the repository lives on. If the repository is public, you can also use the `https://` protocol.
```command
git clone file:///<^>path_to_gitea_installation<^>/repositories/sammy/static-sammy.com.git .
git clone file:///<^>path_to_gitea_installation<^>/repositories/sammy/static.<^>your_domain<^>.git .
```
It's important to remember to add that `.` at the end of the `git clone` command, as this tells Git to check out the repository into the current directory rather than making a new one. Now, if you type `ls`, you should see the file `index.html` from your repository, and if you visit the domain you set up, you should see your page rendered.
@ -186,7 +189,7 @@ Now that Nginx is serving the site from our checked out repository, we can set u
Git hooks for Gitea live in the repository that Gitea serves from. These are located in the data directory, predictably within the `repositories` subdirectory. Move to your data directory using `cd`, then move to your repository's `hooks` directory:
```command
cd git/repositories/sammy/static-sammy.com.git/hooks
cd git/repositories/<^>username<^>/static.<^>your_domain<^>.git/hooks
```
If you list the files in this directory, you will see a whole host of sample scripts, plus a few directories:
@ -234,7 +237,7 @@ This file can be any executable script. In our case, since we'll just be running
set -eux
git --git-dir=/var/www/static-sammy.com/.git --work-tree=/var/www/static-sammy.com/ pull --ff-only
git --git-dir=/var/www/static.<^>your_domain<^>/.git --work-tree=/var/www/static.<^>your_domain<^>/ pull --ff-only
```
Let's break this script down into its parts to understand what's happening:
@ -257,7 +260,7 @@ If Gitea is running in a Docker container --- if, for instance, you deployed it
[label docker-compose.yml]
volumes:
...
<^>- /var/www/static-sammy.com:/var/www/static-sammy.com<^>
<^>- /var/www/static.<^>your_domain<^>:/var/www/static.<^>your_domain<^><^>
```
This will mount that directory as a volume that the container can access. You will need to restart your Gitea containers:
@ -270,14 +273,14 @@ docker-compose up
Next, you will need to change the remote URL for the repository to the proper location for within the Docker container.
```command
git remote set-url origin file:///data/git/repositories/sammy/static-sammy.com.git
git remote set-url origin file:///data/git/repositories/sammy/static.<^>your_domain<^>.git
```
Here, we've changed the original URL we used to check out the branch from `/<^>path_to_gitea_installation<^>/repositories/sammy/static-sammy.com.git` to use `/data/git/` as the path to the installation, as that is how the volume is mapped in `docker-compose.yml`. As always, be sure to use the username and repository name appropriate to your project.
Here, we've changed the original URL we used to check out the branch from `/<^>path_to_gitea_installation<^>/repositories/sammy/static.<^>your_domain<^>.git` to use `/data/git/` as the path to the installation, as that is how the volume is mapped in `docker-compose.yml`. As always, be sure to use the username and repository name appropriate to your project.
### Testing the `post-receive` Hook
Now, after Gitea receives a new commit and it's written to disk, it will run this script to update the branch checked out in `/var/www/static-sammy.com`. To test this out, change the `index.html` file on your local working directory to include a congratulations message. For example, change the file so that it resembles this:
Now, after Gitea receives a new commit and it's written to disk, it will run this script to update the branch checked out in `/var/www/static.<^>your_domain<^>`. To test this out, change the `index.html` file on your local working directory to include a congratulations message. For example, change the file so that it resembles this:
```html
[label index.html]
@ -314,15 +317,15 @@ Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 312 bytes | 312.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: + cd /var/www/static-sammy.com
remote: + git --git-dir=/var/www/static-sammy.com/.git pull --ff-only
remote: From file:///data/git/repositories/sammy/static-sammy.com
remote: + cd /var/www/static.<^>your_domain<^>
remote: + git --git-dir=/var/www/static.<^>your_domain<^>/.git pull --ff-only
remote: From file:///data/git/repositories/<^>username<^>/static.<^>your_domain<^>
remote: 28c52bf..95cde6f main -> origin/main
remote: Updating 28c52bf..95cde6f
remote: Fast-forward
remote: . Processing 1 references
remote: Processed 1 references in total
To git.static-sammy.com:sammy/static-sammy.com.git
To <^>your_domain<^>:<^>username<^>/static.<^>your_domain<^>.git
8eed10f..95cde6f main -> main
```