Using Jenkins for Drupal and WordPress – Your First Job

This is part 3 in a series. If you are not familiar with Jenkins, please read Part 1 and Part 2 first.

This walkthrough should give you an overview of the Freestyle Project.

Our first Jenkins job we will set up is to run Drupal Cron every hour, with a 1 hour throttle.

Jenkins β€” Drupal Cron

  1. From the main menu select New Item
  2. Enter a Name for the job and select Freestyle Project, click OK at the bottom of the form.

    You should now be on the config screen.
  3. Check Discard Old Builds, cleaning up older builds will help the admin interface perform better. You can choose between Days or Number of Jobs to keep.
  4. Check Build periodically, this sets the schedule for the build. Jenkins cron system adds a ‘H’ symbol. This is a hashed value based on the job name.
  • H * * * * Would once an hour
  • H H * * * Would once a day
  • H H(0-7) * * * means some time between 12:00 AM (midnight) to 7:59 AM.
  1. In the Build section choose Add build step and Execute shell
  2. On your Drupal site navigate to /admin/config/system/cron and copy the cron URL
  3. End the command text box enter the wget -O - -q -t 1 https://example.com/cron.php?cron_key=drupalkey7hSsc7QtW3lpANY9VxP2O
  4. Save and Build now. If all went well the job will be successful.

While curl works for cron, you will need ssh for more robust remote work. Using SSH Agent Pluginyou can add ssh keys per project, folder or globally. If you have access to ssh in to the server it’s best to add the trusted_hosts via sudo su -l jenkins (setup is beyond the scope of this article).

If you do not have shell access you can use -o StrictHostKeyChecking=no for the first run of the job. Removing the flag after you have verified connection will ensure the remote server has not changed.

ssh -o StrictHostKeyChecking=no -t [email protected] "/usr/local/bin/drush cron -y --root=/var/www/www.example.com"

Once you have ssh access setup and working you can perform other functions; here are a few examples.

Back up the database

ssh -t  [email protected]  "/usr/local/bin/drush sql-dump -y --root=/var/www/www.example.com  --gzip --result-file=~/dbbackup/backup.sql"
scp  [email protected]:dbbackup/backup.sql.gz /var/livedb/backup.sql.gz

Flush cache

ssh -t  [email protected]  "/usr/local/bin/drush cr -y --root=/var/www/www.example.com"

Import config

ssh -t  [email protected]  "/usr/local/bin/drush cim -y --root=/var/www/www.example.com"

WordPress Cron

ssh -t  [email protected]  "/usr/local/bin/wp cron event run --all --path=/var/www/www.example.com"

WordPress Flush Cache

ssh -t  [email protected]  "/usr/local/bin/wp cache flush --path=/var/www/www.example.com"

These examples are just the start of what Jenkins can do for you. If you have questions please leave them in the comments.