Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not clean environment after ci and more #19

Merged
merged 16 commits into from
Oct 20, 2023
44 changes: 35 additions & 9 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ jobs:
source .tinyenv
echo "GIT_BRANCH=$GIT_BRANCH" >> $GITHUB_ENV
echo "_ENV_FLAGS=$_ENV_FLAGS" >> $GITHUB_ENV
echo "_NORMALIZED_ENV_NAME=$_NORMALIZED_ENV_NAME" >> $GITHUB_ENV

- name: Install Tinybird CLI
run: pip install tinybird-cli
run: |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

- name: Tinybird version
run: tb --version
Expand Down Expand Up @@ -89,7 +95,7 @@ jobs:
- name: Deploy changes to the main Workspace
run: |
source .tinyenv
if ${{ inputs.tb_deploy}}; then
if ${{ inputs.tb_deploy }}; then
tb env deploy --semver ${VERSION} --wait
tb release ls
else
Expand Down Expand Up @@ -129,7 +135,12 @@ jobs:
[[ "${{ secrets.tb_admin_token }}" ]] || { echo "Go to the tokens section in your Workspace, copy the 'admin token (user@domain.com)' associated to a user account and set TB_ADMIN_TOKEN as a Secret in your Git repository"; exit 1; }

- name: Install Tinybird CLI
run: pip install tinybird-cli
run: |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

- name: Tinybird version
run: tb --version
Expand All @@ -143,7 +154,7 @@ jobs:
--yes

release_promote:
if: ${{ inputs.tb_deploy}}
if: ${{ inputs.tb_deploy && github.event.workflow_dispatch }}
Copy link
Member Author

@alrocar alrocar Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to run these jobs manually

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I added the if in this PR not to be triggered but I guess they were executed automatically

runs-on: ubuntu-latest
defaults:
run:
Expand All @@ -161,7 +172,12 @@ jobs:
[[ "${{ secrets.tb_admin_token }}" ]] || { echo "Go to the tokens section in your Workspace, copy the 'admin token (user@domain.com)' associated to a user account and set TB_ADMIN_TOKEN as a Secret in your Git repository"; exit 1; }

- name: Install Tinybird CLI
run: pip install tinybird-cli
run: |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

- name: Tinybird version
run: tb --version
Expand All @@ -176,7 +192,7 @@ jobs:
--semver $VERSION

release_rollback:
if: ${{ inputs.tb_deploy}}
if: ${{ inputs.tb_deploy && github.event.workflow_dispatch }}
runs-on: ubuntu-latest
defaults:
run:
Expand All @@ -194,7 +210,12 @@ jobs:
[[ "${{ secrets.tb_admin_token }}" ]] || { echo "Go to the tokens section in your Workspace, copy the 'admin token (user@domain.com)' associated to a user account and set TB_ADMIN_TOKEN as a Secret in your Git repository"; exit 1; }

- name: Install Tinybird CLI
run: pip install tinybird-cli
run: |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

- name: Tinybird version
run: tb --version
Expand All @@ -208,7 +229,7 @@ jobs:
release rollback

release_rm:
if: ${{ inputs.tb_deploy}}
if: ${{ inputs.tb_deploy && github.event.workflow_dispatch }}
runs-on: ubuntu-latest
defaults:
run:
Expand All @@ -226,7 +247,12 @@ jobs:
[[ "${{ secrets.tb_admin_token }}" ]] || { echo "Go to the tokens section in your Workspace, copy the 'admin token (user@domain.com)' associated to a user account and set TB_ADMIN_TOKEN as a Secret in your Git repository"; exit 1; }

- name: Install Tinybird CLI
run: pip install tinybird-cli
run: |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

- name: Tinybird version
run: tb --version
Expand Down
59 changes: 48 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
defaults:
run:
working-directory: ${{ inputs.data_project_dir }}
if: ${{ github.event.action != 'closed' }}
steps:
- uses: actions/checkout@master
with:
Expand All @@ -43,8 +44,15 @@ jobs:
source .tinyenv
echo "GIT_BRANCH=$GIT_BRANCH" >> $GITHUB_ENV
echo "_ENV_FLAGS=$_ENV_FLAGS" >> $GITHUB_ENV
echo "_NORMALIZED_ENV_NAME=$_NORMALIZED_ENV_NAME" >> $GITHUB_ENV

