GitHub Workflows allow developers to automate their software development lifecycle from building and testing to deploying applications.
In this course, we'll dive into the fundamentals of GitHub Workflows, explore real-world use cases, and walk you through step-by-step examples to automate tasks like testing, building, and deploying applications. By the end, you'll have the skills to create robust, automated workflows that save time, reduce errors, and enhance collaboration across your team.
GitHub Workflows are powerful automation tools integrated directly into GitHub Actions, allowing you to create custom software development lifecycle processes directly in your repository. They help you automate tasks like:
GitHub Workflows are defined in YAML files and are executed when certain events occur in your repository. They enable you to:
A workflow is process defined in a YAML file located in the .github/workflows directory of your repository. Each workflow consists of:
The following YAML show an example of a basic workflow
12name: My First Workflow3on: [push] # Trigger on push to any branch45jobs:6 build:7 runs-on: ubuntu-latest8 steps:9 - uses: actions/checkout@v310 - name: Run a one-line script11 run: echo Hello, world!12
In this section, we will build a basic CI pipeline for a Python project and then extend it to create a pipeline to deploy a Node.js application to a Linux VM.
Let's create a pipeline that will ensures that every change to the main branch is tested and linted automatically.
12name: Python Application CI34on:5 push:6 branches: [ main ]7 pull_request:8 branches: [ main ]910jobs:11 build:12 runs-on: ubuntu-latest1314 steps:15 - uses: actions/checkout@v31617 - name: Set up Python18 uses: actions/setup-python@v319 with:20 python-version: '3.9'2122 - name: Install dependencies23 run: |24 python -m pip install --upgrade pip25 pip install -r requirements.txt2627 - name: Run tests28 run: |29 pytest tests/3031 - name: Lint with flake832 run: |33 pip install flake834 flake8 .35
This pipleline run on two type of events: when we push to the main branch and when we create a pull_request targeting the main branch.
The pipeline defines a single job called build, which runs on the latest version of Ubuntu (ubuntu-latest). The jobs contains five steps:
In this section we will create a github workflow that will deploy a Node.js application a linux VM. The workflow will:
Before using this pipeline, you should first create the required secrets on your github secrets. (You should create SSH_PRIVATE_KEY, VM_USER, VM_HOST, APP_DIR)
12name: Deploy Node.js app to Linux VM34on:5 push:6 branches: [ main ]78jobs:9 build-and-deploy:10 runs-on: ubuntu-latest1112 steps:13 - uses: actions/checkout@v31415 - name: Use Node.js16 uses: actions/setup-node@v317 with:18 node-version: '16'1920 - name: Install Dependencies21 run: npm ci2223 - name: Run Tests24 run: npm test2526 - name: Lint code27 run: npm run lint # Add a linting step if applicable2829 - name: Deploy Node.js App to Linux VM30 env:31 SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}32 VM_USER: ${{ secrets.VM_USER }}33 VM_HOST: ${{ secrets.VM_HOST }}34 APP_DIR: ${{ secrets.APP_DIR }} # Directory on the VM35 run: |36 set -e # Exit on error37 mkdir -p ~/.ssh38 chmod 700 ~/.ssh39 echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa40 chmod 600 ~/.ssh/id_rsa4142 # Add VM's SSH fingerprint to known_hosts43 ssh-keyscan -H $VM_HOST >> ~/.ssh/known_hosts4445 # Copy application files to the VM46 scp -r . $VM_USER@$VM_HOST:$APP_DIR4748 # SSH into the VM and deploy the app49 ssh $VM_USER@$VM_HOST << 'EOF'50 set -e # Exit on error51 cd $APP_DIR52 npm ci --production # Install production dependencies53 pm2 start app.js --name <My node app name>54 echo "Deployment successful!"55 EOF5657 # This is just an example assuming that you have an HHTP API to call58 - name: Notify Monitoring Service59 env:60 MONITORING_API_KEY: ${{ secrets.MONITORING_API_KEY }},61 run: |62 curl -X POST https://monitoring-service.com/api/deploy63 -H "Authorization: Bearer $MONITORING_API_KEY"64 -H "Content-Type: application/json"65 -d '{66 "repository": ${{ github.repository }},67 "commit": ${{ github.sha }},68 "status": "deployed"69 }'70
Triggers are a fundamental component of GitHub Workflows, enabling automated workflows to be initiated in response to specific events or actions. These events can include push requests, pull requests, or other repository activities, allowing developers to create customized workflows that respond to changing conditions. Below is a list of triggers that you can use in your github workflows.
Optimizing GitHub Workflows is crucial for maximizing development productivity, as it enables teams to reduce manual errors. By fine-tuning their workflows, developers can automate repetitive tasks, improve code quality, and accelerate deployment times.
To implement effective CI/CD piplelines here is a list of best practice that you must apply to your Github Actions:
GitHub Workflows provide a powerful and a flexible way to automate your software development processes. By understanding the basic concepts and gradually building more complex workflows, you can significantly improve your development efficiency and code quality.