Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Git & GitHub - GitHub Actions

Introduction to GitHub Actions for CI/CD

GitHub Actions is a powerful feature that enables you to automate, customize, and execute your software development workflows directly in your GitHub repository. It supports continuous integration (CI) and continuous deployment (CD), making it easier to build, test, and deploy your code.

Key Points:

  • GitHub Actions allows you to automate workflows using YAML configuration files.
  • Workflows can be triggered by events such as pushes, pull requests, and issues.
  • Actions can be reused from the GitHub Marketplace or created custom for specific tasks.
  • GitHub Actions integrates seamlessly with your existing tools and services.

Setting Up GitHub Actions

Step 1: Create a Workflow File

To set up a GitHub Actions workflow, create a YAML file in the .github/workflows directory of your repository. For example, create a file named ci.yml:


# Create the .github/workflows directory
$ mkdir -p .github/workflows

# Create a new workflow file
$ touch .github/workflows/ci.yml
                

Step 2: Define the Workflow

Edit the ci.yml file to define your workflow. Here is an example of a simple workflow that runs tests on every push:


name: CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test
                

Step 3: Commit and Push

Commit and push the workflow file to your repository:


# Stage the workflow file
$ git add .github/workflows/ci.yml

# Commit the changes
$ git commit -m "Add GitHub Actions workflow"

# Push the changes to GitHub
$ git push origin main
                

After pushing the changes, the workflow will be triggered on every push to the repository.

Managing Workflows

You can manage your workflows from the "Actions" tab in your GitHub repository. This tab provides an overview of all workflows, their status, and logs for each run.

GitHub Actions Overview

Using the GitHub Marketplace

The GitHub Marketplace offers a variety of pre-built actions that you can use in your workflows. To use an action from the marketplace:

  • Browse the GitHub Marketplace for available actions.
  • Select an action and review its documentation.
  • Add the action to your workflow file by following the provided usage instructions.

# Example of using a marketplace action
- name: Set up Node.js
  uses: actions/setup-node@v2
  with:
    node-version: '14'
                

Creating Custom Actions

You can also create custom actions to meet your specific needs. Custom actions can be written in JavaScript or created using Docker. To create a custom action:

  • Create a new directory in your repository for the action.
  • Add an action.yml file to define the action's metadata.
  • Implement the action's logic in JavaScript or a Dockerfile.

# Example of a custom action metadata file (action.yml)
name: 'My Custom Action'
description: 'An example custom action'
inputs:
  my-input:
    description: 'An example input'
    required: true
runs:
  using: 'node12'
  main: 'index.js'
                

Summary

This guide provided an introduction to GitHub Actions for CI/CD, covering how to set up workflows, manage them, use actions from the GitHub Marketplace, and create custom actions. GitHub Actions is a powerful tool for automating your software development workflows, improving efficiency, and ensuring code quality.