From 9c47b04341127d0afde8f187976bcea6dd858929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Mi=C4=85cz?= Date: Mon, 6 Jul 2020 20:50:48 +0200 Subject: [PATCH 1/8] Setup test environment deployment workflow --- .github/PULL_REQUEST_TEMPLATE.md | 6 +++ .github/workflows/test-env-deploy.yml | 66 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 .github/workflows/test-env-deploy.yml diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 7135688da..df5d5c5d0 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -20,3 +20,9 @@ greatly reduce the amount of work needed to review your work. --> 1. [ ] The changes are tested. 1. [ ] Type definitions are up to date. 1. [ ] Changes are mentioned in the changelog. + +### Test environment config + +``` +API_URI=https://master.staging.saleor.rocks/graphql/ +``` diff --git a/.github/workflows/test-env-deploy.yml b/.github/workflows/test-env-deploy.yml new file mode 100644 index 000000000..722956a34 --- /dev/null +++ b/.github/workflows/test-env-deploy.yml @@ -0,0 +1,66 @@ +name: TEST-ENV-DEPLOYMENT + +on: + pull_request: + types: [opened, reopened, edited, synchronize] + branches: ["*"] + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Cache node modules + uses: actions/cache@v2 + env: + cache-name: cache-node-modules + with: + # npm cache files are stored in `~/.npm` on Linux/macOS + path: ~/.npm + key: ${{ runner.os }}-qa-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-qa-${{ env.cache-name }}- + ${{ runner.os }}-qa- + ${{ runner.os }}- + + - name: Install deps + run: | + npm ci + + - name: Set API URI + env: + pull_request_body: ${{ github.event.pull_request.body }} + prefix: API_URI= + pattern: (http|https)://[a-zA-Z0-9.]+/graphql/? + run: | + echo "::set-env name=API_URI::$(echo $pull_request_body | grep -Eo "$prefix$pattern" | sed s/$prefix// | head -n 1)" + + - name: Run build + env: + APP_MOUNT_URI: / + STATIC_URL: / + run: | + npm run build + + - uses: rlespinasse/github-slug-action@master + - name: Set domain + run: | + echo "::set-env name=domain::${{ env.GITHUB_HEAD_REF_SLUG }}.dashboard.saleor.rocks" + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_DEFAULT_REGION }} + + - name: Deploy to S3 + run: aws s3 sync ./build/dashboard s3://test-envs-stack-dashboards5d35b643-nempmfdd9fv/${{ env.domain }} + + - name: Invalidate cache + run: aws cloudfront create-invalidation --distribution-id E1INMM566C4FJP --paths "/${{ env.domain }}/*" + + - name: Print URL + run: | + echo https://${{ env.domain }}/ From 447dc5da14975dccce755c971d53bb7045a89f46 Mon Sep 17 00:00:00 2001 From: Milosz Tyborowski Date: Tue, 7 Jul 2020 15:03:18 +0200 Subject: [PATCH 2/8] Cleanup S3 after decomissioning test environment --- .github/workflows/test-env-cleanup.yml | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .github/workflows/test-env-cleanup.yml diff --git a/.github/workflows/test-env-cleanup.yml b/.github/workflows/test-env-cleanup.yml new file mode 100644 index 000000000..79f9f2222 --- /dev/null +++ b/.github/workflows/test-env-cleanup.yml @@ -0,0 +1,27 @@ +name: TEST-ENV-CLEANUP + +on: + pull_request: + types: [closed] + branches: ["*"] + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - uses: rlespinasse/github-slug-action@master + - name: Set domain + run: | + echo "::set-env name=domain::${{ env.GITHUB_HEAD_REF_SLUG }}.dashboard.saleor.rocks" + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_DEFAULT_REGION }} + + - name: Remove S3 directory + run: aws s3 rm s3://test-envs-stack-dashboards5d35b643-nempmfdd9fv/${{ env.domain }} + + - name: Invalidate cache + run: aws cloudfront create-invalidation --distribution-id E1INMM566C4FJP --paths "/${{ env.domain }}/*" From efb4d2d1580c7bceb1cc97ffc7e9d2c3537b4d67 Mon Sep 17 00:00:00 2001 From: Milosz Tyborowski Date: Wed, 8 Jul 2020 14:36:35 +0200 Subject: [PATCH 3/8] Unify PR template between storefront and dashboard --- .github/PULL_REQUEST_TEMPLATE.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index df5d5c5d0..8f975f497 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -23,6 +23,7 @@ greatly reduce the amount of work needed to review your work. --> ### Test environment config -``` + + API_URI=https://master.staging.saleor.rocks/graphql/ -``` From a2f7f42fdc2dc89e53b0dbc3f7aab80caacd25de Mon Sep 17 00:00:00 2001 From: Milosz Tyborowski Date: Wed, 8 Jul 2020 14:38:23 +0200 Subject: [PATCH 4/8] Comment testenv deployment workflow --- .github/workflows/test-env-deploy.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/test-env-deploy.yml b/.github/workflows/test-env-deploy.yml index 722956a34..7ab498a16 100644 --- a/.github/workflows/test-env-deploy.yml +++ b/.github/workflows/test-env-deploy.yml @@ -2,6 +2,7 @@ name: TEST-ENV-DEPLOYMENT on: pull_request: + # we need to look for "edited" action because eg. API_URI can change in the PR description types: [opened, reopened, edited, synchronize] branches: ["*"] @@ -29,6 +30,7 @@ jobs: npm ci - name: Set API URI + # getting API_URI from PR description in case it has been changed env: pull_request_body: ${{ github.event.pull_request.body }} prefix: API_URI= @@ -45,10 +47,12 @@ jobs: - uses: rlespinasse/github-slug-action@master - name: Set domain + # generate normalized domain name, get rid of characters that may be found in branch names, but cannot be used in domain name like "/" run: | echo "::set-env name=domain::${{ env.GITHUB_HEAD_REF_SLUG }}.dashboard.saleor.rocks" - name: Configure AWS credentials + # get credentials for AWS from GitHub repo secrets uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} From 89065cce41e9124efafccd4f0402e371c9de6ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Mi=C4=85cz?= Date: Wed, 8 Jul 2020 15:35:25 +0200 Subject: [PATCH 5/8] Add deployment status --- .github/workflows/test-env-cleanup.yml | 14 ++++++++-- .github/workflows/test-env-deploy.yml | 37 ++++++++++++++++++-------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test-env-cleanup.yml b/.github/workflows/test-env-cleanup.yml index 79f9f2222..b37fe53db 100644 --- a/.github/workflows/test-env-cleanup.yml +++ b/.github/workflows/test-env-cleanup.yml @@ -1,4 +1,5 @@ name: TEST-ENV-CLEANUP +# Remove test instance for closed pull requests on: pull_request: @@ -11,8 +12,10 @@ jobs: steps: - uses: rlespinasse/github-slug-action@master - name: Set domain + # Set test instance domain based on branch name slug run: | echo "::set-env name=domain::${{ env.GITHUB_HEAD_REF_SLUG }}.dashboard.saleor.rocks" + - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v1 with: @@ -21,7 +24,14 @@ jobs: aws-region: ${{ secrets.AWS_DEFAULT_REGION }} - name: Remove S3 directory - run: aws s3 rm s3://test-envs-stack-dashboards5d35b643-nempmfdd9fv/${{ env.domain }} + run: aws s3 rm s3://${{ secrets.AWS_TEST_DEPLOYMENT_BUCKET }}/${{ env.domain }} - name: Invalidate cache - run: aws cloudfront create-invalidation --distribution-id E1INMM566C4FJP --paths "/${{ env.domain }}/*" + run: aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_TEST_CF_DIST_ID }} --paths "/${{ env.domain }}/*" + + - name: Mark deployment as deactivated + uses: bobheadxi/deployments@master + with: + step: deactivate-env + token: ${{ secrets.GITHUB_TOKEN }} + env: ${{ env.GITHUB_HEAD_REF_SLUG }} diff --git a/.github/workflows/test-env-deploy.yml b/.github/workflows/test-env-deploy.yml index 7ab498a16..911fdae7c 100644 --- a/.github/workflows/test-env-deploy.yml +++ b/.github/workflows/test-env-deploy.yml @@ -1,8 +1,9 @@ name: TEST-ENV-DEPLOYMENT +# Build and deploy test instance for every pull request on: pull_request: - # we need to look for "edited" action because eg. API_URI can change in the PR description + # trigger on "edited" to update instance when configuration changes in PR description types: [opened, reopened, edited, synchronize] branches: ["*"] @@ -12,12 +13,22 @@ jobs: steps: - uses: actions/checkout@v2 + - uses: rlespinasse/github-slug-action@master + + - name: Start deployment + uses: bobheadxi/deployments@master + id: deployment + with: + step: start + token: ${{ secrets.GITHUB_TOKEN }} + env: ${{ env.GITHUB_HEAD_REF_SLUG }} + ref: ${{ github.head_ref }} + - name: Cache node modules uses: actions/cache@v2 env: cache-name: cache-node-modules with: - # npm cache files are stored in `~/.npm` on Linux/macOS path: ~/.npm key: ${{ runner.os }}-qa-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} restore-keys: | @@ -30,7 +41,7 @@ jobs: npm ci - name: Set API URI - # getting API_URI from PR description in case it has been changed + # Setting API_URI env variable from PR description env: pull_request_body: ${{ github.event.pull_request.body }} prefix: API_URI= @@ -45,14 +56,12 @@ jobs: run: | npm run build - - uses: rlespinasse/github-slug-action@master - name: Set domain - # generate normalized domain name, get rid of characters that may be found in branch names, but cannot be used in domain name like "/" + # Set test instance domain based on branch name slug run: | echo "::set-env name=domain::${{ env.GITHUB_HEAD_REF_SLUG }}.dashboard.saleor.rocks" - name: Configure AWS credentials - # get credentials for AWS from GitHub repo secrets uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} @@ -60,11 +69,17 @@ jobs: aws-region: ${{ secrets.AWS_DEFAULT_REGION }} - name: Deploy to S3 - run: aws s3 sync ./build/dashboard s3://test-envs-stack-dashboards5d35b643-nempmfdd9fv/${{ env.domain }} + run: aws s3 sync ./build/dashboard s3://${{ secrets.AWS_TEST_DEPLOYMENT_BUCKET }}/${{ env.domain }} - name: Invalidate cache - run: aws cloudfront create-invalidation --distribution-id E1INMM566C4FJP --paths "/${{ env.domain }}/*" + run: aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_TEST_CF_DIST_ID }} --paths "/${{ env.domain }}/*" - - name: Print URL - run: | - echo https://${{ env.domain }}/ + - name: Update deployment status + uses: bobheadxi/deployments@master + if: always() + with: + step: finish + token: ${{ secrets.GITHUB_TOKEN }} + status: ${{ job.status }} + env_url: https://${{ env.domain }}/ + deployment_id: ${{ steps.deployment.outputs.deployment_id }} From bf96ed060fc3e90e408bcd87ce1249fb1227f60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Mi=C4=85cz?= Date: Wed, 8 Jul 2020 21:30:09 +0200 Subject: [PATCH 6/8] Add codeowners and conditions for deployment workflows --- .github/CODEOWNERS | 2 ++ .github/workflows/test-env-deploy.yml | 1 + 2 files changed, 3 insertions(+) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 000000000..b9efc0d80 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,2 @@ +# Restrict Test Environment Deployment Workflows to be Approved by Cloud Team +.github/workflows/test-env* @mirumee/saleor-cloud \ No newline at end of file diff --git a/.github/workflows/test-env-deploy.yml b/.github/workflows/test-env-deploy.yml index 911fdae7c..bd8552509 100644 --- a/.github/workflows/test-env-deploy.yml +++ b/.github/workflows/test-env-deploy.yml @@ -9,6 +9,7 @@ on: jobs: deploy: + if: github.event.pull_request.head.repo.full_name == 'mirumee/saleor-dashboard' runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 From 4d22c7d0af1599dca0aad36a09795e585bbdc1e4 Mon Sep 17 00:00:00 2001 From: Milosz Tyborowski Date: Thu, 9 Jul 2020 12:46:02 +0200 Subject: [PATCH 7/8] Use hardcoded action versions --- .github/workflows/test-env-cleanup.yml | 4 ++-- .github/workflows/test-env-deploy.yml | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test-env-cleanup.yml b/.github/workflows/test-env-cleanup.yml index b37fe53db..b61ca6ade 100644 --- a/.github/workflows/test-env-cleanup.yml +++ b/.github/workflows/test-env-cleanup.yml @@ -10,7 +10,7 @@ jobs: cleanup: runs-on: ubuntu-latest steps: - - uses: rlespinasse/github-slug-action@master + - uses: rlespinasse/github-slug-action@2.0.0 - name: Set domain # Set test instance domain based on branch name slug run: | @@ -30,7 +30,7 @@ jobs: run: aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_TEST_CF_DIST_ID }} --paths "/${{ env.domain }}/*" - name: Mark deployment as deactivated - uses: bobheadxi/deployments@master + uses: bobheadxi/deployments@v0.4.2 with: step: deactivate-env token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test-env-deploy.yml b/.github/workflows/test-env-deploy.yml index bd8552509..540f064cb 100644 --- a/.github/workflows/test-env-deploy.yml +++ b/.github/workflows/test-env-deploy.yml @@ -14,10 +14,10 @@ jobs: steps: - uses: actions/checkout@v2 - - uses: rlespinasse/github-slug-action@master + - uses: rlespinasse/github-slug-action@2.0.0 - name: Start deployment - uses: bobheadxi/deployments@master + uses: bobheadxi/deployments@v0.4.2 id: deployment with: step: start @@ -76,7 +76,7 @@ jobs: run: aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_TEST_CF_DIST_ID }} --paths "/${{ env.domain }}/*" - name: Update deployment status - uses: bobheadxi/deployments@master + uses: bobheadxi/deployments@v0.4.2 if: always() with: step: finish From 8e356d1b226ec5d4b98b19b4efb0e717d494082d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Mi=C4=85cz?= Date: Thu, 9 Jul 2020 15:07:24 +0200 Subject: [PATCH 8/8] Fix cleanup & add deafult api uri --- .github/workflows/test-env-cleanup.yml | 2 +- .github/workflows/test-env-deploy.yml | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-env-cleanup.yml b/.github/workflows/test-env-cleanup.yml index b61ca6ade..a0216056d 100644 --- a/.github/workflows/test-env-cleanup.yml +++ b/.github/workflows/test-env-cleanup.yml @@ -24,7 +24,7 @@ jobs: aws-region: ${{ secrets.AWS_DEFAULT_REGION }} - name: Remove S3 directory - run: aws s3 rm s3://${{ secrets.AWS_TEST_DEPLOYMENT_BUCKET }}/${{ env.domain }} + run: aws s3 rm --recursive s3://${{ secrets.AWS_TEST_DEPLOYMENT_BUCKET }}/${{ env.domain }}/ - name: Invalidate cache run: aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_TEST_CF_DIST_ID }} --paths "/${{ env.domain }}/*" diff --git a/.github/workflows/test-env-deploy.yml b/.github/workflows/test-env-deploy.yml index 540f064cb..34b74ffb1 100644 --- a/.github/workflows/test-env-deploy.yml +++ b/.github/workflows/test-env-deploy.yml @@ -41,17 +41,20 @@ jobs: run: | npm ci - - name: Set API URI - # Setting API_URI env variable from PR description + - name: Get custom API_URI + id: api_uri + # Search for API_URI in PR description env: pull_request_body: ${{ github.event.pull_request.body }} prefix: API_URI= pattern: (http|https)://[a-zA-Z0-9.]+/graphql/? run: | - echo "::set-env name=API_URI::$(echo $pull_request_body | grep -Eo "$prefix$pattern" | sed s/$prefix// | head -n 1)" + echo "::set-output name=custom_api_uri::$(echo $pull_request_body | grep -Eo "$prefix$pattern" | sed s/$prefix// | head -n 1)" - name: Run build env: + # Use custom API_URI or the default one + API_URI: ${{ steps.api_uri.outputs.custom_api_uri || 'https://master.staging.saleor.rocks/graphql/' }} APP_MOUNT_URI: / STATIC_URL: / run: |