How to Schedule A Job in GitLab 8.13

I recently discovered that GitLab CE 8.13 has no native way to schedule a job. It could be done with cron if I wanted to schedule nightly builds.  However, what I wanted is to be able to schedule a one time job to run at either a specific time or just to run in 5 minutes. So, after looking high and low, and trying different things I finally found the solution. To test my process I have setup a simple project that contains some html files.  During a build these files get uploaded to S3.  The bucket I am using have been made public so that my “website” can be viewed.

Requirements:

The scripts created here will use the “at” command to schedule the trigger to run.  So whatever gitlab-runner you are using must have that software installed.  It is standard in Ubuntu 14 but not in Docker Ubuntu images.

Step 1: Set up A Trigger

Navigate to your project in GitLab and click the gear symbol then choose Triggers

choose_triggers

Click Add Trigger

add-trigger

 

 

Make a note of the token you will need this in the script.  Also make a note of the API path to the project which is shown in the example underneath the trigger.

Step 2: Creating The Scheduling Scripts

I am going to create two scripts.  One to deploy in 5 minutes and one to deploy at 8pm.

If your project does not already have a scripts folder then create one:

scripts_folder

Below are the two scripts: (I have removed my token. Replace it with the token generated in the previous step.)  One of these will schedule an “at” job in 5 minutes and the other will schedule an “at” job at 8:00pm UTC time.  You can modify the time to suite your needs.

5minDeploy.sh

#! /bin/bash
echo "curl -X POST -F token=triggertokengoeshere -F ref=master https://gitlab.domain.com/api/v3/projects/17/trigger/builds"| at now + 5 min

8pmDeploy.sh – (3am UTC time = 8pm my time)

#! /bin/bash
echo "curl -X POST -F token=triggertokengoeshere -F ref=master https://gitlab.domain.com/api/v3/projects/17/trigger/builds"| at 03:00

Note: The path to the API will be different for each project.

Step 3: Configuring CI

Here is an example of my CI Settings in .gitlab-ci.yml.  I have added notes that do not actually appear in the .gitlab-ci.yml.  This example is from a very simple test app I have created.  All it does is upload an some .html files to one of two buckets.  A test branch update updates the test bucket and the production update goes to the test bucket.  My AWS credentials are stored as variables in the GitLab project.

variables:
 S3_BUCKET_NAME_PROD: my-production-bucker
 S3_BUCKET_NAME_TEST: my-test-bucket

deploy_to_testing:
 environment: testing
 image: python:latest
 script:
 - pip install awscli
 - aws s3 cp ./ s3://$S3_BUCKET_NAME_TEST/ --recursive --exclude "*" --include "*.html"
 only:
 - testing  ## Runs when code is pushed to testing.
 tags:
   - docker

deploy_to_production:
 environment: production
 image: python:latest
 script:
 - pip install awscli
 - aws s3 cp ./ s3://$S3_BUCKET_NAME_PROD/ --recursive --exclude "*" --include "*.html"
 only:
 - triggers ## Runs when only on API trigger
 tags:
   - docker

Deploy_at_8pm:
 environment: planning
 image: ruby:latest
 script:
 - scripts/8pmDeploy.sh
 only:
 - master
 except:
 - triggers
 tags:
   - local
 when: manual  ## Manual does not run automatically

Deploy_in_5mins:
 environment: planning
 image: ruby:latest
 script:
 - scripts/5minDeploy.sh
 only:
 - master
 except:
 - triggers
 tags:
   - local
 when: manual ## Manual does not run automatically

The order is important.  All manual tasks should be at the bottom, unless maybe you want other jobs to run automatically after the manual task.

After you do a build, go an look at the Pipeline.  You will now see one or both of the buttons below.

manual_play_buttons

manual_schedule_buttons

 

 

 

 

 

 

If you click the Deploy_in_5mins button the results will be this:

5minsched

As you can see this created job 4 to be run atThu Nov 3 03:41:00 2016 which is 5 minutes after I pushed the button.  Because my script runs the API call for my trigger, this will run any jobs I have configures with:

only:
 - triggers

In my case it is this job:

deploy_to_production:
 environment: production
 image: python:latest
 script:
 - pip install awscli
 - aws s3 cp ./ s3://$S3_BUCKET_NAME_PROD/ --recursive --exclude "*" --include "*.html"
 only:
 - triggers ## Runs when only on API trigger
 tags:
   - docker

This will upload any HTML files located in my project’s master branch to the production S3 bucket.

If I click on the Deploy_at_8pm this will run my script at 8pm.

Follow by Email
Facebook
Google+
http://cloudlady911.com/index.php/2016/11/02/how-to-schedule-a-job-in-gitlab-8-13/
LinkedIn

4 thoughts on “How to Schedule A Job in GitLab 8.13

  1. Felipe Cabargas Reply

    Thanks for sharing this! We love to see how the community is using GitLab and we’re glad you’re part of our community.

    Keep the tips & tricks going!

Leave a Reply

Your email address will not be published. Required fields are marked *