Bitbucket pipelines with Perl and MySQL

DBD::mysql

If you have an application in Perl that uses MySQL and you with to use BitBucket Pipelines for your Perl project then this example might help you set up MySQL as well.

Bitbucket uses Docker images so the recommended way is to use a separate Docker image for mysql and to configure it as a service for the pipeline.

This is a version of the bitbucket-pipelines.yml file that worked for me:

examples/bitbucket_mysql_57/bitbucket-pipelines.yml

image: perl:5.26

definitions:
  services:
    mysql:
      image: mysql:5.7
      environment:
        MYSQL_DATABASE: 'test_db'
        MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
        MYSQL_USER: 'test_user'
        MYSQL_PASSWORD: 'test_password'

pipelines:
  default:
    - step:
        script:
          - cpanm DBD::mysql
          - perl check_mysql.pl
        services:
          - mysql



Here the main Docker image where our code will run is called perl:5.26 from Docker HUB.

Though given that this is a hash, the order does not matter, but I think it is more logical to have the definitions next. In there we define a service called mysql

examples/bitbucket_mysql_57/check_mysql.pl

use strict;
use warnings;
use DBI;

my $hostname = '127.0.0.1';
my $port = 3306;
my $database = 'test_db';
my $user = 'test_user';
my $password = 'test_password';
my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
my $dbh = DBI->connect($dsn, $user, $password, {
   PrintError       => 0,
   RaiseError       => 1,
   AutoCommit       => 1,
   FetchHashKeyName => 'NAME_lc',
});

my $sql = q{
CREATE TABLE people (
   id   INTEGER PRIMARY KEY AUTO_INCREMENT,
   name VARCHAR(20)
);
};

$dbh->do($sql);
$dbh->do("INSERT INTO people (name) VALUES(?)", undef, "Foo");
$dbh->do("INSERT INTO people (name) VALUES(?)", undef, "Bar");

my $sth = $dbh->prepare("SELECT * FROM people");
$sth->execute;
while (my @row = $sth->fetchrow_array) {
   print "id: $row[0]  name: $row[1]\n";
}



MySQL on Docker HUB

Author

Gabor Szabo (szabgab) Gabor Szabo