In my post this time, I will share a little information about automation deploy to Server with Github Actions. Previously for those who don’t know what Automation Deploy is, Automation Deploy is a method to build an application automatically. This automation plays an important role in the development of an application, which will make development run faster, efficiently and also structured. Therefore I will make a simple tutorial on how Automation Deploy works.
Required :
- GitHub Account
- Project Repository on GitHub
- Server with SSH Access, Installed sshpass, Git and connected to Github
If you already have what you need, let’s jump straight into the tutorial :
1. Clone your project repository to your server.
The first thing you need to do is clone the repository onto your server.
# Enter your server with ssh # ssh -p [port] [user]@[yourIpServer] # Example ssh -p 22 root@192.168.1.1 # Cd to directory you want # Example cd /var/www/ # Clone your repository with git clone # git clone [your repo link] # Example git clone https://github.com/lukman134/sample.git
2. Create a Shell Script that will update your project.
These shell scripts will run automatically using triggers. The trigger I use is when the developer does a push or pull request on GitHub. Create a shellscripts file with the name deploy-sample.sh (example),
# cd to directory you want to place shellscript file # Example cd /usr/local/sbin/ # create shellscript file vim deploy-sample.sh
then enter the following command :
#!bin/bash cd /your/repository/project && git fetch origin main && git pull origin main echo "Deployment Finished"
3. Set your project’s environment variables on GitHub.
You need to create an environment variable in the project repository on your GitHub. This variable will be used by GitHub Actions to access your server.
- Log into your GitHub account,
- after that go to your project repository,
- go to settings > secrets.
- Inside these secrets, enter the important variables needed for the automation deploy to server. Enter the ssh host, ssh port, ssh user and ssh password from your server. Don’t worry, GitHub will encrypt the value of the variable you have created. So no one will know its contents except you..
4. Create Github Actions to configure automation deploy
Finally, we create a configuration to automate the deployment.
- Log into your GitHub account,
- after that go to your project repository,
- open Actions, then scroll to the Automation section,
- Select Manual Workflow, then click Configuration,
- Give the file name as you wish,
- Fill the configuration file with this :
# This is a basic workflow to help you get started with Actions name: CI # Controls when the action will run. on: # Triggers the workflow on push or pull request events but only for the main branch push: branches: [ main ] pull_request: branches: [ main ] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" build: # The type of runner that the job will run on runs-on: ubuntu-latest # Steps represent a sequence of tasks that will be executed as part of the job steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 # Runs a set of commands using the runners shell - name: Sample Automation Deploy run: | VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') which ssh-agent || ( yum install openssh-client -y ) eval $(ssh-agent -s) sshpass -p ${{ secrets.SSH_PASSWORD }} ssh -p ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }} "cd /usr/local/sbin && sh deploy-sample.sh"
Little explanation :
This configuration serves to set what actions you want if there is an update in the project repository. In this example I made, every time there is a push or pull request on the main branch, it will trigger GitHub Action to run the commands in the “#Runs a set of commands using the runners shell” section . The command I entered is the command to log into the server, then run the deploy-sample.sh shellscript that was created in step 2.
After the configuration is entered, then click Start Commit, click Commit New File then the configuration will run, you can see directly the process as in the example below :
Done, then every time there is a push or pull request to the main branch, Automation Deploy will run. So many tutorials about Automation Deploy to Server with GitHub Actions that I can share. Hopefully this article can be useful, Thank you 😀
Read also :
Install Jenkins and Nginx with Docker-compose