Github Actions Basics
DevOps has become an important part of product testing development and delivery. To automate the process DevOps makes sure that this testing, development & delivery process goes structured and under a workflow which can be controlled and maintained very easily.
To do this CI ( Continuos Integration ) & CD ( Continuos Delivery ) is an important part. Developer writes codes & push them on the repo. After that CI CD does the rest. Depending on the branch like test dev stage prod etc as developer likes each branch contains different test and different deployments. To do this CI CD we need a platform to do this CI CD according to our instructions.
And such a platform is GitHub. Github actions does the job. There are also other platforms or tools that do this like
- Jenkins
- Travis CI
- Gitlab Auto Devops
- Bitbucket
etc. In this blog post i will talk about github actions and especially i will talk about deploying the code on the server
Github Actions
A tools that does the ci cd
- There is a .yml file that keeps the instructions for CI & CD
- Create the yml file inside the
.github/workflows/your-github-actions.yml
- The basic flow of github-actions or any other standard CI CD procedure
Commit code -> Test -> Build -> Deploy on Stage -> Deploy on Prod -> Publish
- You can do many things only just typing the instructions on the yml file
- Suppose you have a virtual machine or server which you access with you
pem
file. In that case you need to provide the credentials with the following syntax.name: Name of the workflow on: workflow_dispatch: jobs: deploy: runs-on: ubuntu-latest steps: - name: Name of the step uses: appleboy/ssh-action@master with: host: 3.1.80.58, 18.142.118.51 username: ubuntu key: $ script: | echo "Command goes here"
- This is just a sample workflow which only trigger on clicking the
run workflow
on actions tab. - Here
on
means the trigger action. - Here
jobs
means all the tasks that will be executed on the workflow. - Here
deploy
means tasks instructions - Here
runs-on
the OS that the workflow will run on - Here
step
the procedure that will be followed for the workflow - Here
uses
this is the vital part the workflow will the using this method to run the tasks means the workflow- Here we are using
appleboy/ssh-action@master
to run our jobs and the whole CI and CD
- Here we are using
- Here
host
means the servers for multiple servers just put the ips there by using comma - Here
username
&key
where you should put your server username and key ( pem ) file - And at last
script
is the part where you will put all the commands that will be executed on the server to deploy the application
Some sample yml files
- If you have username and password then use the following yml syntax
name: remote ssh command on: [push] jobs: build: name: Build runs-on: ubuntu-latest steps: - name: executing remote ssh commands using password uses: appleboy/ssh-action@master with: host: $ username: $ password: $ port: $ script: whoami
- Check the link for more details appleboy/ssh-action
Trigger workflow on postman request
- We can also trigger a github actions workflow using postman post request.
- To setup we just need to add the trigger event on
repository_dispatch
. - Sample code:
name: Trigger workflow on repository Dispatch
on:
repository_dispatch:
types: [build]
jobs:
run-sample-workflow:
runs-on: ubuntu-latest
steps:
- name: just a sample
run: echo "hello"
- Postman post url:
https://api.github.com/repos/username/repo-name/dispatches
- Body
{
"event_type": "build"
}
- 2 Headers ->
- Accept ->
application/vnd.github.everest-preview+json
- Content-Type ->
application/json
- We need to add a token as Bearer Token.
- Accept ->
- It is always a best practice to add all the ip, username, password, keys or any secret to github secrets.
- And after that you can add those secrets on the appropriate field with $.
Problems that i faced
- Well connecting to server is easy but after we log in than how would we recognize the server our private github repo?
- Well we could recognize the private repo but how? how do we skip the id and password filed for the repo?
- Everyone has there own methods to do this step.
- Let me show you my method.
- On git pull we need to make sure that our repo is authenticated and recognized by our server.
- So we can just send the username and password with the requested pull.
- So we can pass the username and password or the access token.
- Syntax
git pull https://token_name_you_saved_as:gph_sampletoken@github.com/username/repo.git
- On this type of pull request github will let you pull from your repo because of the credentials that you put with your git pull
- Well making the workflow was easy but getting this pull idea was a bit tough for me.
I’m posting this because of all the trouble that i faced during my workflows. I couldn’t pull the app as i was unable to pass the username and password.