From 4c8b1392b34a3dc400a09e8f7dfa5f54beab6f19 Mon Sep 17 00:00:00 2001 From: ypoplavs Date: Thu, 1 Aug 2024 11:46:42 +0300 Subject: [PATCH 1/7] add sandbox --- .github/comment-template.md | 9 ++ .github/workflows/sandbox.yaml | 143 +++++++++++++++++++++++++++ goreleaser/.goreleaser-snapshot.yaml | 5 + 3 files changed, 157 insertions(+) create mode 100644 .github/comment-template.md create mode 100644 .github/workflows/sandbox.yaml diff --git a/.github/comment-template.md b/.github/comment-template.md new file mode 100644 index 00000000..ef6fc7b2 --- /dev/null +++ b/.github/comment-template.md @@ -0,0 +1,9 @@ +Sandbox environment for `{{ .branch_ref }}` has been created. + +## URLs + +- **Dashboard**: https://dashboard.{{ .branch_ref }}.testkube.dev +- **API**: https://api.{{ .branch_ref }}.testkube.dev +- **Agent**: https://agent.{{ .branch_ref }}.testkube.dev +- **Storage**: https://storage.{{ .branch_ref }}.testkube.dev +- **Websockets**: https://websockets.{{ .branch_ref }}.testkube.dev diff --git a/.github/workflows/sandbox.yaml b/.github/workflows/sandbox.yaml new file mode 100644 index 00000000..bd5a4107 --- /dev/null +++ b/.github/workflows/sandbox.yaml @@ -0,0 +1,143 @@ +name: Sanbox image to build on push to sandbox/ branch + +on: + push: + branches: + - sandbox/** + - ci/add-sandbox + pull_request: + types: + - opened + +permissions: + contents: write + id-token: write + pull-requests: write + issues: write + + +env: + ALPINE_IMAGE: alpine:3.18.3 + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Docker Buildx + if: startsWith(github.ref, 'refs/heads/sandbox/') + id: buildx + uses: docker/setup-buildx-action@v3 + + - name: Set up QEMU + if: startsWith(github.ref, 'refs/heads/sandbox/') + uses: docker/setup-qemu-action@v3 + + - name: Set up Go + if: startsWith(github.ref, 'refs/heads/sandbox/') + uses: actions/setup-go@v4 + + - name: Login to DockerHub + if: startsWith(github.ref, 'refs/heads/sandbox/') + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Set version + if: startsWith(github.ref, 'refs/heads/sandbox/') + run: | + #get short commit sha that triggered the flow + echo git_hash="$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_ENV + + # Extract everything before the first slash + branch_identifier=$(echo "$GITHUB_REF_NAME" | cut -d'/' -f2-) + + # Replace slashes with dashes using sed + echo branch_identifier=$(echo "$branch_identifier" | sed 's/\//-/g') >> $GITHUB_ENV + + - name: Set image tag + if: startsWith(github.ref, 'refs/heads/sandbox/') + run: | + # set image tag that includes service name, sandbox identifier and commit sha + image_tag="${{ matrix.service }}-${{ env.branch_identifier }}-${{ env.git_hash }}" + echo image_tag_sha=$image_tag >> $GITHUB_ENV + + - name: Build images with GoReleaser + if: startsWith(github.ref, 'refs/heads/sandbox/') + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release -f goreleaser/.goreleaser-snapshot.yaml --snapshot + env: + GITHUB_TOKEN: ${{ secrets.CI_BOT_TOKEN }} + DOCKER_BUILDX_BUILDER: "${{ steps.buildx.outputs.name }}" + DOCKER_BUILDX_CACHE_FROM: "type=gha" + DOCKER_BUILDX_CACHE_TO: "type=gha,mode=max" + ALPINE_IMAGE: ${{ env.ALPINE_IMAGE }} + SANDBOX_IMAGE: true + + - name: Push Docker images + if: startsWith(github.ref, 'refs/heads/sandbox/') + run: | + docker tag kubeshop/testkube-operator:${{ env.image_tag_sha }} kubeshop/testkube-sandbox:${{ env.image_tag_sha }} + docker push kubeshop/testkube-sandbox:${{ env.image_tag_sha }} + +# - name: Repository dispatch +# if: startsWith(github.ref, 'refs/heads/sandbox/') +# uses: peter-evans/repository-dispatch@v2 +# with: +# token: ${{ secrets.CI_BOT_TOKEN }} +# repository: kubeshop/testkube-deployment +# event-type: sandbox_image_update +# client-payload: '{"ref_name": "${{ github.ref_name }}", "sandbox_repo": "kubeshop/testkube-sandbox"}' + + - name: Output summary + if: startsWith(github.ref, 'refs/heads/sandbox/') + run: | + echo -e "### Sandbox Environment" >> $GITHUB_STEP_SUMMARY + echo -e '```' >> $GITHUB_STEP_SUMMARY + ## URLs + + - Dashboard: https://dashboard.${{ github.ref_name }}.testkube.dev + - API: https://api.${{ github.ref_name }}.testkube.dev + - Agent: https://agent.${{ github.ref_name }}.testkube.dev + - Storage: https://storage.${{ github.ref_name }}.testkube.dev + - Websockets: https://websockets.${{ github.ref_name }}.testkube.dev + echo -e '```' >> $GITHUB_STEP_SUMMARY + + - name: Get a branch name if PR is created + if: startsWith(github.event.pull_request.head.ref, 'sandbox/') + run: | + # get a branch name + branch_ref="${{ github.event.pull_request.head.ref }}" + + #remove slash + branch_ref="${branch_ref#*/}" + + #create env var + echo "branch_ref=$branch_ref" >> $GITHUB_ENV + + - name: Render template + if: startsWith(github.event.pull_request.head.ref, 'sandbox/') + id: template + uses: chuhlomin/render-template@v1.4 + with: + template: .github/comment-template.md + vars: | + branch_ref: ${{ env.branch_ref }} + + - name: Create comment on a PR with the endpoints + if: startsWith(github.event.pull_request.head.ref, 'sandbox/') + uses: peter-evans/create-or-update-comment@v4 + with: + token: ${{ secrets.CI_BOT_TOKEN }} + issue-number: ${{ github.event.pull_request.number }} + body: ${{ steps.template.outputs.result }} + + diff --git a/goreleaser/.goreleaser-snapshot.yaml b/goreleaser/.goreleaser-snapshot.yaml index 4930ea6a..29e5cce1 100644 --- a/goreleaser/.goreleaser-snapshot.yaml +++ b/goreleaser/.goreleaser-snapshot.yaml @@ -9,6 +9,8 @@ env: # https://github.com/moby/buildkit#export-cache - DOCKER_BUILDX_CACHE_FROM={{ if index .Env "DOCKER_BUILDX_CACHE_FROM" }}{{ .Env.DOCKER_BUILDX_CACHE_FROM }}{{ else }}type=registry{{ end }} - DOCKER_BUILDX_CACHE_TO={{ if index .Env "DOCKER_BUILDX_CACHE_TO" }}{{ .Env.DOCKER_BUILDX_CACHE_TO }}{{ else }}type=inline{{ end }} + - DOCKER_IMAGE_TITLE={{ if index .Env "SANDBOX_IMAGE" }}testkube-sandbox{{ else }}testkube-operator{{ end }} + - DOCKER_IMAGE_URL={{ if index .Env "SANDBOX_IMAGE" }}https://hub.docker.com/r/kubeshop/testkube-sandbox{{ else }}https://hub.docker.com/r/kubeshop/testkube-operator{{ end }} project_name: testkube-operator builds: @@ -28,6 +30,9 @@ dockers: image_templates: - "kubeshop/testkube-operator:{{ .ShortCommit }}" build_flag_templates: + - "--label=org.opencontainers.image.title={{ .Env.DOCKER_IMAGE_TITLE }}" + - "--label=org.opencontainers.image.description=Testkube Operator" + - "--label=org.opencontainers.image.url={{ .Env.DOCKER_IMAGE_URL }}" - "--platform=linux/amd64" - "--label=org.opencontainers.image.title={{ .ProjectName }}" - "--label=org.opencontainers.image.created={{.Date}}" From 96db1a3f9c0d2b00ef135fe7e7e40d455966dcbc Mon Sep 17 00:00:00 2001 From: ypoplavs Date: Thu, 1 Aug 2024 13:18:31 +0300 Subject: [PATCH 2/7] test operator --- .github/workflows/sandbox.yaml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/sandbox.yaml b/.github/workflows/sandbox.yaml index bd5a4107..fab3c4bb 100644 --- a/.github/workflows/sandbox.yaml +++ b/.github/workflows/sandbox.yaml @@ -29,27 +29,27 @@ jobs: fetch-depth: 0 - name: Set up Docker Buildx - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') id: buildx uses: docker/setup-buildx-action@v3 - name: Set up QEMU - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') uses: docker/setup-qemu-action@v3 - name: Set up Go - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') uses: actions/setup-go@v4 - name: Login to DockerHub - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Set version - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') run: | #get short commit sha that triggered the flow echo git_hash="$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_ENV @@ -61,14 +61,14 @@ jobs: echo branch_identifier=$(echo "$branch_identifier" | sed 's/\//-/g') >> $GITHUB_ENV - name: Set image tag - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') run: | # set image tag that includes service name, sandbox identifier and commit sha image_tag="${{ matrix.service }}-${{ env.branch_identifier }}-${{ env.git_hash }}" echo image_tag_sha=$image_tag >> $GITHUB_ENV - name: Build images with GoReleaser - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') uses: goreleaser/goreleaser-action@v4 with: distribution: goreleaser @@ -83,7 +83,7 @@ jobs: SANDBOX_IMAGE: true - name: Push Docker images - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') run: | docker tag kubeshop/testkube-operator:${{ env.image_tag_sha }} kubeshop/testkube-sandbox:${{ env.image_tag_sha }} docker push kubeshop/testkube-sandbox:${{ env.image_tag_sha }} @@ -98,7 +98,7 @@ jobs: # client-payload: '{"ref_name": "${{ github.ref_name }}", "sandbox_repo": "kubeshop/testkube-sandbox"}' - name: Output summary - if: startsWith(github.ref, 'refs/heads/sandbox/') + if: startsWith(github.ref, 'refs/heads/ci/') run: | echo -e "### Sandbox Environment" >> $GITHUB_STEP_SUMMARY echo -e '```' >> $GITHUB_STEP_SUMMARY From 19da4c7d9b09076558d371dd30d386ba51077921 Mon Sep 17 00:00:00 2001 From: ypoplavs Date: Thu, 1 Aug 2024 13:21:30 +0300 Subject: [PATCH 3/7] typo --- .github/workflows/sandbox.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sandbox.yaml b/.github/workflows/sandbox.yaml index fab3c4bb..9d4f21c5 100644 --- a/.github/workflows/sandbox.yaml +++ b/.github/workflows/sandbox.yaml @@ -64,7 +64,7 @@ jobs: if: startsWith(github.ref, 'refs/heads/ci/') run: | # set image tag that includes service name, sandbox identifier and commit sha - image_tag="${{ matrix.service }}-${{ env.branch_identifier }}-${{ env.git_hash }}" + image_tag="${{ env.branch_identifier }}-${{ env.git_hash }}" echo image_tag_sha=$image_tag >> $GITHUB_ENV - name: Build images with GoReleaser From 9934159ed23533cf48cade5f02224ba0d40d8eaa Mon Sep 17 00:00:00 2001 From: ypoplavs Date: Thu, 1 Aug 2024 13:25:22 +0300 Subject: [PATCH 4/7] update git_hash --- .github/workflows/sandbox.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sandbox.yaml b/.github/workflows/sandbox.yaml index 9d4f21c5..abeeba6f 100644 --- a/.github/workflows/sandbox.yaml +++ b/.github/workflows/sandbox.yaml @@ -85,7 +85,7 @@ jobs: - name: Push Docker images if: startsWith(github.ref, 'refs/heads/ci/') run: | - docker tag kubeshop/testkube-operator:${{ env.image_tag_sha }} kubeshop/testkube-sandbox:${{ env.image_tag_sha }} + docker tag kubeshop/testkube-operator:${{ env.git_hash }} kubeshop/testkube-sandbox:${{ env.image_tag_sha }} docker push kubeshop/testkube-sandbox:${{ env.image_tag_sha }} # - name: Repository dispatch From 56b496564d011948b3d55ff57c76bac571cc9878 Mon Sep 17 00:00:00 2001 From: ypoplavs Date: Thu, 1 Aug 2024 13:40:42 +0300 Subject: [PATCH 5/7] github summary --- .github/workflows/sandbox.yaml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/sandbox.yaml b/.github/workflows/sandbox.yaml index abeeba6f..68347ef0 100644 --- a/.github/workflows/sandbox.yaml +++ b/.github/workflows/sandbox.yaml @@ -102,13 +102,12 @@ jobs: run: | echo -e "### Sandbox Environment" >> $GITHUB_STEP_SUMMARY echo -e '```' >> $GITHUB_STEP_SUMMARY - ## URLs - - - Dashboard: https://dashboard.${{ github.ref_name }}.testkube.dev - - API: https://api.${{ github.ref_name }}.testkube.dev - - Agent: https://agent.${{ github.ref_name }}.testkube.dev - - Storage: https://storage.${{ github.ref_name }}.testkube.dev - - Websockets: https://websockets.${{ github.ref_name }}.testkube.dev + echo -e "## URLs" >> $GITHUB_STEP_SUMMARY + echo "- Dashboard: https://dashboard.${{ github.ref_name }}.testkube.dev" >> $GITHUB_STEP_SUMMARY + echo "- API: https://api.${{ github.ref_name }}.testkube.dev" >> $GITHUB_STEP_SUMMARY + echo "- Agent: https://agent.${{ github.ref_name }}.testkube.dev" >> $GITHUB_STEP_SUMMARY + echo "- Storage: https://storage.${{ github.ref_name }}.testkube.dev" >> $GITHUB_STEP_SUMMARY + echo "- Websockets: https://websockets.${{ github.ref_name }}.testkube.dev" >> $GITHUB_STEP_SUMMARY echo -e '```' >> $GITHUB_STEP_SUMMARY - name: Get a branch name if PR is created From 266c8e25680b295a7213b91d5d6e5df35260b30b Mon Sep 17 00:00:00 2001 From: ypoplavs Date: Fri, 2 Aug 2024 09:34:41 +0300 Subject: [PATCH 6/7] add removal --- .github/workflows/sandbox-deletion.yaml | 31 +++++++++++++++++++ .github/workflows/sandbox.yaml | 41 ++++++++++++------------- 2 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/sandbox-deletion.yaml diff --git a/.github/workflows/sandbox-deletion.yaml b/.github/workflows/sandbox-deletion.yaml new file mode 100644 index 00000000..c68699b2 --- /dev/null +++ b/.github/workflows/sandbox-deletion.yaml @@ -0,0 +1,31 @@ +name: Remove sandbox environment + +permissions: + contents: read + actions: write + +on: + pull_request: + types: [closed] + +jobs: + delete: + if: startsWith(github.event.pull_request.head.ref, 'sandbox/') + name: Pass payload data + runs-on: ubuntu-22.04 + steps: + - uses: actions/create-github-app-token@v1 + id: app-token + with: + app-id: ${{ secrets.APP_ID }} + private-key: ${{ secrets.PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + + - name: Repository dispatch + uses: peter-evans/repository-dispatch@v2 + with: + token: ${{ steps.app-token.outputs.token }} + repository: kubeshop/testkube-deployment + event-type: sandbox_env_delete + client-payload: '{"ref_name": "${{ github.event.pull_request.head.ref }}"}' + diff --git a/.github/workflows/sandbox.yaml b/.github/workflows/sandbox.yaml index 68347ef0..c7e8b172 100644 --- a/.github/workflows/sandbox.yaml +++ b/.github/workflows/sandbox.yaml @@ -4,7 +4,6 @@ on: push: branches: - sandbox/** - - ci/add-sandbox pull_request: types: - opened @@ -29,27 +28,27 @@ jobs: fetch-depth: 0 - name: Set up Docker Buildx - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') id: buildx uses: docker/setup-buildx-action@v3 - name: Set up QEMU - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') uses: docker/setup-qemu-action@v3 - name: Set up Go - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') uses: actions/setup-go@v4 - name: Login to DockerHub - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Set version - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') run: | #get short commit sha that triggered the flow echo git_hash="$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_ENV @@ -61,14 +60,14 @@ jobs: echo branch_identifier=$(echo "$branch_identifier" | sed 's/\//-/g') >> $GITHUB_ENV - name: Set image tag - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') run: | # set image tag that includes service name, sandbox identifier and commit sha image_tag="${{ env.branch_identifier }}-${{ env.git_hash }}" echo image_tag_sha=$image_tag >> $GITHUB_ENV - name: Build images with GoReleaser - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') uses: goreleaser/goreleaser-action@v4 with: distribution: goreleaser @@ -83,22 +82,22 @@ jobs: SANDBOX_IMAGE: true - name: Push Docker images - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') run: | - docker tag kubeshop/testkube-operator:${{ env.git_hash }} kubeshop/testkube-sandbox:${{ env.image_tag_sha }} - docker push kubeshop/testkube-sandbox:${{ env.image_tag_sha }} - -# - name: Repository dispatch -# if: startsWith(github.ref, 'refs/heads/sandbox/') -# uses: peter-evans/repository-dispatch@v2 -# with: -# token: ${{ secrets.CI_BOT_TOKEN }} -# repository: kubeshop/testkube-deployment -# event-type: sandbox_image_update -# client-payload: '{"ref_name": "${{ github.ref_name }}", "sandbox_repo": "kubeshop/testkube-sandbox"}' + docker tag kubeshop/testkube-operator:${{ env.git_hash }} kubeshop/testkube-sandbox:operator-${{ env.image_tag_sha }} + docker push kubeshop/testkube-sandbox:operator-${{ env.image_tag_sha }} + + - name: Repository dispatch + if: startsWith(github.ref, 'refs/heads/sandbox/') + uses: peter-evans/repository-dispatch@v2 + with: + token: ${{ secrets.CI_BOT_TOKEN }} + repository: kubeshop/testkube-deployment + event-type: sandbox_image_update + client-payload: '{"ref_name": "${{ github.ref_name }}", "sandbox_repo": "kubeshop/testkube-sandbox"}' - name: Output summary - if: startsWith(github.ref, 'refs/heads/ci/') + if: startsWith(github.ref, 'refs/heads/sandbox/') run: | echo -e "### Sandbox Environment" >> $GITHUB_STEP_SUMMARY echo -e '```' >> $GITHUB_STEP_SUMMARY From 460a8a1935b76a147579dd7fc227b0858cc2e538 Mon Sep 17 00:00:00 2001 From: ypoplavs Date: Fri, 2 Aug 2024 11:13:46 +0300 Subject: [PATCH 7/7] rename sandbox template --- .github/{comment-template.md => sandbox-comment-template.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{comment-template.md => sandbox-comment-template.md} (100%) diff --git a/.github/comment-template.md b/.github/sandbox-comment-template.md similarity index 100% rename from .github/comment-template.md rename to .github/sandbox-comment-template.md