Often time when your project starts to grow, the test suite also starts to get bigger, i.e slower CI.

You will notice that there are tests that hardly ever fail. Maybe we shouldn’t run these every commit? How about run these and the full test suite on schedule such as once a day?

For a Laravel project that is hosted on Github, here is the idea:

1. Create a branch ci-full-tests off your main branch and set up your CI to run tests for this branch
2. Get a Github OAuth token
3. Create a console command to merge the main branch to ci-full-tests:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;

class TriggerCIToRunFullTests extends Command
{
    /**
    * The name and signature of the console command.
    *
    * @var string
    */
    protected $signature = 'ci:full-tests';

    /**
    * The console command description.
    *
    * @var string
    */
    protected $description = 'Trigger CI to run the full tests';

    /**
    * @var Client
    */
    protected $client;

    /**
    * Create a new command instance.
    *
    * @return void
    */
    public function __construct(Client $client)
    {
        parent::__construct();

        $this->client = $client;
    }

    /**
    * Execute the console command.
    *
    * @return mixed
    */
    public function handle()
    {
        if (app()->environment() !== 'staging') {
            return;
        }

        $this->client->post("https://api.github.com/repos/YOUR_ORG/REPO_NAME/merges", [
            'headers' => [
                'X-GitHub-Media-Type' => 'application/vnd.github.v3+json',
                'Authorization' => 'token ' . env('GITHUB_OAUTH_TOKEN'),
            ],
            'json' => [
                'base' => 'ci-full-tests',
                'head' => env('BRANCH_FOR_CODESHIP_FULL_TESTS', 'master'),
                'commit_message' => '[Schedule] Run full tests',
            ],
        ]);
    }
}

4. Schedule to run this command once a day

1
2
3
// app/Console/Kernel.php

$schedule->command('ci:full-tests')->daily();

5. Add an if condition in your CI bash script

1
2
3
4
5
6
7
8
ci_branch=`printenv GIT_BRANCH`

if [[ $ci_branch == "ci-full-tests" ]]
then
    vendor/bin/behat --tags=~@wip
else
    vendor/bin/behat --tags='@important'
fi

Comments