-
Notifications
You must be signed in to change notification settings - Fork 45
216 lines (204 loc) · 15.9 KB
/
GitHub-Flyway-CICD-Pipeline_Linux.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# This is a basic workflow to help you get started with Actions
name: GitHub-Flyway-AutoPilot-Pipeline-Linux
on:
pull_request:
branches:
- main
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
env:
# Enable this for additional debug logging
ACTIONS_RUNNER_DEBUG: false
### Step 1: Define Environment Secrets ###
### Environment Secrets - Create Environment Secrets ###
### Navigate to Settings > Secrets & Variables > Actions
# FLYWAY_EMAIL: Enter the email address linked to the Redgate Account that created the PAT
# FLYWAY_TOKEN: Enter the PAT Value (This should be treated like a password and thus as a secure variable.
# TARGET_DATABASE_USERNAME: Leave blank if using integratedSecurity (default).
# TARGET_DATABASE_PASSWORD: Leave blank if using integratedSecurity (default).
# CUSTOM_PARAMS: Optional - Used for passing custom Flyway Parameters to each Flyway command
### End of Environment Variables ###
# Step 3: Authenticate Flyway with Personal Access Tokens (PATs)
# Details on how to do this can be found here: https://documentation.red-gate.com/flyway/flyway-cli-and-api/tutorials/tutorial-personal-access-tokens
# Documentation on all available Authentication methods can be found here: https://documentation.red-gate.com/fd/flyway-licensing-263061944.html
FLYWAY_EMAIL: "${{ secrets.FLYWAY_EMAIL }}" # Enter the email address linked to the Redgate Account that created the PAT
FLYWAY_TOKEN: "${{ secrets.FLYWAY_TOKEN }}" # Enter the PAT Value (This should be treated like a password and thus as a secure variable.
BASELINE_VERSION: "001" # This should match the version number of your baseline script
FIRST_UNDO_SCRIPT: "002" # This should match the first undo version in your project
# Optional - Validate Flyway CLI installation for ephemeral agents
FLYWAY_CLI_INSTALL_CHECK: "${{ secrets.FLYWAY_CLI_INSTALL_CHECK }}" # Setting to false will skip the Flyway CLI check step
FLYWAY_VERSION: "Latest" # This outlines the version of Flyway CLI that will be downloaded if no Flyway CLI is detected on the target agent (Examples - '11.0.0' for specific version. Or 'Latest' for latest version)
FLYWAY_INSTALL_DIRECTORY: "" # The path on the agent machine where Flyway CLI will be installed
# Optional: Side Quest #1 - Setup Flyway Pipeline Integration - https://flyway.red-gate.com/ For More Details
FLYWAY_PUBLISH_RESULT: "true" # Set this value to true to enable Flyway Pipelines and track your releases centrally!
FLYWAY_DRIFT_ON_MIGRATE: "true" # Set this value to true to enable Flyway Pipelines drift detection and track your drift centrally!
### DO NOT EDIT BELOW THIS LINE - All variables set in the above section will be consumed by the jobs below and therefore do not require any updates to function ###
jobs:
# This workflow contains a single job called "build"
build:
name: Deploy Build
# The type of runner that the job will run on
runs-on: "self-hosted" # Options - self-hosted/ubuntu-latest/windows-latest (See here for more details on GitHub hosted runners - https://docs.github.com/en/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners)
environment: 'build'
env:
stage: 'Build'
displayName: 'Build'
DATABASE_NAME: 'AutoPilotBuild'
ENVIRONMENT: 'Build' # This variable refers to the environment name present in the Flyway Projects TOML file.
TARGET_DATABASE_USERNAME: "${{ secrets.TARGET_DATABASE_USERNAME }}"
TARGET_DATABASE_PASSWORD: "${{ secrets.TARGET_DATABASE_PASSWORD }}"
CUSTOM_PARAMS: "${{ secrets.CUSTOM_PARAMS }}" # Secure method of adding custom Flyway Parameters (Example -X for debug)
EXECUTE_BUILD: 'true' # Turn to false to skip the build stage tasks
publishArtifacts: 'true' # Turn to false to skip the artifact upload
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4
# Optional - If Enabled, Flyway CLI will be validated and installed if not present
- name: Flyway - CLI Automatic Validation and Install
run: bash ${{ GITHUB.WORKSPACE }}/Scripts/Flyway_DownloadAndInstallCLI_Unix.sh
if: env.FLYWAY_CLI_INSTALL_CHECK == 'true'
# Step 1 - Flyway License Authentication #
- name: Flyway Authentication
if: ${{ env.EXECUTE_BUILD == 'true' && success() }}
run: |
flyway auth -IAgreeToTheEula -email="${{ env.FLYWAY_EMAIL }}" -token="${{ env.FLYWAY_TOKEN }}"
# Step 2 - Ensure the Build Database is cleaned of all objects, meaning the build starts from scratch
- name: Clean Build DB
if: ${{ env.EXECUTE_BUILD == 'true' && success() }}
run: |
flyway info clean info -environment="${{ env.ENVIRONMENT }}" -user="${{ env.TARGET_DATABASE_USERNAME }}" -password="${{ env.TARGET_DATABASE_PASSWORD }}" -configFiles="${{ GITHUB.WORKSPACE }}/flyway.toml" -locations="filesystem:${{ GITHUB.WORKSPACE }}/migrations" -cleanDisabled='false' ${{ env.CUSTOM_PARAMS }}
# Step 3 - Migrate all scripts from the migration folder, to verify they can be deployed against an empty database. This is the quickest way to get feedback about problematic scripts
- name: Migrate Build DB
if: ${{ env.EXECUTE_BUILD == 'true' && success() }}
run: |
flyway info migrate info -environment="${{ env.ENVIRONMENT }}" -user="${{ env.TARGET_DATABASE_USERNAME }}" -password="${{ env.TARGET_DATABASE_PASSWORD }}" -configFiles="${{ GITHUB.WORKSPACE }}/flyway.toml" -locations="filesystem:${{ GITHUB.WORKSPACE }}/migrations" -cleanDisabled='false' -baselineOnMigrate="true" "-publishResult=${{ env.FLYWAY_PUBLISH_RESULT }}" "-flywayServicePublish.publishReport=${{ env.FLYWAY_PUBLISH_RESULT }}" "-reportEnabled=${{ env.FLYWAY_PUBLISH_RESULT }}" ${{ env.CUSTOM_PARAMS }}
# Step 4 (Optional) - Undo all relevant migrations using corresponding rollback script, to validate they can be run. This helps to reduce the chance the script will fail during a real life rollback scenario
- name: undo Build DB
if: ${{ env.EXECUTE_BUILD == 'true' && success() }}
run: |
flyway info undo info -environment="${{ env.ENVIRONMENT }}" -user=${{ env.TARGET_DATABASE_USERNAME }} -password=${{ env.TARGET_DATABASE_PASSWORD }} -configFiles="${{ GITHUB.WORKSPACE }}/flyway.toml" -locations="filesystem:${{ GITHUB.WORKSPACE }}/migrations" -baselineOnMigrate="true" -cleanDisabled='false' -target="${{ env.FIRST_UNDO_SCRIPT }}" ${{ env.CUSTOM_PARAMS }}
# GitHub - After migration scripts are validated, publish them as an artifact
- name: Publish Validated Migration Scripts as Artifact
if: ${{ env.publishArtifacts == 'true' && success() }}
uses: actions/upload-artifact@v4
with:
name: flyway-build-artifact-${{ github.run_number }}
path: |
!${{ GITHUB.WORKSPACE }}/flyway-*/**/*
!${{ GITHUB.WORKSPACE }}/.git/**/*
!${{ GITHUB.WORKSPACE }}/.git*/**/*
${{ GITHUB.WORKSPACE }}/**/schema-model/**/*
${{ GITHUB.WORKSPACE }}/**/migrations/**/*
${{ GITHUB.WORKSPACE }}/**/Scripts/**/*
${{ GITHUB.WORKSPACE }}/**/backups/**/*
${{ GITHUB.WORKSPACE }}/**/flyway.toml
${{ GITHUB.WORKSPACE }}/**/Filter.scpf
test:
name: Deploy Test
# The type of runner that the job will run on
runs-on: "self-hosted" # Options - self-hosted/ubuntu-latest/windows-latest (See here for more details on GitHub hosted runners - https://docs.github.com/en/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners)
environment: 'test' # This refers to GitHub's Environment name
if: ${{ true }} #Set this variable to false to temporarily disable the job
needs: build
env:
stage: 'Test'
DATABASE_NAME: "AutoPilotTest"
ENVIRONMENT: "Test" # Refers to the environment in the Flyway Project TOML file.
TARGET_DATABASE_USERNAME: "${{ secrets.TARGET_DATABASE_USERNAME }}"
TARGET_DATABASE_PASSWORD: "${{ secrets.TARGET_DATABASE_PASSWORD }}"
REPORT_ENVIRONMENT: "Check"
REPORT_DATABASE_USERNAME: "${{ secrets.TARGET_DATABASE_USERNAME }}" # Optional - Change to a different secret if the connection details for the report database differs from the target
REPORT_DATABASE_PASSWORD: "${{ secrets.TARGET_DATABASE_PASSWORD }}" # Optional - Change to a different secret if the connection details for the report database differs from the target
CUSTOM_PARAMS: "${{ secrets.CUSTOM_PARAMS }}" # Secure method of adding custom Flyway Parameters (Example -X for debug)
generateReport: 'true' # Turn to false to skip the CHECK report stage tasks
FLYWAY_CHECK_DRIFT_ON_MIGRATE: 'true' # Turn to false to skip the Drift Report upload to Flyway Pipelines (If enabled)
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/download-artifact@v4
with:
name: flyway-build-artifact-${{ github.run_number }}
# Optional - List out all build artifact files on disk, helpful for debugging
- name: Display structure of downloaded files
run: ls -R
# Optional - If Enabled, Flyway CLI will be validated and installed if not present
- name: Flyway - CLI Automatic Validation and Install
run: bash ${{ GITHUB.WORKSPACE }}/Scripts/Flyway_DownloadAndInstallCLI_Unix.sh
if: env.FLYWAY_CLI_INSTALL_CHECK == 'true'
# Step 1 - Flyway License Authentication #
- name: Flyway Authentication
if: success()
run: |
flyway auth -IAgreeToTheEula -email="${{ env.FLYWAY_EMAIL }}" -token="${{ env.FLYWAY_TOKEN }}"
# Step 2 - Create Check Report
- name: Create Check Reports
if: ${{ env.generateReport == 'true' && success() }}
run: |
flyway check -dryrun -changes -drift -environment="${{ env.ENVIRONMENT }}" -user=${{ env.TARGET_DATABASE_USERNAME }} -password=${{ env.TARGET_DATABASE_PASSWORD }} -configFiles="${{ GITHUB.WORKSPACE }}/flyway.toml" -locations="filesystem:${{ GITHUB.WORKSPACE }}/migrations" "-check.buildEnvironment=${{ env.REPORT_ENVIRONMENT }}" "-environments.${{ env.REPORT_ENVIRONMENT }}.user=${{ env.REPORT_DATABASE_USERNAME }}" "-environments.${{ env.REPORT_ENVIRONMENT }}.password=${{ env.REPORT_DATABASE_PASSWORD }}" "-reportFilename=${{ GITHUB.WORKSPACE }}/reports/${{ env.DATABASE_NAME }}-Run-${{ GITHUB.RUN_ID }}-${{ env.ENVIRONMENT }}-Check-Report.html" "-publishResult=${{ env.FLYWAY_PUBLISH_RESULT }}" "-flywayServicePublish.publishReport=${{ env.FLYWAY_PUBLISH_RESULT }}" "-reportEnabled=${{ env.FLYWAY_PUBLISH_RESULT }}" ${{ env.CUSTOM_PARAMS }}
continue-on-error: true
# Step 3 - Publish Check Report
- name: Publish Check Report as Artifact
if: ${{ env.generateReport == 'true' && success() }}
uses: actions/upload-artifact@v4
with:
name: flyway-reports-test
path: ${{ GITHUB.WORKSPACE }}/reports/**/*
# Step 4 - Deploy pending migrations to target database
- name: Migrate Test DB
if: success()
run: |
flyway info migrate info -environment="${{ env.ENVIRONMENT }}" -user="${{ env.TARGET_DATABASE_USERNAME }}" -password="${{ env.TARGET_DATABASE_PASSWORD }}" -baselineOnMigrate="true" -configFiles="${{ GITHUB.WORKSPACE }}/flyway.toml" -locations="filesystem:${{ GITHUB.WORKSPACE }}/migrations" "-publishResult=${{ env.FLYWAY_PUBLISH_RESULT }}" "-flywayServicePublish.publishReport=${{ env.FLYWAY_PUBLISH_RESULT }}" "-reportEnabled=${{ env.FLYWAY_PUBLISH_RESULT }}" ${{ env.CUSTOM_PARAMS }}
prod:
name: Deploy Prod
# The type of runner that the job will run on
runs-on: "self-hosted" # Options - self-hosted/ubuntu-latest/windows-latest (See here for more details on GitHub hosted runners - https://docs.github.com/en/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners)
environment: 'prod'
if: ${{ true }} #Set this variable to false to temporarily disable the job
needs: test
env:
stage: 'Prod'
DATABASE_NAME: "AutoPilotProd"
ENVIRONMENT: "Prod" # Refers to the environment name in the Flyway Project TOML file.
TARGET_DATABASE_USERNAME: "${{ secrets.TARGET_DATABASE_USERNAME }}"
TARGET_DATABASE_PASSWORD: "${{ secrets.TARGET_DATABASE_PASSWORD }}"
REPORT_ENVIRONMENT: "Check"
REPORT_DATABASE_USERNAME: "${{ secrets.TARGET_DATABASE_USERNAME }}" # Optional - Change to a different secret if the connection details for the report database differs from the target
REPORT_DATABASE_PASSWORD: "${{ secrets.TARGET_DATABASE_PASSWORD }}" # Optional - Change to a different secret if the connection details for the report database differs from the target
CUSTOM_PARAMS: "${{ secrets.CUSTOM_PARAMS }}" # Secure method of adding custom Flyway Parameters (Example -X for debug)
generateReport: 'true' # Turn to false to skip the CHECK report stage tasks
FLYWAY_CHECK_DRIFT_ON_MIGRATE: 'true' # Turn to false to skip the Drift Report upload to Flyway Pipelines (If enabled)
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/download-artifact@v4
with:
name: flyway-build-artifact-${{ github.run_number }}
# Optional - List out all build artifact files on disk, helpful for debugging
- name: Display structure of downloaded files
run: ls -R
# Optional - If Enabled, Flyway CLI will be validated and installed if not present
- name: Flyway - CLI Automatic Validation and Install
run: bash ${{ GITHUB.WORKSPACE }}/Scripts/Flyway_DownloadAndInstallCLI_Unix.sh
if: env.FLYWAY_CLI_INSTALL_CHECK == 'true'
# Step 1 - Flyway License Authentication #
- name: Flyway Authentication
if: success()
run: |
flyway auth -IAgreeToTheEula -email="${{ env.FLYWAY_EMAIL }}" -token="${{ env.FLYWAY_TOKEN }}"
# Step 2 - Create Check Report
- name: Create Check Reports
if: ${{ env.generateReport == 'true' && success() }}
run: |
flyway check -dryrun -changes -drift -environment="${{ env.ENVIRONMENT }}" -user=${{ env.TARGET_DATABASE_USERNAME }} -password=${{ env.TARGET_DATABASE_PASSWORD }} -configFiles="${{ GITHUB.WORKSPACE }}/flyway.toml" -locations="filesystem:${{ GITHUB.WORKSPACE }}/migrations" "-check.buildEnvironment=${{ env.REPORT_ENVIRONMENT }}" "-environments.${{ env.REPORT_ENVIRONMENT }}.user=${{ env.REPORT_DATABASE_USERNAME }}" "-environments.${{ env.REPORT_ENVIRONMENT }}.password=${{ env.REPORT_DATABASE_PASSWORD }}" "-reportFilename=${{ GITHUB.WORKSPACE }}/reports/${{ env.DATABASE_NAME }}-Run-${{ GITHUB.RUN_ID }}-${{ env.ENVIRONMENT }}-Check-Report.html" "-publishResult=${{ env.FLYWAY_PUBLISH_RESULT }}" "-flywayServicePublish.publishReport=${{ env.FLYWAY_PUBLISH_RESULT }}" "-reportEnabled=${{ env.FLYWAY_PUBLISH_RESULT }}" ${{ env.CUSTOM_PARAMS }}
continue-on-error: true
# Step 3 - Publish Check Report
- name: Publish Check Report as Artifact
if: ${{ env.generateReport == 'true' && success() }}
uses: actions/upload-artifact@v4
with:
name: flyway-reports-prod
path: ${{ GITHUB.WORKSPACE }}/reports/**/*
# Step 4 - Deploy pending migrations to target database
- name: Migrate Production DB
if: success()
run: |
flyway info migrate info -environment="${{ env.ENVIRONMENT }}" -user="${{ env.TARGET_DATABASE_USERNAME }}" -password="${{ env.TARGET_DATABASE_PASSWORD }}" -baselineOnMigrate="true" -configFiles="${{ GITHUB.WORKSPACE }}/flyway.toml" -locations="filesystem:${{ GITHUB.WORKSPACE }}/migrations" "-publishResult=${{ env.FLYWAY_PUBLISH_RESULT }}" "-flywayServicePublish.publishReport=${{ env.FLYWAY_PUBLISH_RESULT }}" "-reportEnabled=${{ env.FLYWAY_PUBLISH_RESULT }}" ${{ env.CUSTOM_PARAMS }}