diff --git a/.github/actions/superset-backup/README.md b/.github/actions/superset-backup/README.md new file mode 100644 index 0000000..ff486e8 --- /dev/null +++ b/.github/actions/superset-backup/README.md @@ -0,0 +1,34 @@ +# superset-backup Shared GitHub action + + +The `superset-backup` is a parameterised reusable GitHub action that exports dashboard config from a Superset instance and saves it as a zip file in the repository that uses the action. + +This action uses the Superset API to authenticate and export dashboard config. +Backups are saved in `/superset/backups` folder, as zip files named based on the date and time when the backups ware generated. eg. `/superset/backups/20240918082215.zip`. +The format of the export is controlled by Superset and no additional transformation is done within this action. + +Requires the following variables: +- `gh_token`: GitHub token that has access to push to the main branch of the repository. +- `superset_link`: Http link to the superset instance. Eg: https://superset.app.com +- `superset_user`: Superset username that has access to the superset API +- `superset_password`: Password of the Superset user + +## Example GitHub Step + +``` +name: Example GitHub Workflow yml + +on: ['push'] + +jobs: + deployment: + runs-on: ubuntu-latest + steps: + - name: Make backup + uses: 'medic/cht-sync/.github/actions/superset-backup@main' + with: + gh_token: ${{ secrets.GH_TOKEN }} + superset_link: ${{ secrets.SUPERSET_LINK }} + superset_user: ${{ secrets.SUPERSET_USER }} + superset_password: ${{ secrets.SUPERSET_PASSWORD }} +``` diff --git a/.github/actions/superset-backup/action.yml b/.github/actions/superset-backup/action.yml new file mode 100644 index 0000000..a7d340c --- /dev/null +++ b/.github/actions/superset-backup/action.yml @@ -0,0 +1,36 @@ +name: 'superset-backup' +description: 'Download and save Superset dashboard export in a GH repository' +inputs: + gh_token: + description: 'The token to use to access the GitHub API' + required: true + superset_link: + description: 'The link to the superset server' + required: true + superset_user: + description: 'The user to use to access the Superset API' + required: true + superset_password: + description: 'The password to use to access the Superset API' + required: true + +runs: + using: 'composite' + steps: + - uses: actions/checkout@master + - name: Download superset export + shell: bash + run: | + NOW=$(date +%Y%m%d%H%M%S) + mkdir -p ./superset/backups + LOGIN=$(curl -X POST -d '{"username":"${{ inputs.superset_user }}","password":"${{ inputs.superset_password }}","provider":"db"}' -H "Content-Type: application/json" ${{ inputs.superset_link }}/api/v1/security/login) + ACCESS_TOKEN=$(echo $LOGIN | sed "s/{.*\"access_token\":\"\([^\"]*\).*}/\1/g") + CSRF=$(curl "${{ inputs.superset_link }}/api/v1/security/csrf_token/" -H "Authorization: Bearer $ACCESS_TOKEN") + CSRF_TOKEN=$(echo $CSRF | sed "s/{.*\"result\":\"\([^\"]*\).*}/\1/g") + curl -X GET -0 ${{ inputs.superset_link }}/api/v1/assets/export/ -H "Authorization: Bearer $ACCESS_TOKEN" -H "X-CSRFToken: $CSRF_TOKEN" -o ./superset/backups/$NOW.zip + - name: Upload superset export + uses: actions-js/push@master + with: + github_token: ${{ inputs.gh_token }} + message: 'chore: Superset autobackup' +