sh 为magento 2部署脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 为magento 2部署脚本相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env bash
usage() {
    echo "Usage: $0 [-e <staging|local>]" 1>&2; exit 1;
}

maintenanceEnable() {
    touch maintenance.enable
    php bin/magento maintenance:enable
}

maintenanceDisable() {
    php bin/magento maintenance:disable
    rm -f maintenance.enable
}

composerInstall() {
    composer install --no-interaction --prefer-dist --ignore-platform-reqs
}

magentoSetup() {
    php bin/magento cache:flush
    rm -rf pub/static/*
    rm -rf var/view_preprocessed/*
    php bin/magento cache:disable
    php bin/magento setup:upgrade
    php bin/magento setup:di:compile
    php bin/magento setup:static-content:deploy
    php bin/magento cache:enable
}

setUrls() {
    host_base="http://$1/"
    host_secure="https://$1/"

    base_media_url="http://$1/pub/media/"
    base_media_url_secure="https://$1/pub/media/"

    base_static_url="http://$1/pub/static/"
    base_static_url_secure="https://$1/pub/static/"

    store_code=$2
    website_code=$3

    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=${website_code} web/unsecure/base_url ${host_base}
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=${website_code} web/unsecure/base_static_url ${base_static_url}
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=${website_code} web/unsecure/base_link_url ${host_base}
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=${website_code} web/unsecure/base_media_url ${base_media_url}
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=${store_code} web/unsecure/base_url ${host_base}
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=${store_code} web/unsecure/base_static_url ${base_static_url}
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=${store_code} web/unsecure/base_link_url ${host_base}
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=${store_code} web/unsecure/base_media_url ${base_media_url}

    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=${website_code} web/secure/base_url ${host_secure}
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=${website_code} web/secure/base_static_url ${base_static_url_secure}
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=${website_code} web/secure/base_link_url ${host_secure}
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=${website_code} web/secure/base_media_url ${base_media_url_secure}
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=${store_code} web/secure/base_url ${host_secure}
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=${store_code} web/secure/base_static_url ${base_static_url_secure}
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=${store_code} web/secure/base_link_url ${host_secure}
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=${store_code} web/secure/base_media_url ${base_media_url_secure}
}

setUnSecureUrl () {
    php bin/magento config:set --lock-env --lock-config web/secure/use_in_adminhtml 0
    php bin/magento config:set --lock-env --lock-config web/secure/use_in_frontend 0
    php bin/magento config:set --lock-env --lock-config web/secure/enable_upgrade_insecure 0
    php bin/magento config:set --lock-env --lock-config web/secure/enable_hsts 0
    php bin/magento config:set --lock-env --lock-config webapi/webapisecurity/allow_insecure 0
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=base web/secure/use_in_adminhtml 0
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=base web/secure/use_in_frontend 0
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=base web/secure/enable_upgrade_insecure 0
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=base web/secure/enable_hsts 0
    php bin/magento config:set --lock-env --lock-config --scope=websites --scope-code=base webapi/webapisecurity/allow_insecure 0
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=default web/secure/use_in_adminhtml 0
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=default web/secure/use_in_frontend 0
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=default web/secure/enable_upgrade_insecure 0
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=default web/secure/enable_hsts 0
    php bin/magento config:set --lock-env --lock-config --scope=stores --scope-code=default webapi/webapisecurity/allow_insecure 0
}

while getopts "e:d:" options; do
    case "${options}" in
        e) e=${OPTARG} ;;
        d) d=${OPTARG} ;;
        *) usage ;;
    esac
done

echo "env ${e}";
echo "db ${d}";

if [ -z "${e}" ]; then
    echo 'env parameter not found';
    maintenanceEnable
    composerInstall
    magentoSetup
    maintenanceDisable
else
    if [ "$e" = "staging" ]; then
        echo 'staging start';
        git stash
        git fetch origin
        git reset --hard origin/staging
        maintenanceEnable
        composerInstall
        magentoSetup
        if [ -z "${d}" ]; then
            echo "parameter db not found"
        else
            if [ "$d" = "url" ]; then
                setUrls 'staging.microbattery.com' 'default' 'base'
                setUrls 'staging.cochlearbatteries.com' 'cochlearbatteries' 'cochlearbatteries'
                setUrls 'staging.hearingaidbatteryshop.com' 'habs' 'habs'
            fi
        fi
        maintenanceDisable
    elif [ "$e" = "local" ]; then
        echo 'local start';
        maintenanceEnable
        composerInstall
        magentoSetup
        if [ -z "${d}" ]; then
            echo "parameter db not found"
        else
            if [ "$d" = "url" ]; then
                setUrls 'microbattery.docker' 'default' 'base'
                setUrls 'cochlearbatteries.docker' 'cochlearbatteries' 'cochlearbatteries'
                setUrls 'hearingaidbatteryshop.docker' 'habs' 'habs'
            elif [ "$d" = "unsecure" ]; then
                setUnSecureUrl
            fi
        fi
        maintenanceDisable
    elif [ "$e" = "prod" ]; then
        echo 'prod start';
        maintenanceEnable
        git stash
        git fetch origin
        git reset --hard origin/master
        composerInstall
        magentoSetup
        maintenanceDisable
    else
        echo "not found env: ${e}"
    fi
fi
echo 'finish'

以上是关于sh 为magento 2部署脚本的主要内容,如果未能解决你的问题,请参考以下文章

sh 部署Magento 2个步骤

sh 将magento 2全部部署到本地

sh 用于关闭magento 2模板和阻止提示的Shell脚本,将文件添加到流浪者中的〜/ bin并将权限设置为755

sh 用于打开magento 2模板和阻止提示的Shell脚本,将文件添加到流浪者中的〜/ bin并将权限设置为755

sh 用于导出magento 2数据库的Bash脚本 - 剥离的销售数据

sh Shell脚本:Magento2数据库+代码库备份。更多 - http://www.blog.magepsycho.com/