Congratulations on mastering the fundamentals of Git and GitHub! Now that you're comfortable with version control and basic collaboration, it's time to unlock the true power of GitHub by exploring automation. GitHub Actions is a robust feature that allows you to automate workflows directly within your repository. Think of it as having a personal assistant for your development tasks, capable of running tests, building your code, deploying applications, and much more, all triggered by events in your repository.
At its core, a GitHub Actions workflow is defined by a YAML file. These files live in the .github/workflows directory of your repository. When a specified event occurs (like a push to a branch or a pull request being opened), GitHub Actions executes the jobs defined in your workflow.
# .github/workflows/main.yml
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
pip install -r requirements.txt
- name: Lint with flake8
run:
flake8 .Let's break down this example workflow:
name: This is the name of your workflow, which will appear in the Actions tab of your GitHub repository.on: This specifies the events that will trigger the workflow. In this case, it's triggered on apushorpull_requestto themainbranch.jobs: A workflow can contain one or more jobs. Jobs run in parallel by default, but you can define dependencies between them.build: This is the name of our single job.runs-on: This defines the operating system environment where the job will run.ubuntu-latestis a common choice for Linux-based environments.steps: A job is composed of a series of steps. Each step runs in sequence.uses: actions/checkout@v3: This is a pre-built action that checks out your repository's code, making it available to subsequent steps.name: Set up Python 3.10anduses: actions/setup-python@v3: This is another pre-built action to set up a specific version of Python.run: This executes shell commands. Here, we're installing dependencies and then runningflake8for code linting.