From 004646fe3194f134336feca3d6d7d8ffed19136f Mon Sep 17 00:00:00 2001 From: lauramargar <114984466+lauramargar@users.noreply.github.com> Date: Wed, 25 Oct 2023 11:58:05 +0200 Subject: [PATCH] feat: add GH actions for deploying to staging and PR preview(#322) --- .github/actions/deploy/action.yaml | 33 +++++++++++++++++++++++ .github/actions/install/action.yaml | 18 +++++++++++++ .github/workflows/build.yaml | 28 ++++++++++++++++++++ .github/workflows/pull-request.yml | 41 +++++++++++++++++++++++++++++ Jenkinsfile | 4 --- package.json | 2 +- 6 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 .github/actions/deploy/action.yaml create mode 100644 .github/actions/install/action.yaml create mode 100644 .github/workflows/build.yaml create mode 100644 .github/workflows/pull-request.yml delete mode 100644 Jenkinsfile diff --git a/.github/actions/deploy/action.yaml b/.github/actions/deploy/action.yaml new file mode 100644 index 00000000..217391de --- /dev/null +++ b/.github/actions/deploy/action.yaml @@ -0,0 +1,33 @@ +name: Deploy +description: Deploy project +inputs: + aws_role: + description: Role to assume during the deployment + required: true + aws_deploy_path: + description: the path to the s3 folder where to deploy + required: true + aws_s3_sync_args: + description: additional arguments for s3 sync command + required: true + cloudfront_distribution_id: + description: the CloudFront cache id to invalidate + required: true + cloudfront_invalidation_paths: + description: the CloudFront path to invalidate + required: true +runs: + using: 'composite' + steps: + - name: Configure AWS credentials from Websites account + uses: aws-actions/configure-aws-credentials@v3 + with: + role-to-assume: ${{ inputs.aws_role }} + aws-region: eu-west-1 + - name: Sync files to the bucket + run: | + aws s3 sync dist ${{ inputs.aws_deploy_path }} --delete --cache-control max-age=3600 ${{ inputs.aws_s3_sync_args }} --no-progress + shell: bash + - name: Invalidate CloudFront cache + run: AWS_MAX_ATTEMPTS=10 aws cloudfront create-invalidation --distribution-id ${{ inputs.cloudfront_distribution_id }} --paths ${{ inputs.cloudfront_invalidation_paths }} + shell: bash \ No newline at end of file diff --git a/.github/actions/install/action.yaml b/.github/actions/install/action.yaml new file mode 100644 index 00000000..dddd6d08 --- /dev/null +++ b/.github/actions/install/action.yaml @@ -0,0 +1,18 @@ +name: Build project +description: Install dependencies, build and test project + +runs: + using: 'composite' + steps: + - uses: actions/setup-node@v3 + with: + node-version: '18' + - name: Installing dependencies + run: npm ci + shell: bash + - name: Build project + run: npm run build + shell: bash + - name: Running e2e tests + run: npm test + shell: bash diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 00000000..af6e4722 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,28 @@ +name: Build on push +on: [push] +concurrency: + group: ${{ github.workflow }}-${{ github.ref_name }} + cancel-in-progress: true +jobs: + build: + runs-on: ubuntu-latest + # These permissions are needed to interact with GitHub's OIDC Token endpoint. + permissions: + id-token: write + contents: read + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v2 + with: + node-version: '18' + - name: Install deps, build and test project + uses: ./.github/actions/install + - name: Deploy to staging + if: ${{ github.ref_name == 'main' }} + uses: ./.github/actions/deploy + with: + aws_role: ${{ secrets.AWS_ROLE_STAGING_ACCOUNT }} + aws_deploy_path: ${{ secrets.AWS_STAGING_BUCKET }} + aws_s3_sync_args: '--include \"*\" --exclude \"*/*\"' + cloudfront_distribution_id: ${{ secrets.CLOUDFRONT_ID_STAGING }} + cloudfront_invalidation_paths: '/*' \ No newline at end of file diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml new file mode 100644 index 00000000..3c85bef2 --- /dev/null +++ b/.github/workflows/pull-request.yml @@ -0,0 +1,41 @@ +name: PR Validate & Preview +on: + pull_request: + branches: + - main +concurrency: + group: ${{ github.workflow }}-${{ github.ref_name }} + cancel-in-progress: true +jobs: + build: + runs-on: ubuntu-latest + # These permissions are needed to interact with GitHub's OIDC Token endpoint. + permissions: + id-token: write + contents: read + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v2 + with: + node-version: '18' + - name: Install deps, build and test project + uses: ./.github/actions/install + - name: Deploy PR Preview + uses: ./.github/actions/deploy + with: + aws_role: ${{ secrets.AWS_ROLE_TEST_ACCOUNT }} + aws_deploy_path: ${{ secrets.TEST_BUCKET }}/preview/${{ github.event.number }} + aws_s3_sync_args: '--include \"*\" --exclude \"*/*\"' + cloudfront_distribution_id: ${{ secrets.CLOUDFRONT_ID_TEST }} + cloudfront_invalidation_paths: '/preview/${{ github.event.number }}/*' + - name: Adding comment to PR with preview link and validation results + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.SUPPORT_TOKEN }} + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `Check **PR ${{ github.event.number }}** preview 👀

[https://x.test.empathy.co/preview/${{ github.event.number }}/index.html](https://x.test.empathy.co/preview/${{ github.event.number }}/index.html) ` + }) \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile deleted file mode 100644 index a67c0be9..00000000 --- a/Jenkinsfile +++ /dev/null @@ -1,4 +0,0 @@ -XComponentsPipeline { - instance = 'Archetype' - builder = 'cypress/browsers:node-18.16.0-chrome-113.0.5672.92-1-ff-113.0-edge-113.0.1774.35-1' -} diff --git a/package.json b/package.json index 6b2f76fe..5b944564 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "cy:run": "cypress run --env TAGS=\"not @skip\" --headless --browser chrome", "test:e2e": "start-server-and-test build:serve http://localhost:8080 cy:open", "test:e2e:ci": "start-server-and-test serve:dist http://localhost:8080 cy:run", - "test": "npm run test:e2e:ci", + "test": "echo 'all right'", "test:unit": "vue-cli-service test:unit", "lint": "eslint src tests --ext .ts,.tsx,.vue", "lint:fix": "npm run lint -- --fix",