- name: Install Tinybird CLI
run: pip install tinybird-cli
run: |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

- name: Tinybird version
run: tb --version
Expand All @@ -55,12 +63,28 @@ jobs:
- name: Check auth
run: tb --host ${{ secrets.tb_host }} --token ${{ secrets.tb_admin_token }} auth info

- name: Try delete previous Environment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see how workflows evolve but it would be nice to create a proper github action to use our cli so we can encapsulate this kind of things as they getting complex (also getting advantage of our cli docker image).
Also taking into account how to reuse for other providers

run: |
output=$(tb --host ${{ secrets.tb_host }} --token ${{ secrets.tb_admin_token }} env ls)
ENVIRONMENT_NAME="tmp_ci_${_NORMALIZED_ENV_NAME}_${{ github.event.pull_request.number }}"

# Check if the environment name exists in the output
if echo "$output" | grep -q "\b$ENVIRONMENT_NAME\b"; then
tb \
--host ${{ secrets.tb_host }} \
--token ${{ secrets.tb_admin_token }} \
env rm $ENVIRONMENT_NAME \
--yes
else
echo "Skipping clean up: The Environment '$ENVIRONMENT_NAME' does not exist."
fi

- name: Create new test Environment with data
run: |
tb \
--host ${{ secrets.tb_host }} \
--token ${{ secrets.tb_admin_token }} \
env create tmp_ci_${_NORMALIZED_ENV_NAME}_${GITHUB_RUN_ID} \
env create tmp_ci_${_NORMALIZED_ENV_NAME}_${{ github.event.pull_request.number }} \
${_ENV_FLAGS}

- name: List changes with Main Environment
Expand Down Expand Up @@ -98,6 +122,7 @@ jobs:

- name: Run pipe regression tests
run: |
source .tinyenv
echo ${{ steps.regression_labels.outputs.labels }}
REGRESSION_LABELS=$(echo "${{ steps.regression_labels.outputs.labels }}" | awk -F, '{for (i=1; i<=NF; i++) if ($i ~ /^--/) print $i}' ORS=',' | sed 's/,$//')
echo ${REGRESSION_LABELS}
Expand All @@ -107,8 +132,7 @@ jobs:
defaults:
run:
working-directory: ${{ inputs.data_project_dir }}
if: ${{ always() }}
needs: [ci_branching]
if: ${{ github.event.action == 'closed' }}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this requires users to add the closed event to their ci template, otherwise it won't be executed on merge/close

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Let's remember to add in our generated yml from cli

steps:
- uses: actions/checkout@master
- uses: actions/setup-python@v3
Expand All @@ -121,15 +145,28 @@ jobs:
[[ "${{ secrets.tb_admin_token }}" ]] || { echo "Go to the tokens section in your Workspace, copy the 'admin token (user@domain.com)' associated to a user account and set TB_ADMIN_TOKEN as a Secret in your Git repository"; exit 1; }

- name: Install Tinybird CLI
run: pip install tinybird-cli
run: |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

- name: Tinybird version
run: tb --version

- name: Drop test Environment
- name: Try delete previous Environment
run: |
tb \
--host ${{ secrets.tb_host }} \
--token ${{ secrets.tb_admin_token }} \
env rm tmp_ci_${_NORMALIZED_ENV_NAME}_${GITHUB_RUN_ID} \
--yes
output=$(tb --host ${{ secrets.tb_host }} --token ${{ secrets.tb_admin_token }} env ls)
ENVIRONMENT_NAME="tmp_ci_${_NORMALIZED_ENV_NAME}_${{ github.event.pull_request.number }}"

# Check if the environment name exists in the output
if echo "$output" | grep -q "\b$ENVIRONMENT_NAME\b"; then
tb \
--host ${{ secrets.tb_host }} \
--token ${{ secrets.tb_admin_token }} \
env rm $ENVIRONMENT_NAME \
--yes
else
echo "Skipping clean up: The Environment '$ENVIRONMENT_NAME' does not exist."
fi
71 changes: 61 additions & 10 deletions .gitlab/ci_cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ variables:
- source .venv/bin/activate

# Install Tinybird CLI
- pip install tinybird-cli
- |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

