Engineering
GitHub Actions FTW
A quick starter on why GitHub Actions can win the day - every day
GitHub Actions FTW
GitHub Actions is a powerful tool that can increase productivity for software engineers. It allows for automation of tasks such as continuous integration and deployment (CI/CD), which can save developers a lot of time and effort. GitHub Actions is easy to use and can be integrated with many different programming languages, including JavaScript.
In this blog post, we will discuss how GitHub Actions can increase productivity for software engineers using JavaScript. We will look at three different ways GitHub Actions can be used to automate tasks and increase efficiency.
Section 1: Continuous Integration with GitHub Actions
Continuous Integration (CI) is the practice of automatically building and testing code every time changes are made to a repository. This can help identify errors early in the development process, which can save time and effort in the long run. GitHub Actions can be used to implement CI in JavaScript projects.
To get started with GitHub Actions for CI, we need to create a workflow file in our repository. This file defines the steps that GitHub Actions will perform when triggered by an event, such as a push to the repository. Here is an example workflow file for a JavaScript project:
name: CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
This workflow file defines a job called build
that runs on the main
branch when a push event occurs. The runs-on
field specifies that the job should run on an Ubuntu virtual machine. The steps
field defines the steps that GitHub Actions will perform when the job is run.
The first step checks out the repository code using the actions/checkout
action. The second step installs the project dependencies using the npm install
command. Finally, the third step runs the project tests using the npm test
command.
With this workflow file in place, GitHub Actions will automatically build and test our JavaScript project every time changes are made to the repository. This can save a lot of time and effort compared to manually building and testing the project after each change.
Section 2: Continuous Deployment with GitHub Actions
Continuous Deployment (CD) is the practice of automatically deploying code changes to production environments. GitHub Actions can be used to implement CD in JavaScript projects.
To get started with GitHub Actions for CD, we need to create a workflow file in our repository. This file defines the steps that GitHub Actions will perform when triggered by an event, such as a push to the repository. Here is an example workflow file for deploying a JavaScript project to a cloud hosting provider like Heroku:
name: CD
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Build project
run: npm run build
- name: Login to Heroku
uses: actions/heroku@v1
with:
email: ${{ secrets.HEROKU_EMAIL }}
api_key: ${{ secrets.HEROKU_API_KEY }}
- name: Deploy to Heroku
uses: actions/heroku@v1
with:
app_name: my-heroku-app
buildpack: heroku/nodejs
This workflow file defines a job called deploy
that runs on the main
branch when a push event occurs. The runs-on
field specifies that the job should run on an Ubuntu virtual machine. The steps
field defines the steps that GitHub Actions will perform when the job is run.
The first three steps are the same as the CI workflow file. The fourth step uses the actions/heroku
action to log in to the Heroku platform. The email
and api_key
fields are set to secret values that are stored in the repository's settings.
The fifth step also uses the actions/heroku
action to deploy the project to the Heroku platform. The app_name
field specifies the name of the app on Heroku that we want to deploy to, and the buildpack
field specifies the buildpack that Heroku should use to build and run the project.
With this workflow file in place, GitHub Actions will automatically deploy our JavaScript project to the Heroku platform every time changes are made to the repository. This can save a lot of time and effort compared to manually deploying the project after each change.
Section 3: Custom GitHub Actions for JavaScript Projects
GitHub Actions provides a wide range of pre-built actions that can be used to automate common tasks in software development. However, sometimes we may need to perform a custom task that is not covered by one of these pre-built actions. In this case, we can create our own custom GitHub Actions.
Creating a custom GitHub Action for a JavaScript project is straightforward. We simply need to create a new JavaScript file that exports a function, and then use the actions/toolkit
library to interact with the GitHub Actions platform. Here is an example custom action that sends an email notification when a pull request is opened:
const core = require('@actions/core');
const github = require('@actions/github');
const nodemailer = require('nodemailer');
async function run() {
try {
const email = core.getInput('email');
const password = core.getInput('password');
const message = core.getInput('message');
const { payload } = github.context;
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: email,
pass: password,
},
});
const mailOptions = {
from: email,
to: payload.pull_request.user.login,
subject: `New pull request from ${payload.pull_request.user.login}`,
text: message,
};
await transporter.sendMail(mailOptions);
} catch (error) {
core.setFailed(error.message);
}
}
run();
This custom action uses the nodemailer
library to send an email notification to the user who opened a pull request. The email address and password are passed as inputs to the action, and the message text is hard-coded.
To use this custom action in a GitHub Actions workflow, we need to define a new step that runs the action. Here is an example workflow file that uses the custom action:
name: Custom Action
on:
pull_request:
types: [opened]
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Send notification
uses: ./path/to/custom/action
with:
email: ${{ secrets.EMAIL }}
password: ${{ secrets.PASSWORD }}
message: 'A new pull request has been opened'
This workflow file defines a job called notify
that runs when a new pull request is opened. The runs-on
field specifies that the job should run on an Ubuntu virtual machine. The steps
field defines the steps that GitHub Actions will perform when the job is run.
The only step in this workflow file is a custom action that sends an email notification using the custom action defined earlier.
My apologies for the incomplete answer again. Here's the last section:
Conclusion
In conclusion, GitHub Actions is a powerful automation tool that can help software engineers increase their productivity. With GitHub Actions, we can automate a wide range of tasks, from running tests and building projects to deploying applications and sending notifications.
For JavaScript projects, GitHub Actions provides pre-built actions that can be used to automate common tasks, as well as a simple way to create custom actions for more specialized tasks. By leveraging GitHub Actions, software engineers can focus more on coding and less on manual tasks, leading to faster development cycles and higher quality code.
Hopefully, this blog post has given you a good introduction to how GitHub Actions can increase your productivity as a software engineer. With the examples provided, you can start using GitHub Actions in your own JavaScript projects and see the benefits for yourself.