A GitHub Actions workflow definition is a YAML file that tells GitHub when to run an automation procedure and what to do. Put it in `.github/workflows/`, commit it, and push it with your project. A workflow can start on a push or when you choose Run workflow in GitHub. section 4.1.1
A small workflow has four parts:
- `name` labels the workflow. section 4.1.1
- `on` chooses events, such as `push` or `workflow_dispatch` for a manual run. section 4.1.1
- `jobs` groups the work; `runs-on` selects a machine for a job. section 4.1.1
- `steps` perform the work. A step can use an existing action, such as checkout, or run a command. section 4.1.1
Listing 4.2 Hello World with GitHub Actions
name: Hello World
on:
workflow_dispatch:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Hello World
run: echo "Hello World"
The Hello World workflow shows all four parts together: it checks out the repository, then runs `echo "Hello World"` on an Ubuntu runner. section 4.1.1
The same structure can handle a deployment task. For a remote server, steps can prepare an SSH key from a GitHub Actions secret and install NGINX over SSH. Keep the key and server address in stored secrets rather than committing their values to the repository. section 4.1.5
For larger tasks, the workflow can set up Python and Ansible, create an inventory, and run an Ansible playbook. The workflow arranges when and where those commands run; the playbook describes the desired state of the remote host. section 4.2.1
From build to production deployment
A container workflow can check out a Python or Node.js application, log in to Docker Hub, build an image tagged `latest` and with the commit SHA, then push both tags. Extend that procedure to prepare a production virtual machine, copy a Compose file and environment file over SSH, pull the updated image, and run `docker compose up -d`. The repeated steps make the workflow reusable; repository secrets supply project-specific credentials. section 6.1.2 section 7.3.3
