Automating All the Code & Testing Things with GitHub Actions

Rate this content
Bookmark

Code tasks like linting and testing are critical pieces of a developer’s workflow that help keep us sane like preventing syntax or style issues and hardening our core business logic. We’ll talk about how we can use GitHub Actions to automate these tasks and help keep our projects running smoothly.

This talk has been presented at React Advanced Conference 2021, check out the latest edition of this React Conference.

FAQ

Yes, GitHub Actions are highly customizable. Users can create custom actions using JavaScript or Docker to automate specific workflows unique to their development needs.

GitHub Actions are a CI/CD solution integrated directly into GitHub, allowing automation of code processes. They enable users to set up workflows for testing, linting, deploying, and more, using YAML configuration files.

GitHub Actions automate code tasks like linting and testing, reducing human error and ensuring consistent code quality. They also facilitate quick feedback by running tests and checks directly within pull requests.

Common tasks include code formatting, linting, testing, and deployments. Automation of these tasks ensures they are performed consistently and without human error, improving efficiency and reliability.

CI (Continuous Integration) in GitHub Actions refers to the process of automating the integration of code changes, ensuring that new code does not break the existing functionality. CD (Continuous Deployment/Delivery) automates the deployment of code to production environments, ensuring smooth and frequent releases.

GitHub Actions manage secrets, such as API keys or credentials, using encrypted secrets within the GitHub repository settings. These secrets can be accessed within workflows to safely handle sensitive information.

Examples include the AppleTools Eyes GitHub action for visual testing, Lighthouse CI for performance checks, and a content reminder action for scheduling reposts of content. These custom actions demonstrate the flexibility of GitHub Actions in addressing specific automated tasks.

Beginners can start with GitHub Actions by exploring the documentation available on GitHub, experimenting with simple workflow files, and utilizing pre-built actions from the GitHub Marketplace. Educational resources like online courses and tutorials can also be helpful.

Colby Fayock
Colby Fayock
19 min
25 Oct, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

We will learn how to automate code and testing with GitHub Actions, including linting, formatting, testing, and deployments. Automating deployments with scripts and Git hooks can help avoid mistakes. Popular CI-CD frameworks like Jenkins offer powerful orchestration but can be challenging to work with. GitHub Actions are flexible and approachable, allowing for environment setup, testing, deployment, and custom actions. A custom AppleTools Eyes GitHub action simplifies visual testing. Other examples include automating content reminders for sharing old content and tutorials.

1. Introduction to Code Automation and Testing

Short description:

We're going to learn how to automate code and testing with GitHub Actions. Linting helps pinpoint syntax errors and ensures consistent code style. Formatting tools like Prettier fix code issues automatically. Testing is essential for preventing errors and ensuring functionality. Regularly running tests is crucial. Deployments are necessary to make your application accessible to users.

Hey, everyone. We're going to learn how we can automate all the things, all the code and testing things with GitHub Actions. So who am I? I'm Colby Fayok. I'm the one hugging BB8 and Kylo Ren over there. I work with the dev community as a developer advocate for Applitools. You can find me pretty much anywhere on the web by just googling my name as I'm the only one in the world.

So we're going to just jump right into it. As developers, we have a bunch of tasks that we have to deal with day to day. And particularly, we're going to talk about code tasks. So let's see what some of those might look like.

Well, first there's linting. Linting basically tells you if you write sloppy code. But on a serious note, it can be really helpful for actually pinpointing syntax errors. It's also generally helpful for making sure that your code looks like the same person wrote it, which makes your code easy to read and also work through as a team. The problem is, unless you're running some kind of modern framework with it out of the box, you have to remember to actually run it.

Formatting is basically linting, but it will fix all those issues for you. Your linter like eslint might even do it itself. But with formatting, you have tools like Prettier, where they will format the code and they'll even include an opinionated configuration. Linting and formatting come with a lot of super practical benefits. You also benefit from not having to actually spend time in code reviews where you're going to argue over syntax preferences. You can let the robots be the bad cop and actually do it for you. There's less that you have to think about and less that you actually have to stress about. But again, your worst enemy here is remembering to actually run it.

Even more critical to a developer's workflow is testing, and while formatting and linting help prevent errors, it's more so static analysis, and it helps you find the syntax errors. But it's not doing a lot to actually test the functionality. Tests will help to make sure that you're not breaking things and end up losing money when your online store is down. The tests aren't just for your QA engineer to run every once in a while. You need to actually run these tests regularly, otherwise they're not really doing their job.

And finally, the last thing that we'll talk about is deployments. And ultimately, you want your application to actually go somewhere so that people can actually use it.

2. Automating Deployments with Scripts and Git Hooks

Short description:

Deployments can be complex and prone to human error. Automating the deployment process can help avoid mistakes. Scripts and Git hooks are useful for automation. Git hooks can trigger actions at different points in the Git process. For example, a precommit hook can automate formatting. Lint staged allows for targeted changes to be impacted by the precommit hook. However, running tests as a precommit hook can be inconvenient for developers.

Deployments can be painful, especially for large complex infrastructures where there's a lot of moving pieces. On top of this, you have to remember to deploy all those pieces and missing one step can take the site down in a bad way, not just because you're deploying a new version and have a little time between different migrations. A theme through all these tasks, though, are they're all important in one way or another. They help us focus on the real challenges of the work, not the drama in the code review or stumbling through another code deploy or, but another theme, they all require humans to do them.

That means they're prone to human error. And whether it's simply for getting to run something or forgetting to include a particular step, even for getting to do it in the right order, what can we do to actually avoid those human mistakes? Well we can automate it, of course. So let's learn how to automate all the things.

How do we actually automate all the things? If we're deploying anything even moderately complex, we have a bunch of things that we typically have to do in order to actually deploy our full stack. Those 10 commands or buttons that you have to run every time your Rails server and AWS infrastructure, well we can put that in a script. Scripts are probably the simplest form of automation. This helps us automate the manual tasks that we might have to take. So why not put them in a bash script? Or insert your favorite scripting language of choice, like Node, where you can even write it in JavaScript if that's your thing. The goal is to have a list of repeatable steps. And by using that one command with that script, you're able to do the exact same thing every single time. That way you never forget to leave anything out.

But scripts also ultimately rely on something to trigger that. If it's still a human, you might forget to run that script in the first place. Or if somebody's filling in for a day, maybe they don't even know that you have a script to run this setup. A feature that comes baked in to Git itself is Git hooks. With Git hooks, you can trigger something at different points of the Git process. For instance, you can setup a commit hook, where any time you actually run a commit, your hook will run, automating some part of your process, where a good example for that is formatting. Any time someone commits, you can setup Prettier, or any other formatter, to run before the commit actually applies. With your existing scripts to run linting and formatting, this is called a precommit, where, with tools like Husky, you can hook in and manage all those Git hooks. We're also applying lint staged here, which allows us to get the path of all those changes that we actually get in the stage for Git, where that way, when we run our precommit hook, we're not affecting the entire repository tree, we're only impacting the files that actually changed. But once that script finishes, those changes are added onto the Git stage before that commit is actually applied. So when the actual commit happens, all Git knows is that the code is pretty and formatted. All the developer knows is they got their job done, and when the code is actually pushed up to GitHub, it looks like that one person wrote it. But not everything makes sense inside of a Git hook. You don't want to run your tests as a precommit hook, for example, where that's going to be a huge pain for developers. Imagine you're working on a project with a huge test suite and in order to actually commit, you have to run that entire suite.