CI/CD Deployment with GitHub Actions and Private Servers
==============================================================
Deployment Architecture
In this technical guide, we will explore implementing a CI/CD deployment using GitHub Actions and private servers. This process will automate the building, testing, and deployment of applications in a production environment.
Prerequisites
A GitHub account with an existing repository
A private server with SSH access
Access to the server console
Basic knowledge of Git and GitHub Actions
Setting up GitHub Actions
Create a Workflow
1. Sign in to your GitHub account and navigate to your repository.
2. Click
Settings >
Actions >
New workflow.
3. Select
Create a new workflow from scratch.
4. Define the name and description of your workflow.
Workflow Configuration
yml
name: CI/CD Deployment
on:
push:
branches:
-main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Compile code
run: npm run build
- name: Tests
run: npm run test
- name: Deployment
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.PRIVATE_SERVER }}
username: ${{ secrets.SERVER_USER }}
private-key: ${{ secrets.PRIVATE_KEY }}
script: |
cd /var/www/html
git pull origin main
npm install
npm run build
npm start
Environment Variables
PRIVATE_SERVER: IP address or name of the private server.
SERVER_USER: Username to access the server.
PRIVADA_KEY: Private key to access the server.
SSH Configuration
1. Create an SSH configuration file in your repository with the name .ssh/config.
2. Add the following configuration:bash
PRIVATE_SERVER Host
HostName PRIVATE_SERVER
User SERVER_USER
IdentityFile ~/.ssh/PRIVATE_KEY
3. Create a private key file on your local machine with the name PRIVADA_KEY.
Private Server Configuration
Install SSH
1. Connect to your private server using SSH.
2. Install SSH if it is not installed:bash
sudo apt-get update
sudo apt-get install openssh-server
3. Restart the SSH service:bash
sudo service ssh restart
SSH Access Configuration
1. Create a new user group on the server:bash
sudo groupadd deployment_group
2. Add the user to the group:bash
sudo usermod -aG deployment_group SERVER_USER
3. Grant write permissions to the group on the deployment directory:bash
sudo chown -R SERVER_USER:deployment_group /var/www/html
Automated Deployment
1. Save changes to your workflow.
2. Verify that the workflow runs correctly.
3. The deployment will be done automatically every time a push is made to the main branch.
Security
Make sure the private key is encrypted with a password.
Limits access to the server through IP or user group.
Uses an access token for deployment.
Common Problems
Verify that the private key is configured correctly in the .ssh/config file.
Make sure the server is configured to allow SSH connections.
Verify that the workflow is configured correctly on GitHub.
References
GitHub Actions
SSH
Private Servers