So recently I have been working on one of my past projects Linkedin-RSS which I made here Making integration between RSS and LinkedIn and I have been working on adding Continuous Integration to it using GitHub Actions.
What is Continuous Integration?
Continuous Integration is a software development practice where automated builds and tests are run. By doing so, you can detect errors quickly, and locate them more easily.
What are GitHub Actions?
GitHub Actions is a feature that allows you to automate your workflow. You can create a workflow that is triggered by an event, such as a push or a pull request. You can also schedule a workflow to run at specific times or at regular intervals.
Github Actions can allow you to run certain scripts when a certain event happens. For example, you can run a script when a pull request is made or when a push is made to the repository to test the code or to build the code.
And that can be in any language you want, for example, Python, Javascript, or even C++ as every script is run in an Action Runner.
What is an Action Runner?
An Action Runner is a virtual machine that runs the scripts that you want to run. It is a virtual machine that is hosted by GitHub and it is used to run the scripts that you want to run.
What is a workflow?
A workflow is a configurable automated process made up of one or more jobs. Workflows are made up of your jobs and can be scheduled or triggered by an event.
You can also create your own Github Action but you can also use other people’s Github Actions from the Github Marketplace
How do you use Github Actions?
So first you need to have a github repository. If you need to create a repository you can follow this guide Creating a new repository
After you have created a repository you can go to the Actions tab and click on Set up a workflow yourself
After you click on that it will create a file called main.yml
in the .github/workflows
folder and it will open it in the editor.
Let’s make a simple workflow that will run a script when a push is made to the repository.
name: CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: echo "Hello World"
Ok, let me explain what this does.
So first we need to give the workflow a name.
name: CI
Then we need to specify when the workflow should be run. In this case, we want the workflow to run when a push is made to the main
branch.
on:
push:
branches: [ main ]
Then we need to specify what the workflow should do. In this case, we want the workflow to run a script.
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: echo "Hello World"
jobs
is used to specify what the workflow should do. In this case, we want the workflow to run a script.
runs-on
is used to specify the operating system that the script should run on. In this case, we want the script to run on the latest version of Ubuntu.
steps
is used to specify what the script should do. In this case, we want the script to run echo "Hello World"
.
uses
is used to specify what Github Action should be used. In this case, we want to use the checkout
Github Action.
run
is used to specify what the script should run. In this case, we want the script to run echo "Hello World"
.
So now we have a simple workflow that will run a script when a push is made to the repository.
If you want to also run it manually you can add this to the workflow.
on:
push:
branches: [ main ]
workflow_dispatch:
workflow_dispatch is used to run the workflow manually from the Actions tab.
And that is an example of how to use GitHub Actions.
How can we use Actions from the Marketplace?
Let’s say we have a repository that uses Node.js and we want to run a script when a push is made to the repository to download the dependencies and test the code.
So first we need to go to the Github Marketplace and search for the action that we want to use.
Then we need to click on the action that we want to use I used Setup-node for this example.
Setup-node is used to set up a Node.js environment.
Press the Use latest version
button.
And click the 2 Boxes to copy the workflow syntax.
Then we need to go to the .github/workflows
folder and open the main.yml
file.
Then we need to paste the workflow syntax that we copied from the Github Marketplace.
name: CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2.5.2
with:
node-version: 16
- run: npm install
- run: npm run build --if-present
- run: npm test
Now you also see that I added the with
and node-version
this is because the setup-node
action needs to know what version of Node.js to use so I added node-version: 16
to tell it to use Node.js version 16.
So now we have a workflow that will run npm tests and install npm dependencies. This would be mostly used for NodeJS projects.
So every time we got to push a commit to the repository it will run the script.
You can see the workflow running in the Actions tab as well.
How can I create my own GitHub Action?
And if you want to make your own GitHub Action you can find more information about that in the Creating Actions.
How can I use GitHub Actions in my project?
You can use GitHub Actions in your project by following the steps in the Using GitHub Actions.
How can I learn more about GitHub Actions?
You can find more information about GitHub Actions in the GitHub Actions Documentation
And if you want to learn more about how it works you can check Understanding Github Actions.
Github action is a great tool that can help you automate your workflow and make it a lot easier I have used it numerous times in my projects and I highly suggest taking a look at it.
Hope you enjoyed this project and Thanks so much for reading :D