Learning Notes #01 - Github Actions
{% raw %}
GitHub Actions is a CI/CD tool that automates workflows directly from your GitHub repository. I am writing this notes for my future self.
Github: https://github.com/syedjaferk/gh_action_docker_build_push_fastapi_app
0. Github Actions
Github Actions is a managed CI/CD pipeline offering. It provides free runners for running the code.
1. Workflow, Jobs, Steps
A workflow is a collection of jobs defined in a .yml file inside .github/workflows. Each workflow consists of jobs, and jobs have steps.
name: My First Workflowon: pushjobs: example-job: runs-on: ubuntu-latest steps: - name: Print a message run: echo "Hello, GitHub Actions!"
2. Availability and Pricing
GitHub Actions is free for public repositories and has free usage limits for private repositories, depending on the plan. Paid plans increase these limits. For detailed pricing, visit the GitHub Actions pricing page.
3. First workflow with basic echo commands
Start with a workflow triggered by any push event. Here’s a simple example,
name: Echo Workflowon: pushjobs: echo-job: runs-on: ubuntu-latest steps: - name: Say Hello run: echo "Hello from my first workflow!"
4. Multiline Shell Commands
You can use the | symbol to write multiline shell commands.
name: Push Event Workflowon: push: branches: - mainjobs: push-job: runs-on: ubuntu-latest steps: - name: On Push run: echo "Code pushed to main branch!"
5. A new workflow with push events
Workflows can be triggered by specific events like push. This example triggers only on push to the main branch.
name: Push Event Workflowon: push: branches: - mainjobs: push-job: runs-on: ubuntu-latest steps: - name: On Push run: echo "Code pushed to main branch!"
6. Using actions in workflow (Marketplace and Open Source)
GitHub Actions Marketplace offers reusable actions. For example, using the actions/checkout action.
steps: - name: Checkout Code uses: actions/checkout@v3
7. Checkout in code
Checking out your code is essential to access the repository content. Use actions/checkout
steps: - name: Checkout Code uses: actions/checkout@v3
8. Adding workflow job steps
Each job can have multiple steps.
jobs: build: runs-on: ubuntu-latest steps: - name: Step 1 - Checkout Code uses: actions/checkout@v3 - name: Step 2 - Run a Script run: echo "Script running in step 2."
9. Jobs in Parallel and Sequential
To run jobs in parallel, define them without dependencies. For sequential jobs, use needs.
Parallel Jobs:
jobs: job1: runs-on: ubuntu-latest steps: - run: echo "Job 1" job2: runs-on: ubuntu-latest steps: - run: echo "Job 2"
Sequential Jobs:
jobs: job1: runs-on: ubuntu-latest steps: - run: echo "Job 1" job2: runs-on: ubuntu-latest needs: job1 steps: - run: echo "Job 2 after Job 1"
10. Using Multiple Triggers
A workflow can be triggered by multiple events.
on: push: branches: - main pull_request: branches: - main
11. Expressions and Context Objects
Expressions are used to access variables and contexts like github or env.
steps: - name: Use an Expression run: echo "Triggered by ${{ github.event_name }}"{% endraw %}