# Tinybird version
- tb --version
Expand All @@ -44,14 +49,30 @@ variables:
- tb check

# Check auth
tb --host $TB_HOST --token $TB_ADMIN_TOKEN auth info
- tb --host $TB_HOST --token $TB_ADMIN_TOKEN auth info

# Try delete previous Environment
- |
output=$(tb --host $TB_HOST --token $TB_ADMIN_TOKEN env ls)
ENVIRONMENT_NAME="tmp_ci_${_NORMALIZED_ENV_NAME}_${CI_MERGE_REQUEST_IID}"

# Check if the environment name exists in the output
if echo "$output" | grep -q "\b$ENVIRONMENT_NAME\b"; then
tb \
--host $TB_HOST \
--token $TB_ADMIN_TOKEN \
env rm $ENVIRONMENT_NAME \
--yes
else
echo "Skipping clean up: The Environment '$ENVIRONMENT_NAME' does not exist."
fi

# Create new test Environment with data
- |
tb \
--host $TB_HOST \
--token $TB_ADMIN_TOKEN \
env create tmp_ci_${_NORMALIZED_ENV_NAME}_${CI_COMMIT_SHORT_SHA} \
env create tmp_ci_${_NORMALIZED_ENV_NAME}_${CI_MERGE_REQUEST_IID} \
${_ENV_FLAGS}

# List changes with Main Environment
Expand Down Expand Up @@ -101,7 +122,12 @@ variables:
- source .venv/bin/activate

# Install Tinybird CLI
- pip install tinybird-cli
- |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

# Tinybird version
- tb --version
Expand Down Expand Up @@ -167,7 +193,12 @@ variables:
- source .venv/bin/activate

# Install Tinybird CLI
- pip install tinybird-cli
- |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

# Tinybird version
- tb --version
Expand All @@ -177,7 +208,7 @@ variables:
tb \
--host $TB_HOST \
--token $TB_ADMIN_TOKEN \
env rm tmp_ci_${_NORMALIZED_ENV_NAME}_${CI_COMMIT_SHORT_SHA} \
env rm tmp_ci_${_NORMALIZED_ENV_NAME}_${CI_MERGE_REQUEST_IID} \
--yes

.cleanup_cd_branch:
Expand All @@ -193,7 +224,12 @@ variables:
- source .venv/bin/activate

# Install Tinybird CLI
- pip install tinybird-cli
- |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

# Tinybird version
- tb --version
Expand All @@ -217,7 +253,12 @@ variables:
- source .venv/bin/activate

# Install Tinybird CLI
- pip install tinybird-cli
- |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

# Tinybird version
- tb --version
Expand All @@ -242,7 +283,12 @@ variables:
- source .venv/bin/activate

# Install Tinybird CLI
- pip install tinybird-cli
- |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

# Tinybird version
- tb --version
Expand All @@ -266,7 +312,12 @@ variables:
- source .venv/bin/activate

# Install Tinybird CLI
- pip install tinybird-cli
- |
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
pip install tinybird-cli
fi

# Tinybird version
- tb --version
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Next release
============

- Support for a `requirements.txt` file inside the Data Project folder. That way you can control which version of the `tinybird-cli` to install.
- Environments in CI are now created with a fixed name using the Pull Request number.
- Environments are not cleaned up after CI finishes. This is a very convenient workflow to debug issues directly in the Environment with the changes of the branch deployed.
- Users updating from previous versions need to do some actions:
- GitHub: Add the `closed` type like [this](https://github.com/tinybirdco/ci_analytics/pull/12/commits/01a207ab2dac38a18ea76c81b0b3087ad3f9cb91).
- GitLab: Change the rule to run the clean up job on merge:

```yaml
- &cli_cleanup_rule
if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
changes:
- .gitlab-ci.yml
- ./**/*
when: always
```
- If you have doubts when updating just drop the .github or .gitlab-ci.yml workflow and re-run `tb init --git` using the latest version of `tinybird-cli` to re-generate the CI/CD templates.
- `.tinyenv` now supports `export OBFUSCATE_REGEX_PATTERN=<regex>` to have a list of regex separated by `|` to obfuscate the output of regression tests. It requires version 1.0.1 of tinybird-cli.