# Introduction to GitHub Actions | Cycle.io

Canonical URL: https://cycle.io/learn/introduction-to-github-actions

## Introduction to GitHub Actions.

Automation is a key part of modern software development, and GitHub Actions makes it easy to integrate [**Continuous Integration (CI) and Continuous Deployment (CD)**](https://cycle.io/learn/introduction-to-ci-cd) directly into your GitHub repositories. Whether you want to automatically run tests, build applications, or deploy updates, GitHub Actions provides a flexible way to define workflows triggered by events in your repository.

If you're new to GitHub Actions, this guide will walk you through:

-   What GitHub Actions is and why it's useful
-   How GitHub Actions workflows work
-   Setting up your first GitHub Actions workflow
-   Best practices for using GitHub Actions effectively

By the end of this guide, you'll have a working GitHub Actions workflow and a solid understanding of how to use it to automate tasks in your projects.

### What is GitHub Actions?

GitHub Actions is a **CI/CD and automation platform** that allows you to define workflows that run automatically in response to events in your repository. These workflows can be used to:

✅ **Run tests automatically** when code is pushed or a pull request is opened. 🚀 **Deploy applications** to cloud services or servers. 📦 **Build and publish packages** to [registries](https://cycle.io/learn/container-registries). 🔄 **Automate repetitive tasks** like code formatting, issue triaging, and notifications.

GitHub Actions is **built into GitHub**, so there's no need to set up external CI/CD services. You can define workflows inside your repository using simple **YAML** configuration files, making automation easy to manage alongside your code.

![Zero To One: Github Actions On Cycle.io cover](https://cycle.io/_cdn/github-actions-on-cycle-hero-1.svg?v=mo0awgu3)

From Our Blog

Zero To One: Github Actions On Cycle.io.

Get started using Github Actions and Cycle Pipelines to automate deployments.

<https://cycle.io/blog/github-actions-on-cycle>

### How GitHub Actions Works

#### Key Concepts in GitHub Actions

Before setting up a workflow, it's important to understand a few core concepts:

-   **Workflows** - Automated processes defined in a repository (e.g., run tests, deploy code).
-   **Jobs** - Individual tasks within a workflow (e.g., "Run unit tests" and "Deploy to production").
-   **Steps** - Commands executed inside a job (e.g., installing dependencies, running tests).
-   **Actions** - Pre-built reusable units that perform specific tasks (e.g., checkout code, setup environments).

#### How Workflows Are Triggered

GitHub Actions workflows can be triggered by different events, such as:

-   **Push events** - Runs when code is pushed to the repository.
-   **Pull requests** - Runs when a PR is opened or updated.
-   **Scheduled jobs** - Runs at set intervals (e.g., nightly builds).
-   **Manual triggers** - Runs when manually triggered via GitHub UI or API.

### Setting Up Your First GitHub Actions Workflow

#### Step 1: Creating a Workflow File

Workflows in GitHub Actions are defined using **YAML** files stored inside the **.github/workflows/** directory. To create your first workflow:

1.  In your GitHub repository, go to the **Code** tab.
2.  Click **Add file** → **Create new file**.
3.  Name the file **.github/workflows/ci.yml**.

#### Step 2: Writing Your First Workflow

Copy and paste the following **basic GitHub Actions workflow** into your file:

```yaml
name: First GitHub Actions Workflow

on: push # Runs the workflow every time code is pushed to the repository

jobs:
    example-job:
        runs-on: ubuntu-latest # The OS environment for the job
        steps:
            - name: Checkout repository
              uses: actions/checkout@v3 # Pulls your repository code into the runner

            - name: Print a message
              run: echo "Hello, GitHub Actions!" # Runs a shell command
```

#### Step 3: Committing and Running the Workflow

1.  Click **Commit new file** to save the workflow.
2.  Go to the **Actions** tab in your repository.
3.  You should see your workflow running automatically after the push.

If everything is set up correctly, the job should complete successfully, and you should see the message **"Hello, GitHub Actions!"** in the logs. 🎉

### Customizing Workflows

#### Using Environment Variables

You can define environment variables inside your workflow to make scripts more flexible:

```yaml
env:
    GREETING: "Hello"
    NAME: "GitHub Actions"

jobs:
    example-job:
        runs-on: ubuntu-latest
        steps:
            - name: Print a message with variables
              run: echo "$GREETING, $NAME!"
```

#### Running Jobs on Different Platforms

By default, GitHub Actions runs on [Ubuntu](https://cycle.io/learn/linux-basics#linux-distributions), but you can specify other environments:

```yaml
jobs:
    test-on-windows:
        runs-on: windows-latest
        steps:
            - name: Run a command
              run: echo "Running on Windows"

    test-on-macos:
        runs-on: macos-latest
        steps:
            - name: Run a command
              run: echo "Running on macOS"
```

#### Adding Multiple Jobs and Dependencies

Workflows can contain **multiple jobs**, and you can define dependencies between them:

```yaml
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v3
            - name: Run build script
              run: ./build.sh

    test:
        needs: build # This job will run only after the build job completes
        runs-on: ubuntu-latest
        steps:
            - name: Run tests
              run: ./test.sh
```

### Best Practices for GitHub Actions

#### ✅ Keep Workflows Efficient

-   Run only necessary jobs to avoid wasted compute time.
-   Use **caching** to speed up workflows where possible.

#### 🔒 Secure Your Secrets

-   Use **GitHub Secrets** instead of storing sensitive data in the YAML file.
-   Example of accessing a secret:
    
    ```yaml
    env:
        API_KEY: ${{ secrets.API_KEY }}
    ```
    

#### 🔄 Avoid Unnecessary Workflow Runs

-   Use conditional execution (**if:**) to prevent running jobs when they aren't needed.
-   Example: Run a job only on the **main** branch:
    
    ```yaml
    jobs:
        deploy:
            runs-on: ubuntu-latest
            if: github.ref == 'refs/heads/main'
            steps:
                - name: Deploy application
                  run: ./deploy.sh
    ```

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
