In today’s fast-paced world of software development, automation is the key to efficiency. GitHub Actions is a powerful tool that can help you automate your workflow, saving you time and reducing errors. In this article, I’ll dive into what GitHub Actions is, how it works, and how you can leverage it to supercharge your development process. I will touch on those aspects that are not the most obvious to beginners upon reading the short guide from GitHub.
This article will help those who want to set up automatic deployment of a project from codebase to their remote server using the free GitHub Actions service (the free tariff plan of course carries on some restrictions; for personal needs, it is definitely sufficient). More to say, this service can be used for free, even with a private repository. It doesn’t matter what programming language or stack you have: the main thing is to understand how GitHub Actions work. In the next article, I’ll show how to use it with the XM Cloud project.
What is GitHub Actions?
GitHub Actions is a feature provided by GitHub that enables you to automate various tasks in your software development workflow. It allows you to build, test, and deploy your code right from your GitHub repository, eliminating the need for external tools and services. GitHub Actions is based on the concept of “workflows,” which are customizable, automated sequences of steps that execute in response to events like code pushes, pull requests, and more.
Workflow files
The workflow file is central to this technology – it tells GitHub which actions to execute and how. On the Actions tab on the repository page based on your code, Github offers different workflow file templates, let’s start with Simple workflow.
After creating the your-wokflow-file.yml
file, you will have a folder in the repository with the file .github/workflows/your-wokflow-file.yml
. You can create multiple workflow files and execute them in any subsequence.
Workflow Syntax
GitHub Actions workflows are defined using YAML syntax. Here’s a basic example:
name: CI/CD Workflow on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Build and Test run: | npm install npm test
So, the idea is to execute processes with certain actions with the code from the branch you have chosen.
For example, deploy means we need to launch certain processes:
- get the latest code from a specified branch
- execute build
- deploy the build artifacts to the destination server or built containers into their registries
And if you write it taking into account the workflow file, in more detail this sequence will be as follows:
- GitHub creates a VM with the OS of your choice.
- Verifies your repository: its presence, git, required branches, and authorization.
- Copies your repository to this operating system after successful verification
- Runs code reviews and tests, if any
- Sends the changes you made to your repository to your remote server.
- Takes action to make your changes take effect on your site.
- Does some post-actions, such as warm-up or index rebuild
Typically, developers split code review, test running, and deployment into separate jobs to be run by separate runners. A runner in a given terminology is a server that runs one job.
Workflow Triggers
GitHub Actions supports a variety of triggers for your workflows. You can set them to run on events like pushes, pull requests, scheduled intervals, or even manual triggers. This flexibility allows you to tailor your automation to your specific needs.
Actions
The most curious line in the workflow file is the one with actions
and uses
. In the above workflow example, this is actions/checkout@v3. GitHub Actions has a rich ecosystem of pre-built actions available in the GitHub Marketplace. These actions cover a wide range of tasks, from deploying to cloud services like AWS or Azure to sending notifications on Slack. You can easily incorporate these actions into your workflows to save time and effort. You can also see in the source code what action does.
But it’s easier to look at the job’s execution page after you run it:
- copies variables inside the container
- checks the git version, creates the necessary folders, writes a configuration file
- checks the repository
- logs in
- copies the repository inside the container
- goes to the main branch
There are many actions created by developers that can be used by selecting the one you need on the GitHub Marketplace. For example, I used microsoft/setup-msbuild@v1.1
which builds my solution on a remote server.
Secrets and Secure Environment Variables
Security is paramount when dealing with sensitive information, such as API keys or access tokens. GitHub Actions provides a secure way to store and access secrets, which can be used as environment variables within your workflows, ensuring your sensitive data remains confidential.
Monitoring and Notifications
With GitHub Actions, you can set up notifications and monitoring for your workflows. Receive notifications about the status of your builds and deployments, and take action when something goes wrong.
VS Code Extension
GitHub has introduced the GitHub Actions plugin for Visual Studio Code, which allows you to view and edit workflows directly from the code editor. The plugin is currently available in public beta mode.
Initially, the project was developed by the community and was intended to monitor GitHub Actions. Now the company has released a tool for full editing of workflows from Visual Studio Code. The plugin leverages all the innovations of GitHub Actions.
First of all, the plugin was developed specifically for quick and convenient editing of CI processes. Therefore, developers have access to familiar functions for working with code and tools for tracking errors and monitoring failures. The GitHub Actions plugin includes the following features:
1. Syntax highlighting is available, allowing you to understand complex expressions and make it easier to navigate large projects:
2. Visual Studio Code provides editing tips based on the official GitHub Actions documentation, saving time
3. Syntax checking and error highlighting are provided. The plugin not only understands obvious errors but can also report incorrect links and out-of-order steps in workflows
4. Code autocompletion feature is available.
The GitHub Actions plugin can be downloaded from the official Visual Studio Marketplace website. If the user previously installed the community version, it will be automatically updated.
Conclusion
GitHub Actions is a game-changer for modern software development workflows. It streamlines and automates many tasks, allowing you to focus on what really matters: writing great code. Whether you’re a solo developer or part of a large team, GitHub Actions can help you improve your development process and deliver high-quality software faster.
This article provides theoretical insight into GitHub actions. In the following post, I am going to review the entire process of configuring GitHub Action for deploying Sitecore XM Cloud multisite and multi-environment solution – the approach I am using for automated CI/CD with Perficient XM Cloud JumpStart solution.