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

ci: add sandbox #288

Merged
merged 7 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/sandbox-comment-template.md
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions .github/workflows/sandbox-deletion.yaml
Original file line number Diff line number Diff line change
@@ -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 }}"}'

141 changes: 141 additions & 0 deletions .github/workflows/sandbox.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
name: Sanbox image to build on push to sandbox/<sandbox identifier> branch

on:
push:
branches:
- sandbox/**
pull_request:
types:
- opened

permissions:
contents: write
id-token: write
pull-requests: write
issues: write


env:
ALPINE_IMAGE: alpine:3.18.3
ypoplavs marked this conversation as resolved.
Show resolved Hide resolved

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="${{ 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.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/sandbox/')
run: |
echo -e "### Sandbox Environment" >> $GITHUB_STEP_SUMMARY
echo -e '```' >> $GITHUB_STEP_SUMMARY
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
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/[email protected]
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 }}


5 changes: 5 additions & 0 deletions goreleaser/.goreleaser-snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}}"
Expand Down
Loading