Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rafaelstz/a3a074bb083bdce362f4afbd61a7b985 to your computer and use it in GitHub Desktop.
Save rafaelstz/a3a074bb083bdce362f4afbd61a7b985 to your computer and use it in GitHub Desktop.
Automated deploy using Magento 2 + Bitbucket Pipelines + Deployer

Use both files in your root folder and run:

curl -LO https://deployer.org/deployer.phar && sudo mv deployer.phar /usr/local/bin/dep && sudo chmod +x /usr/local/bin/dep
composer require deployer/recipes --dev
composer require rafaelstz/deployer-magento2 dev-master --dev
34.199.54.113,34.232.25.90,34.232.119.183,34.236.25.177,35.171.175.212,52.54.90.98,52.202.195.162,52.203.14.55,52.204.96.37,34.218.156.209,34.218.168.212,52.41.219.63,35.155.178.254,35.160.177.10,34.216.18.129,34.199.54.113, 34.232.25.90, 34.232.119.183, 34.236.25.177, 52.204.96.37, 35.171.175.212, 52.54.90.98, 52.202.195.162, 52.203.14.55, 34.218.156.209, 34.218.168.212, 52.41.219.63, 35.155.178.254, 35.160.177.10, 34.216.18.129, 52.8.84.222, 52.52.234.127, 104.192.136.0/21
image: rafaelcgstz/magento2
clone:
depth: 1 # include just the last commit
pipelines:
branches:
staging:
- step:
name: Deploy Staging
deployment: staging
caches:
- composer
artifacts:
- vendor/**
script:
- echo "$AUTH" > auth.json
- composer install --quiet
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > /root/.ssh/id_rsa.tmp
- base64 -d /root/.ssh/id_rsa.tmp > /root/.ssh/id_rsa
- echo "$SSH_PUBLIC_KEY" | tr -d '\r' > /root/.ssh/id_rsa.pub
- cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
- eval "$(ssh-agent -s)"
- echo "$KNOWN_HOSTS" >> /root/.ssh/known_hosts
- chmod 700 /root/.ssh
- chmod 600 /root/.ssh/id_rsa
- chmod 644 /root/.ssh/authorized_keys
- chmod 644 /root/.ssh/known_hosts
- echo -e "Host *\n StrictHostKeyChecking no\n UserKnownHostsFile=/dev/null" > /root/.ssh/config
- curl -LO https://deployer.org/deployer.phar && mv deployer.phar /usr/local/bin/dep && chmod +x /usr/local/bin/dep
- echo "set('slack_text', '_Bitbucket Pipelines_ deploying `{{branch}}` to *{{target}}*');" >> deploy.php
- dep deploy staging
- step:
name: PHP Copy/Paste Detector
caches:
- composer
artifacts:
- vendor/**
trigger: manual
script:
- composer install --quiet
- php vendor/bin/phpcpd app/
- step:
name: PHP CodeSniffer PSR1
caches:
- composer
artifacts:
- vendor/**
trigger: manual
script:
- composer install --quiet
- php vendor/bin/phpcs --standard=PSR1 app/code app/design
- step:
name: PHP Mess Detector
caches:
- composer
artifacts:
- vendor/**
trigger: manual
script:
- composer install --quiet
- php vendor/bin/phpmd app/code,app/design text cleancode,codesize,controversial,design,naming,unusedcode
- step:
name: ECG Magento Code Sniffer PSR2
caches:
- composer
artifacts:
- vendor/**
trigger: manual
script:
- composer install --quiet
- composer require magento-ecg/coding-standard
- php vendor/bin/phpcs --standard=PSR2 app/code app/design
- php vendor/bin/phpcs --standard=vendor/magento-ecg/coding-standard/EcgM2/ruleset.xml --extensions=php,phtml app/code app/design
master:
- step:
name: Deploy Production
deployment: production
size: 2x # Double resources available for this step
caches:
- composer
artifacts:
- vendor/**
script:
- echo "$AUTH" > auth.json
- composer install --quiet
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > /root/.ssh/id_rsa.tmp
- base64 -d /root/.ssh/id_rsa.tmp > /root/.ssh/id_rsa
- echo "$SSH_PUBLIC_KEY" | tr -d '\r' > /root/.ssh/id_rsa.pub
- cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
- eval "$(ssh-agent -s)"
- echo "$KNOWN_HOSTS" >> /root/.ssh/known_hosts
- chmod 700 /root/.ssh
- chmod 600 /root/.ssh/id_rsa
- chmod 644 /root/.ssh/authorized_keys
- chmod 644 /root/.ssh/known_hosts
- echo -e "Host *\n StrictHostKeyChecking no\n UserKnownHostsFile=/dev/null" > /root/.ssh/config
- curl -LO https://deployer.org/deployer.phar && mv deployer.phar /usr/local/bin/dep && chmod +x /usr/local/bin/dep
- echo "set('slack_text', '_Bitbucket Pipelines_ deploying `{{branch}}` to *{{target}}*');" >> deploy.php
- dep deploy production
- step:
name: Install Magento 2 Local
trigger: manual
caches:
- composer
artifacts:
- vendor/**
script:
- echo "$AUTH" > auth.json
- composer install --no-dev --quiet
- chmod u+w -R ./var ./vendor ./pub/static ./pub/media ./app/etc
- chmod u+x ./bin/magento
- php -f ./bin/magento setup:install --base-url=http://localhost/ --use-secure=0 --db-host=127.0.0.1 --db-name=magento2 --db-user=root --db-password=root --admin-firstname=Magento --admin-lastname=Commerce [email protected] --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1 --backend-frontname=admin
- php -f ./bin/magento setup:upgrade
- php -f ./bin/magento deploy:mode:set production --skip-compilation
- php -f ./bin/magento setup:di:compile
- php -f ./bin/magento setup:static-content:deploy -j 10
- composer dump-autoload --no-dev --no-interaction --optimize
services:
- mysql
definitions:
services:
mysql:
image: mysql:5.6
environment:
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_DATABASE: 'magento2'
MYSQL_USER: 'magento'
MYSQL_PASSWORD: 'magento'
<?php
namespace Deployer;
require __DIR__ . '/vendor/rafaelstz/deployer-magento2/deploy.php';
// Project
set('application', 'My Project Name');
set('repository', '[email protected]:lesite/my-project.git');
set('default_stage', 'staging');
// Env Configurations
set('php', '/usr/bin/php70');
set('magerun', '/usr/bin/n98-magerun2');
set('composer', '/usr/bin/composer');
// Project Configurations
host('my-store.com')
->hostname('iuse.magemojo.com')
->user('my-user')
->port(22)
->set('deploy_path', '/home/my-project-folder')
->set('branch', 'master')
->set('is_production', 0)
->stage('production')
//->configFile('~/.ssh/config')
->identityFile('~/.ssh/id_rsa')
->addSshOption('UserKnownHostsFile', '/dev/null')
->addSshOption('StrictHostKeyChecking', 'no');
// Slack
set('slack_webhook', 'https://hooks.slack.com/services/YOUR/REGISTER/HERE');
before('deploy', 'slack:notify');
after('success', 'slack:notify:success');
after('deploy:failed', 'slack:notify:failure');
@remibaar
Copy link

remibaar commented Aug 9, 2023

Regarding $SSH_PRIVATE_KEY and $SSH_PUBLIC_KEY can you please clarify how were those created and where are referenced from (are those set as environment variables in the bitbucket deployment)?

The SSH keys are generated by Bitbucket. You can generate those in the settings of your project. The values are passed to your bitbucket pipelines as environment variables. Also see https://support.atlassian.com/bitbucket-cloud/docs/using-ssh-keys-in-bitbucket-pipelines/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment