Codiwan.com

The blog for Design Patterns, Linux, HA and Myself!

Simple CI/CD using Github Action Workflows from Scratch

Learn GitHub Actions and Workflows by creating a workflow pipeline from Scratch

Now, you can create your CI/CD pipelines for your Github repositories using Github Workflows.

However, there were some services like, Travis-CI, already available for the CI/CD needs but for the private repositories it was a dire need.

The setup requires:

The runner can have the following labels:

In this tutorial we’ll be using a self hosted runner and it is divided into the following sections:

How to Setup a Self Hosted Runner

  1. Go to your repository’s settings:

    Click on Settings

  2. Click on the Actions menu option:

    Click on Actions

  3. Click on the button Add Runner:

    Click on Add Runner

  4. You’ll be presented with a list of action items:

    Follow the install steps

  5. Once you’ve completed all the tasks in the configured section, the runner will be available on the same page:

    Runners are Listed once you're done

  6. Well, a better way would be setup the runner as a service instead of running as a bash script. It can installed using the following command:

    sudo ./svc.sh install
    
  7. To start the service, execute the following command:

    sudo ./svc.sh start
    
  8. You can check the status of the service using the following command:

    sudo ./svc.sh status
    

    However, it is not required right now but you stop and unistall this service using the following commads:

    sudo ./svc.sh stop
    
    sudo ./svc.sh uninstall
    

Creating the Workflow YML

  1. Create a new file in the following location in your repository:
    mkdir -p .github/workflows
    touch .github/workflows/codiwan-test.yml
    
  2. Add a name for your workflow. The name will appear in the the Actions section in the Github Page.
    name: Codiwan Test Deploy
    
  3. If you want to run your pipeline whenever a commit is made to master
    name: Codiwan Test Deploy
    on:
      push:
        branches:
          - master
    
  4. The pipeline is divided into Jobs and the Jobs are divided into Steps.
    name: Codiwan Test Deploy
    on:
      push:
        branches:
          - master
       
    jobs:
      test-deploy-job:
        name: Test and Deploy Job
        runs-on: self-hosted
        steps:
          - uses: actions/checkout@master
          - run: /usr/local/bin/hugo
    
    

    Here the there’s a single job whose job is to test and deploy.

    • The first step is to clone the repository and checkout to master.
    • It is done using - uses: actions/checkout@master.
    • This job is to be run on the worker that we’ve just created. It is done by adding the runs-on.
      runs-on: self-hosted
      
    • The next step is to validate the build. I’m using hugo in my project so I’ll execute Hugo. Surely, your require would be different.
      run: /usr/local/bin/hugo
      
  5. We’ll execute the scripts once Hugo runs successfully
    name: Codiwan Test Deploy
    on:
     push:
       branches:
         - master
      
    jobs:
     test-deploy-job:
       name: Test and Deploy Job
       runs-on: self-hosted
       steps:
         - uses: actions/checkout@master
         - name: Check Hugo
           run: /usr/local/bin/hugo
           if: success()
         - name: Check permissions
           if: success()
           run: chmod +x make_it_live.sh
         - name: Make it Live
           if: success()
           run: ./make_it_live.sh
    
    • You would have noticed an if: success(). This makes sure that the current step runs only if the previous step executed successfully. It makes the job sequential.
    • I’ve a custom script, make_it_live.sh, to update the Nginx root directory.
    • Adding permission to the script using
    run: chmod +x make_it_live.sh
    
    • Then the script is executed
    run: ./make_it_live.sh
    

Run Your Tests

So, we’ve created a very simple pipeline now. Let’s test it by committing this file to the master branch.

Once pushed, the workflow will start. Go to the Actions page for checking it’s status.

Actions after workflow is created

The status of the jobs are presented in the section that have the same name as the Job. In our case it is Test and Deploy Job.

Job Status

Some links that can provide you more detail:

Loading Comments... Disqus Loader
comments powered by Disqus