You can build unique, automated software development workflows (including CI/CD) right inside your GitHub repository with the help of GitHub Actions. Let’s say we have a GitHub repository.
Different types of events, such as starring, pull requests, and issue creation, can occur in that repository. These events can be used to start an automated workflow. GitHub is able to launch one or more containers for us in the cloud and carry out instructions to perform useful tasks.
Table of Content
Things You Should Know About GitHub Actions Checkout
- GitHub will track each step’s progress and make it crystal clear if anything went wrong.
- The benefit of GitHub Actions is that we can use the community-implemented steps rather than having to write them ourselves.
- A workflow can be made by writing individual tasks, or actions, and then combining them.
- You can set up workflows in your repository to automatically build, test, package, release, and deploy any project on GitHub.
- By writing custom code that interacts with your repository however you like, you can create actions. For instance, an action can publish npm modules, send Slack updates, or deploy code that is ready for production.
- To carry out practical tasks like CI/CD, publish to NPM, deploy to Azure/AWS, and more, you can use GitHub Actions.
Introduction to GitHub Actions
Go to the Actions tab in any repository where you want to create a workflow.
- Choose a workflow from the ones offered or start from scratch.
- Create a.yml file in the path.github/workflows/action1.yml to create your own workflow. When you commit the file, GitHub will recognize that it is a workflow.
- After setting it up, you can check your workflow logs in the same Actions tab.
How to design unique actions for typical applications
The whole point of continuous integration (CI) is to have developers submit small, easily maintainable chunks of their code to the main codebase on a daily basis. The primary codebase ought to be automatically tested against those modifications.
Although this tutorial uses a straightforward node project, you are free to use your own.
- Make a new action.yml file in the .github/workflows directory.
name: Node Continuous Integration on: pull_request: branches: [ master ] jobs: job_1_name: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: 12 - run: npm ci - run: npm test - run: npm run build As a result of the action described above,
- You have created a Node Continuous Integration action.
- When there is a pull request on the master branch, this action is initiated. The action’s precipitating event is described below on:
- There are one or more jobs for an action. The initial action is as follows:
opens a virtual machine running Ubuntu.
- actions/checkout@v2 the source code is brought to the active working directory. An existing GitHub action is checkout@v2.
sets up node.
- utilizes npm ci to install app dependencies.
- The npm test and npm run build commands run the test script specified in package.json. We can use our preferred libraries, such as jest and webpack, with these commands.
A green checkmark or a red “x” is displayed in the repository’s actions tab log if there are any errors or build failures.
Continuous Deployment
We also want to deploy the application to production after we merge the code from your valid pull request into the master branch. Continuous deployment involves sending that code to the production environment.
You can use the github-pages action if your website is hosted on GitHub. However, options like SFTP, FTP, and SSH are available if a third-party provider hosts your app. This is fine when we do it manually, but using a CD tool makes it more difficult. For the CD procedure, we will have to divulge a secret token to GitHub.
See how to deploy to a firebase below:
firebase init firebase deploy --only-hosting Authenticate firebase login:ci
This gives us a private token that we can give to GitHub. Navigate to the GitHub repository’s settings tab. A new FIREBASE TOKEN secret should be made.
name: Firebase CD on: push: branches: [ master ] jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@master - uses: action/setup-node@master with: node-version: 12 - run: npm ci - run: npm run build - uses: w9jds/firebase-action@master with: args: deploy --only-hosting env: FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
The preceding action deploys our application to Firebase whenever changes are pushed to the master branch. Above w9jds/firebase-action@master is a GitHub-hosted action developed by a third party.
Tips
- There are numerous CI tools available, including Jenkins, Travis CI, and Circle Ci. Jenkins is a self-hosted automation server, while GitHub actions are SaaS. (SaaS).
- For open-source repositories, GitHub Actions is free, and private repositories have a free limit. GitHub Actions incorporates CI into the ecosystem of GitHub.
- When migrating your repository to a different Git platform without rewriting the entire CI process, using an external CI tool such as Travis is helpful.