From a95c36eb7ace2e26e2cc7dd890b3c65863f8523f Mon Sep 17 00:00:00 2001 From: sw-wayner Date: Thu, 29 Feb 2024 10:33:06 -0400 Subject: [PATCH 1/4] ci: add needed actions --- .../workflows/build-and-publish-preview.yaml | 50 ++++++++ .github/workflows/clean-up-pr-preview.yaml | 27 +++++ .github/workflows/deploy-pr-preview.yaml | 112 ++++++++++++++++++ .../workflows/slash-command-dispatcher.yaml | 30 +++++ infrastructure/preview.Dockerfile | 13 ++ 5 files changed, 232 insertions(+) create mode 100644 .github/workflows/build-and-publish-preview.yaml create mode 100644 .github/workflows/clean-up-pr-preview.yaml create mode 100644 .github/workflows/deploy-pr-preview.yaml create mode 100644 .github/workflows/slash-command-dispatcher.yaml create mode 100644 infrastructure/preview.Dockerfile diff --git a/.github/workflows/build-and-publish-preview.yaml b/.github/workflows/build-and-publish-preview.yaml new file mode 100644 index 00000000..a251ec2f --- /dev/null +++ b/.github/workflows/build-and-publish-preview.yaml @@ -0,0 +1,50 @@ +name: Build & Publish Stable Preview +on: + push: + branches: ["master"] +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Check Out Repo + uses: actions/checkout@v2 + with: + registry-url: 'https://npm.pkg.github.com' + - run: echo "registry=https://registry.yarnpkg.com/" > .npmrc + - run: echo "@internxt:registry=https://npm.pkg.github.com" >> .npmrc + # You cannot read packages from other private repos with GITHUB_TOKEN + # You have to use a PAT instead https://github.com/actions/setup-node/issues/49 + - run: echo //npm.pkg.github.com/:_authToken=${{ secrets.PERSONAL_ACCESS_TOKEN }} >> .npmrc + - run: echo "always-auth=true" >> .npmrc + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Build and push to drive-server-dev + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./infrastructure/preview.Dockerfile + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/drive-server-dev:${{ github.sha }} + dispatch_update_preview_image: + needs: build + runs-on: ubuntu-latest + steps: + - name: Dispatch Update Preview Image Command + uses: myrotvorets/trigger-repository-dispatch-action@1.0.0 + with: + token: ${{ secrets.PAT }} + repo: internxt/environments + type: update-preview-image-command + payload: | + { + "image": { + "name": "${{ secrets.DOCKERHUB_USERNAME }}/drive-server", + "newName": "${{ secrets.DOCKERHUB_USERNAME }}/drive-server-dev", + "newTag": "${{ github.sha }}" + } + } \ No newline at end of file diff --git a/.github/workflows/clean-up-pr-preview.yaml b/.github/workflows/clean-up-pr-preview.yaml new file mode 100644 index 00000000..815cbf1b --- /dev/null +++ b/.github/workflows/clean-up-pr-preview.yaml @@ -0,0 +1,27 @@ +name: Clean Up PR Preview +on: + pull_request: + types: [closed] +jobs: + dispatch_cleanup_deployment: + runs-on: ubuntu-latest + steps: + - name: Dispatch Cleanup Preview Repository Command + uses: myrotvorets/trigger-repository-dispatch-action@1.0.0 + with: + token: ${{ secrets.PAT }} + repo: internxt/environments + type: cleanup-preview-command + payload: | + { + "github": { + "payload": { + "repository": { + "name": "${{ github.event.repository.name }}" + }, + "issue": { + "number": ${{ github.event.number }} + } + } + } + } \ No newline at end of file diff --git a/.github/workflows/deploy-pr-preview.yaml b/.github/workflows/deploy-pr-preview.yaml new file mode 100644 index 00000000..188c009b --- /dev/null +++ b/.github/workflows/deploy-pr-preview.yaml @@ -0,0 +1,112 @@ +name: Deploy PR Preview +on: + pull_request: + types: [opened, reopened, synchronize, ready_for_review] +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Check Out Repo + uses: actions/checkout@v2 + with: + registry-url: 'https://npm.pkg.github.com' + - run: echo "registry=https://registry.yarnpkg.com/" > .npmrc + - run: echo "@internxt:registry=https://npm.pkg.github.com" >> .npmrc + # You cannot read packages from other private repos with GITHUB_TOKEN + # You have to use a PAT instead https://github.com/actions/setup-node/issues/49 + - run: echo //npm.pkg.github.com/:_authToken=${{ secrets.PERSONAL_ACCESS_TOKEN }} >> .npmrc + - run: echo "always-auth=true" >> .npmrc + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Build and push to drive-server-dev + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./Dockerfile + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/drive-server-dev:preview-${{ github.event.number }}-${{ github.event.pull_request.head.sha }} + add_preview_label: + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + needs: build + steps: + - uses: actions-ecosystem/action-add-labels@v1 + with: + labels: | + preview + dispatch_update_deployment: + needs: add_preview_label + runs-on: ubuntu-latest + if: ${{ contains(github.event.pull_request.labels.*.name, 'deployed') }} + steps: + - name: Dispatch Update Preview Repository Command + uses: myrotvorets/trigger-repository-dispatch-action@1.0.0 + with: + token: ${{ secrets.PAT }} + repo: internxt/environments + type: update-preview-command + payload: | + { + "github": { + "payload": { + "repository": { + "name": "${{ github.event.repository.name }}", + "full_name": "${{ github.event.repository.full_name }}" + }, + "issue": { + "number": ${{ github.event.number }}, + "labels": ${{ toJSON(github.event.pull_request.labels) }} + } + } + }, + "slash_command": { + "args": { + "named": { + "deployment": "${{ github.event.repository.name }}", + "tag": "preview-${{ github.event.number }}-${{ github.event.pull_request.head.sha }}", + "imageSuffix": "-dev" + } + } + } + } + dispatch_check_deployment: + needs: add_preview_label + runs-on: ubuntu-latest + steps: + - name: Dispatch Check Preview Repository Command + uses: myrotvorets/trigger-repository-dispatch-action@1.0.0 + with: + token: ${{ secrets.PAT }} + repo: internxt/environments + type: check-preview-command + payload: | + { + "github": { + "payload": { + "repository": { + "name": "${{ github.event.repository.name }}", + "full_name": "${{ github.event.repository.full_name }}", + "html_url": "${{ github.event.repository.html_url }}" + }, + "issue": { + "number": ${{ github.event.number }}, + "labels": ${{ toJSON(github.event.pull_request.labels) }}, + "pull_request": { + "html_url": "${{ github.event.pull_request.html_url }}" + } + } + } + }, + "slash_command": { + "args": { + "named": { + "notify": "true" + } + } + } + } \ No newline at end of file diff --git a/.github/workflows/slash-command-dispatcher.yaml b/.github/workflows/slash-command-dispatcher.yaml new file mode 100644 index 00000000..50120cde --- /dev/null +++ b/.github/workflows/slash-command-dispatcher.yaml @@ -0,0 +1,30 @@ +name: Slash Command Dispatch +on: + issue_comment: + types: [created] +jobs: + slash_command_dispatch: + runs-on: ubuntu-latest + if: ${{ contains(github.event.issue.labels.*.name, 'deployed') || contains(github.event.issue.labels.*.name, 'preview') }} + steps: + - name: Slash Command Dispatch + id: scd + uses: peter-evans/slash-command-dispatch@v4 + with: + token: ${{ secrets.PAT }} + commands: update-preview,check-preview + permission: write + repository: internxt/environments + issue-type: pull-request + allow-edits: false + reactions: false + - name: Edit comment with error message + if: steps.scd.outputs.error-message + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ github.event.comment.id }} + body: | + + > [!CAUTION] + > Couldn't dispatch your command due to error: + > **${{ steps.scd.outputs.error-message }}** \ No newline at end of file diff --git a/infrastructure/preview.Dockerfile b/infrastructure/preview.Dockerfile new file mode 100644 index 00000000..0a25a468 --- /dev/null +++ b/infrastructure/preview.Dockerfile @@ -0,0 +1,13 @@ +FROM node:16 + +WORKDIR /usr/app + +COPY package*.json ./ + +COPY .npmrc ./ + +RUN yarn + +COPY . ./ + +CMD yarn migrate && yarn dev \ No newline at end of file From ca061c6e81fb308a63ef3a484d94020b08e17a37 Mon Sep 17 00:00:00 2001 From: sw-wayner Date: Thu, 29 Feb 2024 11:57:44 -0400 Subject: [PATCH 2/4] chore: use `./infrastructure/preview.Dockerfile` --- .github/workflows/deploy-pr-preview.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-pr-preview.yaml b/.github/workflows/deploy-pr-preview.yaml index 188c009b..42501f09 100644 --- a/.github/workflows/deploy-pr-preview.yaml +++ b/.github/workflows/deploy-pr-preview.yaml @@ -27,7 +27,7 @@ jobs: uses: docker/build-push-action@v2 with: context: ./ - file: ./Dockerfile + file: ./infrastructure/preview.Dockerfile push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/drive-server-dev:preview-${{ github.event.number }}-${{ github.event.pull_request.head.sha }} add_preview_label: From fd570b87a5dc0cde372e001d942fca0181c0798f Mon Sep 17 00:00:00 2001 From: sw-wayner Date: Thu, 29 Feb 2024 17:47:01 -0400 Subject: [PATCH 3/4] chore: use repository name as image name --- .github/workflows/build-and-publish-preview.yaml | 8 ++++---- .github/workflows/deploy-pr-preview.yaml | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-and-publish-preview.yaml b/.github/workflows/build-and-publish-preview.yaml index a251ec2f..60262e20 100644 --- a/.github/workflows/build-and-publish-preview.yaml +++ b/.github/workflows/build-and-publish-preview.yaml @@ -23,13 +23,13 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - name: Build and push to drive-server-dev + - name: Build and push to ${{ github.event.repository.name }}-dev uses: docker/build-push-action@v2 with: context: ./ file: ./infrastructure/preview.Dockerfile push: true - tags: ${{ secrets.DOCKERHUB_USERNAME }}/drive-server-dev:${{ github.sha }} + tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}-dev:${{ github.sha }} dispatch_update_preview_image: needs: build runs-on: ubuntu-latest @@ -43,8 +43,8 @@ jobs: payload: | { "image": { - "name": "${{ secrets.DOCKERHUB_USERNAME }}/drive-server", - "newName": "${{ secrets.DOCKERHUB_USERNAME }}/drive-server-dev", + "name": "${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}", + "newName": "${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}-dev", "newTag": "${{ github.sha }}" } } \ No newline at end of file diff --git a/.github/workflows/deploy-pr-preview.yaml b/.github/workflows/deploy-pr-preview.yaml index 42501f09..a1880a15 100644 --- a/.github/workflows/deploy-pr-preview.yaml +++ b/.github/workflows/deploy-pr-preview.yaml @@ -23,13 +23,13 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - name: Build and push to drive-server-dev + - name: Build and push to ${{ github.event.repository.name }}-dev uses: docker/build-push-action@v2 with: context: ./ file: ./infrastructure/preview.Dockerfile push: true - tags: ${{ secrets.DOCKERHUB_USERNAME }}/drive-server-dev:preview-${{ github.event.number }}-${{ github.event.pull_request.head.sha }} + tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}-dev:preview-${{ github.event.number }}-${{ github.event.pull_request.head.sha }} add_preview_label: if: github.event.pull_request.draft == false runs-on: ubuntu-latest From 75223580816da8e191923eab2dbc3df3b1a747ee Mon Sep 17 00:00:00 2001 From: sw-wayner Date: Mon, 4 Mar 2024 11:38:31 -0400 Subject: [PATCH 4/4] fix: add ready-for-preview instead of preview --- .github/workflows/deploy-pr-preview.yaml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy-pr-preview.yaml b/.github/workflows/deploy-pr-preview.yaml index a1880a15..78cef22b 100644 --- a/.github/workflows/deploy-pr-preview.yaml +++ b/.github/workflows/deploy-pr-preview.yaml @@ -30,7 +30,7 @@ jobs: file: ./infrastructure/preview.Dockerfile push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}-dev:preview-${{ github.event.number }}-${{ github.event.pull_request.head.sha }} - add_preview_label: + add_ready_for_preview_label: if: github.event.pull_request.draft == false runs-on: ubuntu-latest needs: build @@ -38,9 +38,9 @@ jobs: - uses: actions-ecosystem/action-add-labels@v1 with: labels: | - preview + ready-for-preview dispatch_update_deployment: - needs: add_preview_label + needs: add_ready_for_preview_label runs-on: ubuntu-latest if: ${{ contains(github.event.pull_request.labels.*.name, 'deployed') }} steps: @@ -75,8 +75,9 @@ jobs: } } dispatch_check_deployment: - needs: add_preview_label + needs: add_ready_for_preview_label runs-on: ubuntu-latest + if: ${{ contains(github.event.pull_request.labels.*.name, 'preview') }} steps: - name: Dispatch Check Preview Repository Command uses: myrotvorets/trigger-repository-dispatch-action@1.0.0