loading...
Cover image for CI/CD Workflow with GitHub Actions

CI/CD Workflow with GitHub Actions

jorgehrj profile image Jorge Hernández Ríos ・3 min read

One of the advantages that I found of GitLab versus GitHub was their GitLab CI/CD tool (https://docs.gitlab.com/ee/ci/). But I have always used GitHub, so I have used another ways to perform those CI/CD actions. But then GitHub Actions appeared (https://github.blog/2019-08-08-github-actions-now-supports-ci-cd/) and caught my attention.

Basically, it allows us to automate all our workflow actions easily, such as running the tests suite or deploying the app. Whatever you want. You can find all the information here: https://github.com/features/actions.

I recently used it for the first time in a project and I really liked it. This project is a Docker-based Symfony app and it is deployed in a DigitalOcean droplet. So, when I am developing this project, my workflow is the following:

Continous Integration tasks when a pull request is made.

Those actions are running some quality checks in order to mantain the quality of the code. For that, I use the following tools:

I usually run this script for that: https://github.com/JorgeHRJ/qualitify.
Until the checks have not passed, the pull request is not going to be merged.

Continous Deployment tasks when commits are pushed/merged to master.

Finally, we have our pull requests merged into our ‘master’ branch, so we need to deploy our app with the new changes. And we do it in this step.

Let’s get into the practical part, and you will see how easy is it to implement in your project:
First of all, you need to create a .github/workflows directory in your repo. This will tell GitHub that we have some GitHub Actions workflow to be performed. Then, we create a .yml file for every workflow explained before. Here there are the ones I am using:

continuous-integration.yml
name: Continuous Integration

on: [pull_request]

jobs:
  quality:
    name: Quality checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Install dependencies
        run: composer install --prefer-dist --no-progress --no-suggest

      - name: PHP Code Sniffer
        if: ${{ always() }}
        run: vendor/bin/phpcs --standard=PSR2 src

      - name: PHP Mess Detector
        if: ${{ always() }}
        run: vendor/bin/phpmd src/ text codesize,controversial,phpmd.xml

      - name: PHP Stan
        if: ${{ always() }}
        run: vendor/bin/phpstan analyse -c phpstan.neon

      - name: PHP Unit tests
        if: ${{ always() }}
        run: bin/phpunit

As you can see, this workflow is named ‘Continuous Integration’. It has only one job named ‘quality’, which is labeled as ‘Quality checks’. It will use a ‘ubuntu-latest’ Docker image for running the steps we are declaring then:

  1. Checkout the code
  2. Install our composer dependencies. So notice we need to have added there our quality tools. You can have them globally installed, so notice you will need to change the ‘run’ part in the following steps.
  3. Run every tool. Here you can see there are some ‘if’s. They are there because I want every tool step to be runned, despite some of them have failed before.

CI in GitHub

continuous-deployment.yml
name: Continuous Deployment
on:
  push:
    branches:
      - master

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Deploy
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          port: ${{ secrets.PORT }}
          username: ${{ secrets.USER }}
          key: ${{ secrets.KEY }}
          script: |
            cd /home/app/bielatv
            bin/deploy.sh

And here you have the ‘Continuous Deployment’ one. Basically, it is declared that when some commits are pushed (or merged) into the ‘master’ branch, we will run our ‘deploy’ job. In my case, I only have one production environment, buy maybe you have some development or staging environment. In that case, you could define another job for when commits are merged into other branch.

Anyway, this job will checkout our code, and uses a nice GitHub action: https://github.com/appleboy/ssh-action. It allow us to run some ssh actions. We will have to set some GitHub Secrets (https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) so the ssh connection is able to be done. Then, in the ‘script’ part, we define the actions to perform the deploy. In my case, getting into the project directory and running a deploy script.

CD in GitHub

And that’s it. We have our new feature developed and deployed in a moment!

Discussion

markdown guide
 

Hi,

Thanks for the nice post. Can you please share phpmd.xml ?

TIA

 

Sorry, I didn't see this message. I am using this one:

<?xml version="1.0"?>
<ruleset name="Clean Code Rules (Modification)"
         xmlns="http://pmd.sf.net/ruleset/1.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
         xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
    <description>
        The Clean Code ruleset contains rules that enforce a clean code base. This includes rules from SOLID and object calisthenics. (Modification)
    </description>

    <rule name="ElseExpression"
          since="1.4.0"
          message="The method {0} uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them."
          class="PHPMD\Rule\CleanCode\ElseExpression"
          externalInfoUrl="https://phpmd.org/rules/cleancode.html#elseexpression">
        <description>
            <![CDATA[
An if expression with an else branch is basically not necessary. You can rewrite the
conditions in a way that the else clause is not necessary and the code becomes simpler
to read.  To achieve this, use early return statements, though you may
need to split the code it several smaller methods. For very simple assignments
you could also use the ternary operations.
            ]]>
        </description>
        <priority>1</priority>
        <properties/>
        <example>
            <![CDATA[
class Foo
{
    public function bar($flag)
    {
        if ($flag) {
            // one branch
        } else {
            // another branch
        }
    }
}
            ]]>
        </example>
    </rule>

    <rule name="IfStatementAssignment"
          since="2.7.0"
          message="Avoid assigning values to variables in if clauses and the like (line '{0}', column '{1}')."
          class="PHPMD\Rule\CleanCode\IfStatementAssignment"
          externalInfoUrl="http://phpmd.org/rules/cleancode.html#ifstatementassignment">
        <description>
            <![CDATA[
Assignments in if clauses and the like are considered a code smell.
Assignments in PHP return the right operand as their result.
In many cases, this is an expected behavior, but can lead
to many difficult to spot bugs, especially when the right
operand could result in zero, null or an empty string and the like.
            ]]>
        </description>
        <priority>1</priority>
        <properties></properties>
        <example>
            <![CDATA[
class Foo
{
    public function bar($flag)
    {
        if ($foo = 'bar') { // possible typo
            // ...
        }
        if ($baz = 0) { // always false
            // ...
        }
    }
}
            ]]>
        </example>
    </rule>

    <!--
    <rule name="StaticAccess"
          since="1.4.0"
          message="Avoid using static access to class '{0}' in method '{1}'."
          class="PHPMD\Rule\CleanCode\StaticAccess"
          externalInfoUrl="https://phpmd.org/rules/cleancode.html#staticaccess">
        <description>
            <![CDATA[
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid
using static access at all costs and instead inject dependencies through the constructor. The only
case when static access is acceptable is when used for factory methods.
            ]]>
        </description>
        <priority>1</priority>
        <properties>
            <property name="exceptions" description="Comma-separated class name list of exceptions" value=""/>
        </properties>
        <example>
            <![CDATA[
class Foo
{
    public function bar()
    {
        Bar::baz();
    }
}
            ]]>
        </example>
    </rule>
    -->

    <rule name="DuplicatedArrayKey"
          message="Duplicated array key {0}, first declared at line {1}."
          class="PHPMD\Rule\CleanCode\DuplicatedArrayKey"
          externalInfoUrl="http://phpmd.org/rules/cleancode.html#duplicatedarraykey">
        <description>
            <![CDATA[
Defining another value for the same key in an array literal overrides the previous key/value,
which makes it effectively an unused code. If it's known from the beginning that the key
will have different value, there is usually no point in defining first one.
            ]]>
        </description>
        <priority>2</priority>
        <example>
            <![CDATA[
function createArray() {
    return [
        'non-associative 0element', // not applied
        0 => 'associative 0-element', // applied
        false => 'associative 0-element', // applied
        'foo' => 'bar', // not applied
        "foo" => 'baz', // applied
    ];
}
            ]]>
        </example>
    </rule>

    <rule name="UndefinedVariable"
          since="2.8.0"
          message="Avoid using undefined variables such as '{0}' which will lead to PHP notices."
          class="PHPMD\Rule\CleanCode\UndefinedVariable"
          externalInfoUrl="">
        <description>
            Detects when a variable is used that has not been defined before.
        </description>
        <priority>3</priority>
        <example>
            <![CDATA[
class Foo
{
    private function bar()
    {
        // $message is undefined
        echo $message;
    }
}
]]>
        </example>
    </rule>
</ruleset>

Actually, it is a modification of this one that you can use from the PHPMD people: github.com/phpmd/phpmd/blob/master...

I deleted some of them, I do not remember now which ones.