From 0be1599a8420c0316cd63aafaa52699e3b3a6321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Calzado?= <56086628+moicalcob@users.noreply.github.com> Date: Tue, 4 Jun 2024 13:29:33 +0200 Subject: [PATCH] Add CI to deploy changes generated in a branch (#545) * Add preflight to check egress requirements * Add the new preflight * Add CI to deploy changes generated in a branch * Fix * Fix * Add step to deploy changes * Fix * Fix channel name * Add workflow to delete channel * Finish delete channel * Fix * Fixes * Fixes * Last changes * Last one * Fix --- .github/workflows/branch-changes-delete.yaml | 71 +++++++++++++++++++ .github/workflows/branch-changes-release.yaml | 63 ++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 .github/workflows/branch-changes-delete.yaml create mode 100644 .github/workflows/branch-changes-release.yaml diff --git a/.github/workflows/branch-changes-delete.yaml b/.github/workflows/branch-changes-delete.yaml new file mode 100644 index 00000000..67deeb66 --- /dev/null +++ b/.github/workflows/branch-changes-delete.yaml @@ -0,0 +1,71 @@ +name: Delete Branch Changes +on: + pull_request: + types: [closed, unlabeled] # Trigger on close and unlabel events in pull requests + +jobs: + delete-changes: + timeout-minutes: 3 + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install Replicated CLI + shell: bash + run: | + curl -s https://api.github.com/repos/replicatedhq/replicated/releases/latest \ + | grep "browser_download_url.*linux_amd64.tar.gz" \ + | cut -d : -f 2,3 \ + | tr -d \" \ + | wget -O replicated.tar.gz -qi - + tar xf replicated.tar.gz replicated && rm replicated.tar.gz + mv replicated /usr/local/bin/replicated + + - name: Generate channel name + id: generate-channel-info + shell: bash + run: | + GITHUB_BRANCH_NAME=${GITHUB_HEAD_REF} + + # Cleaning refs/*/ + # refs/whatever/main => main + export GITHUB_BRANCH_NAME=${GITHUB_BRANCH_NAME#refs/[a-z]*/} + + # Normalize image tags characters + # my-branch/test-01 => my-branch_test-01 + export NORMALIZED_GIT_BRANCH_NAME=${GITHUB_BRANCH_NAME//[^[:alnum:]]/_} + + # To lowercase all characters + export NORMALIZED_GIT_BRANCH_NAME=${NORMALIZED_GIT_BRANCH_NAME,,} + + # Set maximum 128 characters for image name + export NORMALIZED_GIT_BRANCH_NAME=${NORMALIZED_GIT_BRANCH_NAME:0:127} + + # Share the variable for further steps + echo "channel-name=${NORMALIZED_GIT_BRANCH_NAME}" >> "$GITHUB_OUTPUT" + echo "version=$(yq .version -r chart/Chart.yaml)" >> "$GITHUB_OUTPUT" + + - name: Check if channel assigned to PR exists + id: check-if-channel-exists + shell: bash + env: + REPLICATED_APP: carto + REPLICATED_API_TOKEN: ${{ secrets.REPLICATED_API_TOKEN }} + run: | + CHANNEL_DELETE_REQUIRED="false" + if replicated channel ls | grep -q ${{ steps.generate-channel-info.outputs.channel-name }}; then + CHANNEL_DELETE_REQUIRED="true" + fi + + echo "channel-exists=${CHANNEL_DELETE_REQUIRED}" >> ${GITHUB_OUTPUT} + + - name: Delete channel + if: steps.check-if-channel-exists.outputs.channel-exists == 'true' + shell: bash + env: + REPLICATED_APP: carto + REPLICATED_API_TOKEN: ${{ secrets.REPLICATED_API_TOKEN }} + run: | + CHANNEL_ID=$(replicated channel ls | grep ${{ steps.generate-channel-info.outputs.channel-name }} | awk '{print $1}') + replicated channel rm $CHANNEL_ID diff --git a/.github/workflows/branch-changes-release.yaml b/.github/workflows/branch-changes-release.yaml new file mode 100644 index 00000000..807b59ca --- /dev/null +++ b/.github/workflows/branch-changes-release.yaml @@ -0,0 +1,63 @@ +name: Release Branch Changes +on: + pull_request: + types: [labeled, synchronize] # Trigger on label and push events in pull requests + +jobs: + release-changes: + if: | + github.event.label.name == 'release-changes' || ( + github.event_name == 'pull_request' && + contains(github.event.pull_request.labels.*.name, 'release-changes') + ) + timeout-minutes: 3 + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Generate channel name + id: generate-channel-info + shell: bash + run: | + GITHUB_BRANCH_NAME=${GITHUB_HEAD_REF} + + # Cleaning refs/*/ + # refs/whatever/main => main + export GITHUB_BRANCH_NAME=${GITHUB_BRANCH_NAME#refs/[a-z]*/} + + # Normalize image tags characters + # my-branch/test-01 => my-branch_test-01 + export NORMALIZED_GIT_BRANCH_NAME=${GITHUB_BRANCH_NAME//[^[:alnum:]]/_} + + # To lowercase all characters + export NORMALIZED_GIT_BRANCH_NAME=${NORMALIZED_GIT_BRANCH_NAME,,} + + # Set maximum 128 characters for image name + export NORMALIZED_GIT_BRANCH_NAME=${NORMALIZED_GIT_BRANCH_NAME:0:127} + + # Share the variable for further steps + echo "channel-name=${NORMALIZED_GIT_BRANCH_NAME}" >> "$GITHUB_OUTPUT" + echo "version=$(yq .version -r chart/Chart.yaml)" >> "$GITHUB_OUTPUT" + + - name: Publish changes into channel + uses: ./.github/actions/publish-release + with: + replicated-channel: ${{ steps.generate-channel-info.outputs.channel-name }} + version: ${{ steps.generate-channel-info.outputs.version }} + release-notes: "CARTO Self-Hosted dev version generated from branch ${{ steps.generate-channel-info.outputs.channel-name }}" + trigger-action: "dev" + gcloud-service-account: ${{ secrets.CARTO_ARTIFACTS_SERVICE_ACCOUNT }} + replicated-api-token: ${{ secrets.REPLICATED_API_TOKEN }} + + - name: Comment on PR + uses: actions/github-script@v7 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `Changes release to Replicated on channel ${{ steps.generate-channel-info.outputs.channel-name }}! Check the [Replicated Dashboard](https://vendor.replicated.com/apps/carto/channels) for more details. + If you need to test the changes, you can assign the channel to your customer and download the latest version from the Admin Console.` + })