Creating Continuous Integration and Continuous Delivery with GitHub Actions
Although there are a number of third party actions to be found, I have
decided not to use them.
For this I used a private server from DigitalOcean and have my app running on this server
So DON'T forget to push the necessary content from your local machine to the repository on git, and clone this repository at the erver side. So first setup the server side to access your repository, just like you do with your local machine.
When all of this is up and running, follow the steps I made.
Step 1: Generate an SSH Key
Do this on the private server.
SSH into the server with the following command. (You should already know this!)
When you are in the server cd into the .shh folder, where we create the SSH key.
Create the SSH key using the following command.
ssh-keygen -t ed25519 -C "email@example.com"
After that we will need to name the SSH Key file. I used the name
github-actions so I know this key is used for Github Actions.
You are also be asked to provide a passphrase. Just leave this empty, because you can't enter passwords when Github Actions run the SSH command.
If you use the ls command, you should see your key in the .ssh folder. The public key has a .pub extension while the private key does not.
Step 2: Adding the Public Key to authorized_keys
We need to add the public key github-actions.pub to the private
servers authorized_keys so machines using the private key
github-actions can access the server.
We can easily do that using the cat command to append github-actions.pub to authorized_keys.
cat github-actions.pub >> ~/.ssh/authorized_keys
This command grabs the content of github-actions.pub with cat end appends it to authorized_keys with >>. Be sure to use >> and not > because a single bracket will overwrite the authorized_keys file.
Step 3: Adding the private key to your repository's secrets
Go to your repository on Github and click on Settings then
Secrets and variables and click Actions. On the right
side you can add a new repository secret by clicking
New repository secret.
You have to specify a secret name and the contents. Later, in the Github Action workflow, you will use the secret name to get the contents. Write your secret name in uppercase and underscores as spaces. This is a format usually used for specifying secrets.
In this case I chose SSH_PRIVATE_KEY For the value we need to go back to the server and open github-actions using Nano.
Select evertything from -----BEGIN ..... to
.....PRIVATE KEY----- and copy. Then paste it in the secret value
at github and click Add secret.
You will find your new secret under the Repository secrets.
Step 4: Adding the hostname and the username to your repository's secrets
If we want to connect the Github repository with our server we will have
to create some more secrets.
One for the hostname, our servers ip address, and one for the username. Also we make secrets for the branch and the working directory.
Create a new secret at github.
For the hostname use SSH_HOST, in the value field enter the ip address of your server.
For the username use SSH_USER, in the value field enter your server username.
Create a secret for the workdirectory at the host, the directory where you cloned the repository to.
Here I used WORK_DIR, in the value field enter your workdirectory path. The last secret I used is MAIN_BRANCH, in the value field enter the branch you wish to track, usually main.
Step 5: Writing the Github workflow
Workflows are defined by a YAML file checked in to your repository and
will run when triggered by an event in your repository (such as a push
command), they can be triggered manually, or at a defined schedule.
They are defined in the .github/workflows as a your_new.yml file. Underneath I show you my pretty basic yaml file.
In this file I have written the necessary comments, but I leave the possibility for you to give your own interpretation. I will try not to repeat comments for lines which I have already explained.
In 5 steps we have created Continuous Integration and Continuous Delivery
with GitHub. Maybe this has given you some insights and learned something
Thanks for reading!