├── .gitignore ├── LICENSE ├── action.yml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ### MacOS 2 | 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 WeSQL Database Community 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'MySQL Branch Action' 2 | description: 'Creates a MySQL branch from a source MySQL' 3 | author: 'earayu' 4 | branding: 5 | icon: 'database' 6 | color: 'green' 7 | 8 | inputs: 9 | source_host: 10 | description: 'Source host address' 11 | required: true 12 | source_port: 13 | description: 'Source port' 14 | required: false 15 | default: '3306' 16 | source_user: 17 | description: 'Source user' 18 | required: false 19 | default: 'root' 20 | source_password: 21 | description: 'Source password for MySQL (use secrets.SOURCE_PASSWORD)' 22 | required: true 23 | include_databases: 24 | description: 'Databases to include' 25 | required: false 26 | default: '*' 27 | exclude_databases: 28 | description: 'Databases to exclude' 29 | required: false 30 | default: 'information_schema,mysql,performance_schema,sys' 31 | wescale_image: 32 | description: 'WeScale image tag' 33 | required: false 34 | default: 'apecloud/apecloud-mysql-scale:0.3.8' 35 | 36 | runs: 37 | using: 'composite' 38 | steps: 39 | - name: Set Up Target Cluster 40 | shell: bash 41 | run: | 42 | docker run -itd --network host --name mysql-server \ 43 | -p 3306:3306 \ 44 | -e MYSQL_ROOT_PASSWORD=passwd \ 45 | -e MYSQL_ROOT_HOST=% \ 46 | -e MYSQL_LOG_CONSOLE=true \ 47 | mysql/mysql-server:8.0.32 \ 48 | --bind-address=0.0.0.0 \ 49 | --port=3306 \ 50 | --log-bin=binlog \ 51 | --gtid_mode=ON \ 52 | --enforce_gtid_consistency=ON \ 53 | --log_replica_updates=ON \ 54 | --binlog_format=ROW 55 | 56 | docker run -itd --network host --name wescale \ 57 | -p 15306:15306 \ 58 | -w /vt/examples/wesql-server \ 59 | -e MYSQL_ROOT_USER=root \ 60 | -e MYSQL_ROOT_PASSWORD=passwd \ 61 | -e MYSQL_PORT=3306 \ 62 | -e MYSQL_HOST=127.0.0.1 \ 63 | "${{ inputs.wescale_image }}" \ 64 | /vt/examples/wesql-server/init_single_node_cluster.sh 65 | 66 | - name: Wait for Target Cluster to be ready 67 | shell: bash 68 | run: | 69 | for i in {1..60}; do 70 | if nc -z localhost 15306; then 71 | echo "MySQL port 15306 is ready!" 72 | exit 0 73 | fi 74 | echo "Waiting for MySQL port 15306..." 75 | sleep 5 76 | done 77 | echo "Timeout waiting for MySQL port 15306" 78 | exit 1 79 | 80 | - name: Create Branch On Target Cluster 81 | shell: bash 82 | env: 83 | SOURCE_HOST: ${{ inputs.source_host }} 84 | SOURCE_PORT: ${{ inputs.source_port }} 85 | SOURCE_USER: ${{ inputs.source_user }} 86 | INCLUDE_DATABASES: ${{ inputs.include_databases }} 87 | EXCLUDE_DATABASES: ${{ inputs.exclude_databases }} 88 | SOURCE_PASSWORD: ${{ inputs.source_password }} 89 | run: | 90 | mysql -h127.0.0.1 -P15306 -u root -ppasswd -e "Branch create with ( 91 | 'source_host'='${SOURCE_HOST}', 92 | 'source_port'='${SOURCE_PORT}', 93 | 'source_user'='${SOURCE_USER}', 94 | 'source_password'='${SOURCE_PASSWORD}', 95 | 'include_databases'='${INCLUDE_DATABASES}', 96 | 'exclude_databases'='${EXCLUDE_DATABASES}' 97 | );" 98 | 99 | mysql -h127.0.0.1 -P15306 -u root -ppasswd -e "Branch show;" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MySQL Branch Action 2 | 3 | ![Database Icon](https://img.icons8.com/ios-filled/50/000000/database.png) 4 | 5 | **Automate MySQL Branch in Your CI/CD Pipelines** 6 | 7 | ## Table of Contents 8 | 9 | - [Introduction](#introduction) 10 | - [Example Workflow](#example-workflow) 11 | - [Features](#features) 12 | - [Inputs](#inputs) 13 | - [Secrets](#secrets) 14 | - [Usage](#usage) 15 | - [Further Reading](#further-reading) 16 | - [License](#license) 17 | 18 | ## Introduction 19 | 20 | The **MySQL Branch Action** is an open-source GitHub Action designed to streamline the creation and management of MySQL database branches within your CI/CD pipelines. By leveraging [WeScale](https://github.com/wesql/wescale/blob/main/doc/toturial/08-Branch.md), this action enables you to create isolated database environments for testing, development, and deployment without impacting your production data. Similar to platforms like Neon and PlanetScale, this solution offers scalable and efficient database branching to enhance your development workflow. 21 | 22 | ## Example Workflow 23 | 24 | Integrate this action into your GitHub Actions workflow to automate the creation of MySQL database branches. This setup ensures isolated environments for testing and development, enhancing your CI/CD pipeline's reliability and efficiency. 25 | 26 | ```yaml 27 | name: Branch Example 28 | 29 | on: 30 | push: 31 | 32 | jobs: 33 | setup-mysql: 34 | runs-on: ubuntu-latest 35 | steps: 36 | - name: Checkout Repository 37 | uses: actions/checkout@v3 38 | 39 | - name: Create MySQL Branch 40 | uses: wesql/mysql-branch-action@v0.0.7 41 | with: 42 | source_host: 'your-source-host.com' 43 | source_port: '3306' # Optional 44 | source_user: 'your_user' # Optional 45 | source_password: ${{ secrets.SOURCE_PASSWORD }} 46 | include_databases: '*' # Optional 47 | exclude_databases: 'information_schema,mysql,performance_schema,sys' # Optional 48 | wescale_image: 'apecloud/apecloud-mysql-scale:0.3.8' # Optional 49 | 50 | - name: Do Your Schema Migration 51 | run: | 52 | mysql -h127.0.0.1 -P15306 -e "create database if not exists foobar" 53 | mysql -h127.0.0.1 -P15306 -e "create table if not exists foobar.account (id int primary key, name varchar(255))" 54 | 55 | - name: Branch Diff 56 | run: | 57 | mysql -h127.0.0.1 -P15306 -e "Branch diff" 58 | 59 | - name: Branch Prepare Merge Back 60 | run: | 61 | mysql -h127.0.0.1 -P15306 -e "Branch prepare_merge_back" 62 | 63 | - name: Branch Merge Back 64 | run: | 65 | mysql -h127.0.0.1 -P15306 -e "Branch merge_back" 66 | 67 | - name: Branch Show 68 | run: | 69 | mysql -h127.0.0.1 -P15306 -e "Branch show" 70 | ``` 71 | 72 | ### Explanation 73 | 74 | - **Jobs**: 75 | - **Create MySQL Branch**: Sets up a MySQL branch with the same schema as the source database, creating an isolated environment for testing and development. 76 | - **Do Your Schema Migration**: You can perform schema migrations in the branch environment, without affecting the source database. 77 | - **Branch Diff**: Compares differences between the source and branch databases. 78 | - **Branch Prepare Merge Back**: Generates DDL statements to merge the branch changes back into the source database. 79 | - **Branch Merge Back**: Actually merges the branch changes back into the source database. 80 | - **Branch Show**: Displays the current state of the branches. 81 | 82 | ## Inputs 83 | 84 | | Name | Description | Required | Default | 85 | | ------------------- | ------------------------------------------------------------- | -------- | --------------------------------------------- | 86 | | `source_host` | Hostname or IP address of the source MySQL server. | Yes | N/A | 87 | | `source_port` | Port number of the source MySQL server. | No | `3306` | 88 | | `source_user` | Username for the source MySQL server. | No | `root` | 89 | | `source_password` | Password for the source MySQL server. | Yes | N/A | 90 | | `include_databases` | Comma-separated list of databases to include in the branch. | No | `*` | 91 | | `exclude_databases` | Comma-separated list of databases to exclude from the branch. | No | `information_schema,mysql,performance_schema,sys` | 92 | | `wescale_image` | Docker image tag for WeScale. | No | `apecloud/apecloud-mysql-scale:0.3.8` | 93 | 94 | ## Secrets 95 | 96 | To securely handle sensitive information, this action uses GitHub Secrets. You need to define the following secret in your repository: 97 | 98 | | Secret Name | Description | 99 | | ----------------- | ------------------------------------ | 100 | | `SOURCE_PASSWORD` | Password for the source MySQL server. | 101 | 102 | ### Setting Up Secrets 103 | 104 | 1. **Navigate to Your Repository**: 105 | - Go to your GitHub repository. 106 | 107 | 2. **Access Settings**: 108 | - Click on the `Settings` tab. 109 | 110 | 3. **Add Secrets**: 111 | - In the sidebar, click on `Secrets and variables` > `Actions`. 112 | - Click the `New repository secret` button. 113 | 114 | 4. **Define Secret**: 115 | - **Name**: Enter `SOURCE_PASSWORD`. 116 | - **Value**: Enter your source MySQL password. 117 | - Click `Add secret`. 118 | 119 | ## Usage 120 | 121 | Integrate this action into your GitHub Actions workflow to automate the creation of MySQL database branches. This setup ensures isolated environments for testing and development, enhancing your CI/CD pipeline's reliability and efficiency. 122 | 123 | ## Further Reading 124 | 125 | For detailed information on managing branches with WeScale, refer to the [WeScale Branch Tutorial](https://github.com/wesql/wescale/blob/main/doc/toturial/08-Branch.md). 126 | 127 | ## License 128 | 129 | This project is licensed under the [MIT License](LICENSE). 130 | --------------------------------------------------------------------------------