Installing, setting up, and managing your own continuous integration (CI) and continuous deployment tool (CD) is not an easy task. Luckily you can avoid that by using GitHub Actions. In this post, you will find a brief tutorial of how to easily add Actions to your existing GitHub repository.
GitHub Actions allow you to build, test, and even deploy your code to your servers. You only need to define the workflow creating a new YAML file in your repository under the /.github/workflows/ path using the workflow syntax, that’s all.
Workflows are triggered by common events (push, pull requests, etc.). If any of those events happen then GitHub will run your workflow automatically. And if you need to run an action periodically the “schedule” event allows you to run scheduled actions using the Cron syntax.
Every workflow has a list of jobs. And every job has a list of steps. Every step can either be a command to be run or the name of an action to use.
Usually workflows have one job, but you can define more than one job and all of them will run in parallel.
This is an example of a “Laravel” workflow. This workflow runs on the master branch after pushing to GitHub or when accepting a Pull Request. It has one job which runs on a Ubuntu virtual machine.
The first step uses the Checkout Action that checks-out the repository. The next steps just run some commands to prepare the framework environment and run the PHPUnit tests.
The path of this file in the repository would be /.github/workflows/laravel.yml
name: Laravel on: push: branches: [ master ] pull_request: branches: [ master ] jobs: laravel-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Copy .env run: php -r "file_exists('.env') || copy('.env.example', '.env');" - name: Install Dependencies run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist - name: Generate key run: php artisan key:generate - name: Directory Permissions run: chmod -R 777 storage bootstrap/cache - name: Create Database run: | mkdir -p database touch database/database.sqlite - name: Execute tests (Unit and Feature tests) via PHPUnit env: DB_CONNECTION: sqlite DB_DATABASE: database/database.sqlite run: vendor/bin/phpunit
If everything works then your build will succeed and you will see an output like this in the “Actions” tab of your repository.
Just click on the triangle icon over any action to expand the output and see the result.
Checkout is the most basic Action. You can view all the available actions in the GitHub Actions Marketplace. Examples of some useful Actions are:
The best thing about Actions is that if you can’t find an Action that suits your needs you can create your own action.
GitHub Actions are run on a virtual machine with the following resources:
- 2 Core CPU
- 7 GB of RAM
- 14 GB of SSD
In every job definition, the “runs-on” statement sets the virtual environment where the job will be run. These are the currently available virtual environments:
- Windows Server 2019 (windows-2019)
- Ubuntu 20.04 (ubuntu-20.04)
- Ubuntu 18.04 (ubuntu-18.04)
- Ubuntu 16.04 (ubuntu-16.04)
- macOS Catalina 10.15 (macos-10.15)
Actions commands are run on that environment, so you can do almost anything you could do on you local machine:
- Install software dependencies (using apt-get)
- Build and Push Docker images
- Run commands inside Docker images
GitHub Actions are free for public repositories. For private repositories, you get 2,000 minutes every month for free on the Free plan. If you have the Pro plan or a Team plan you get 3,000 minutes every month.
If at any moment you need more minutes their billing model allows you to purchase additional minutes:
- $0.008/minute for Linux builds
- $0.08/minute for macOS builds
- $0.016/minute for Windows builds
Try Actions now
Now that you have enough information you can add Actions to any of your repositories. You will see how fast and easy is to start using this tool from GitHub and how many advantages you get when you share the same Workflow with your teammates.