From 1cba826c104b0b2013b05ecbcc48be95b6d8714c Mon Sep 17 00:00:00 2001 From: Hyungu Kang | Airen Date: Wed, 14 Aug 2024 17:50:52 +0900 Subject: [PATCH] ci: update deploy workflows (#336) ## Changes - Update deploy workflow >AS-IS: A tag creation triggers the self-service deploy workflow. >TO-BE: A merge event triggers the self-service deploy workflow. ticket: [SECURE-601] [SECURE-601]: https://sendbird.atlassian.net/browse/SECURE-601?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --- .circleci/config.yml | 28 +++++++++++---- .github/workflows/self-service-publish.yml | 34 +++++++++++++++++++ .../self-service/scripts/getWidgetVersion.js | 9 ++++- release-guide.md | 11 +++--- 4 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/self-service-publish.yml diff --git a/.circleci/config.yml b/.circleci/config.yml index 5606dbe95..2ee75d189 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,6 +3,11 @@ orbs: aws-cli: circleci/aws-cli@2.1.0 aws-s3: circleci/aws-s3@4.0.0 +parameters: + run_deploy_prod: + type: boolean + default: false + executors: ci-aws: docker: @@ -104,6 +109,9 @@ jobs: # Publish - build self-service build: executor: node + parameters: + widget_version: + type: string steps: - checkout - run: @@ -114,7 +122,7 @@ jobs: command: | cd ./packages/self-service npm install - npm run build version=${CIRCLE_TAG} + npm run build version=<< parameters.widget_version >> # TODO: update self-service to use yarn workspaces # yarn install --immutable --immutable-cache # yarn build @@ -174,18 +182,20 @@ workflows: - run-test: requires: - prepare - deploy_to_s3: + + deploy_prod: + when: << pipeline.parameters.run_deploy_prod >> jobs: - build: name: build - filters: &only_version_tag_trigger - tags: - only: /^v\d+\.\d+\.\d+$/ + widget_version: "" + filters: &default_branch_trigger branches: - ignore: /.*/ + only: + - develop - upload_to_s3_and_invalidate_cache: name: upload_to_s3_and_invalidate_cache - filters: *only_version_tag_trigger + filters: *default_branch_trigger s3_bucket: $S3_BUCKET distribution_id: $DISTRIBUTION_ID role_arn: $ROLE_ARN @@ -193,8 +203,12 @@ workflows: - build context: - circleci-dashboard-bot + + deploy_stg: + jobs: - build: name: build_staging + widget_version: $CIRCLE_TAG filters: &only_version_tag_trigger_staging tags: only: /^v\d+\.\d+\.\d+-staging(\.\d+)?$/ diff --git a/.github/workflows/self-service-publish.yml b/.github/workflows/self-service-publish.yml new file mode 100644 index 000000000..caa13fafa --- /dev/null +++ b/.github/workflows/self-service-publish.yml @@ -0,0 +1,34 @@ +name: self-service build and publish + +env: + DEFAULT_BRANCH: develop + +on: + pull_request: + branches: + - ${{ env.DEFAULT_BRANCH }} + types: + - closed + +jobs: + deploy: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + env: + CIRCLECI_TOKEN: ${{ secrets.CIRCLECI_API_TOKEN }} + DEFAULT_BRANCH: ${{ env.DEFAULT_BRANCH }} + steps: + - name: Check if the source branch is a release branch + run: | + PR_BRANCH="${{ github.event.pull_request.head.ref }}" + echo "Source branch is $PR_BRANCH" + if [[ "$PR_BRANCH" == release/* ]]; then + echo "The source branch is a release branch, proceed with deployment." + curl -d '{\"branch\": \"${DEFAULT_BRANCH}\", \"parameters\": {\"run_deploy_prod\": true}}' \ + -H 'Content-Type: application/json' \ + -H 'Circle-Token: ${CIRCLECI_TOKEN}' \ + -X POST https://circleci.com/api/v2/project/gh/sendbird/chat-ai-widget/pipeline + else + echo "The source branch is not a release branch, skipping deployment." + exit 0 + fi diff --git a/packages/self-service/scripts/getWidgetVersion.js b/packages/self-service/scripts/getWidgetVersion.js index a7a41728d..1c471637b 100644 --- a/packages/self-service/scripts/getWidgetVersion.js +++ b/packages/self-service/scripts/getWidgetVersion.js @@ -7,5 +7,12 @@ const packageJson = JSON.parse(fs.readFileSync('../../package.json', 'utf8')); * */ export function getWidgetVersion() { const version = process.argv[2]; - return version ? `${version.split('version=')[1]}` : `v${packageJson.version}`; + + if (!version.startsWith('version=') || version === 'version=') { + return `v${packageJson.version}`; + } + + return version.split('version=')[1]; } + +console.log('widget version:', getWidgetVersion()); diff --git a/release-guide.md b/release-guide.md index cfd4a6c22..692005e71 100644 --- a/release-guide.md +++ b/release-guide.md @@ -14,13 +14,16 @@ 3. Enter the target version (e.g., 1.3.1) in the version field, and specify `rc` / `alpha` / `beta` for the `npm_tag` field if necessary. workflow-guide 4. Hit "Run workflow" button. - -Once all the steps in the workflow are successfully completed: +5. Once all the steps in the workflow are successfully completed: - The build output will be published to npm. (if `npm_tag` is provided, we stop the workflow from here) - A commit will be pushed to the release PR created in Step 0. This commit includes: - `@sendbird/chat-ai-widget` dependency version updated in `/packages/*`. - - A new tag(`v{version}`) will be pushed to the origin to trigger the self-service script deployment. - - Check the progress in [Circle CI dashboard](https://app.circleci.com/pipelines/github/sendbird/chat-ai-widget). + - A new tag(`v{version}`) will be pushed to the origin ~~to trigger the self-service script deployment.~~ + +## Step 2 - Publish a new self-service script (using automated workflow) +1. Merge the PR created in Step 0. +2. When the release branch is merged into the default branch, the [self-service-publish](./.github/workflows/self-service-publish.yml) workflow will deploy the self-service script. + - Check the progress in [Circle CI dashboard](https://app.circleci.com/pipelines/github/sendbird/chat-ai-widget). ### Want to publish `@sendbird/chat-ai-widget` manually? 1. Update the `version` field in `package.json`.