diff --git a/.github/workflows/site.yml b/.github/workflows/site.yml index b3ef34edd..3f4d97ea6 100644 --- a/.github/workflows/site.yml +++ b/.github/workflows/site.yml @@ -1,13 +1,14 @@ # Deploy site/ to https://copper-browser.github.io/Copper/ with GitHub Pages -# (Settings → Pages → Source: GitHub Actions). A push to fork that touches +# (Settings → Pages → Source: GitHub Actions) and to https://copper.exowatt.com +# (S3 + CloudFront in Exowatt AWS, infra/site-aws.yml). A push to fork that touches # site/ checks it and deploys it; a PR that touches site/ only gets checked. name: site on: push: branches: [fork] - paths: ["site/**", ".github/workflows/site.yml"] + paths: ["site/**", ".github/workflows/site.yml", "infra/site-aws.yml"] pull_request: - paths: ["site/**", ".github/workflows/site.yml"] + paths: ["site/**", ".github/workflows/site.yml", "infra/site-aws.yml"] workflow_dispatch: {} permissions: { contents: read } concurrency: @@ -44,3 +45,30 @@ jobs: - uses: actions/configure-pages@45bfe0192ca1faeb007ade9deae92b16b8254a0d # v6.0.0 - id: deploy uses: actions/deploy-pages@368f82528645a54fb793d4d04e342629a3f51346 # v5.0.1 + + aws: + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/fork' + needs: build + runs-on: ubuntu-latest + timeout-minutes: 10 + # No environment: the deploy role trusts this repo's refs/heads/fork subject only. + permissions: { contents: read, id-token: write } + env: + BUCKET: copper-site-340752802981 + DISTRIBUTION: E2MR74IJVKSHAD + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0 + with: + role-to-assume: arn:aws:iam::340752802981:role/gha-copper-site-deploy + aws-region: us-east-1 + - name: sync site/ to S3 and invalidate CloudFront + run: | + set -euo pipefail + # Assets first (an hour in browsers), HTML last (always revalidated); CloudFront keeps both until the invalidation. + aws s3 sync site/ "s3://$BUCKET/" --delete --exclude '*.html' --exclude README.md --exclude .nojekyll \ + --cache-control 'public, max-age=3600, s-maxage=31536000' --only-show-errors + aws s3 sync site/ "s3://$BUCKET/" --delete --exclude '*' --include '*.html' \ + --cache-control 'public, max-age=0, s-maxage=31536000, must-revalidate' --only-show-errors + id=$(aws cloudfront create-invalidation --distribution-id "$DISTRIBUTION" --paths '/*' --query Invalidation.Id --output text) + aws cloudfront wait invalidation-completed --distribution-id "$DISTRIBUTION" --id "$id" diff --git a/infra/site-aws.yml b/infra/site-aws.yml new file mode 100644 index 000000000..12a3433dd --- /dev/null +++ b/infra/site-aws.yml @@ -0,0 +1,156 @@ +# AWS hosting for site/ at https://copper.exowatt.com (Exowatt account 340752802981, us-east-1): +# private S3 bucket behind CloudFront (OAC, HTTPS only, HTTP/2+3, gzip/br) and the GitHub OIDC +# role that .github/workflows/site.yml assumes to sync site/ and invalidate the cache. +# Same shape as Exowatt-Labs/exowatt-landing-page infra/aws/landing-site.yml. +# Create/update (admin SSO creds): +# aws cloudformation deploy --region us-east-1 --stack-name copper-site \ +# --template-file infra/site-aws.yml --capabilities CAPABILITY_NAMED_IAM --no-fail-on-empty-changeset \ +# --tags owner=crijock app=copper-site +# The stack was first created with --parameter-overrides CertificateArn= (cert waiting on DNS); +# once the cert is ISSUED, run it again with CertificateArn= to attach the name. +# DNS lives in Cloudflare (exowatt.com zone): copper CNAME -> the DistributionDomain output, +# DNS only (grey). Proxied would break: the zone is on Flexible SSL and this redirects to HTTPS. +AWSTemplateFormatVersion: '2010-09-09' +Description: copper.exowatt.com static site (S3 + CloudFront + GitHub deploy role) + +Parameters: + AliasDomain: + Type: String + Default: copper.exowatt.com + CertificateArn: + Type: String + Default: arn:aws:acm:us-east-1:340752802981:certificate/e27ade86-dfb8-4add-8207-641576536f22 + Description: ISSUED ACM cert (us-east-1) for AliasDomain. Empty = CloudFront domain only. + +Conditions: + HasCert: !Not [!Equals [!Ref CertificateArn, '']] + +Resources: + SiteBucket: + Type: AWS::S3::Bucket + DeletionPolicy: Retain + UpdateReplacePolicy: Retain + Properties: + BucketName: !Sub copper-site-${AWS::AccountId} + PublicAccessBlockConfiguration: + BlockPublicAcls: true + BlockPublicPolicy: true + IgnorePublicAcls: true + RestrictPublicBuckets: true + OwnershipControls: + Rules: [{ ObjectOwnership: BucketOwnerEnforced }] + BucketEncryption: + ServerSideEncryptionConfiguration: + - ServerSideEncryptionByDefault: { SSEAlgorithm: AES256 } + + OriginAccessControl: + Type: AWS::CloudFront::OriginAccessControl + Properties: + OriginAccessControlConfig: + Name: !Sub ${AWS::StackName}-oac + OriginAccessControlOriginType: s3 + SigningBehavior: always + SigningProtocol: sigv4 + + BucketPolicy: + Type: AWS::S3::BucketPolicy + Properties: + Bucket: !Ref SiteBucket + PolicyDocument: + Version: '2012-10-17' + Statement: + - Sid: CloudFrontRead + Effect: Allow + Principal: { Service: cloudfront.amazonaws.com } + Action: s3:GetObject + Resource: !Sub ${SiteBucket.Arn}/* + Condition: + StringEquals: + AWS:SourceArn: !Sub arn:aws:cloudfront::${AWS::AccountId}:distribution/${Distribution} + + HeadersPolicy: + Type: AWS::CloudFront::ResponseHeadersPolicy + Properties: + ResponseHeadersPolicyConfig: + Name: !Sub ${AWS::StackName}-headers + SecurityHeadersConfig: + StrictTransportSecurity: { AccessControlMaxAgeSec: 63072000, IncludeSubdomains: false, Preload: false, Override: true } + ContentTypeOptions: { Override: true } + ReferrerPolicy: { ReferrerPolicy: strict-origin-when-cross-origin, Override: true } + FrameOptions: { FrameOption: SAMEORIGIN, Override: true } + + Distribution: + Type: AWS::CloudFront::Distribution + Properties: + DistributionConfig: + Comment: copper.exowatt.com (copper-browser/Copper site/) + Enabled: true + HttpVersion: http2and3 + IPV6Enabled: true + PriceClass: PriceClass_100 + DefaultRootObject: index.html + Aliases: !If [HasCert, [!Ref AliasDomain], !Ref AWS::NoValue] + ViewerCertificate: !If + - HasCert + - { AcmCertificateArn: !Ref CertificateArn, SslSupportMethod: sni-only, MinimumProtocolVersion: TLSv1.2_2021 } + - { CloudFrontDefaultCertificate: true } + Origins: + - Id: s3-site + DomainName: !GetAtt SiteBucket.RegionalDomainName + OriginAccessControlId: !GetAtt OriginAccessControl.Id + S3OriginConfig: { OriginAccessIdentity: '' } + DefaultCacheBehavior: + TargetOriginId: s3-site + ViewerProtocolPolicy: redirect-to-https + AllowedMethods: [GET, HEAD] + CachedMethods: [GET, HEAD] + Compress: true + CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6 # Managed-CachingOptimized; deploys invalidate /* + ResponseHeadersPolicyId: !Ref HeadersPolicy + # Missing files: S3 answers 403 (no ListBucket for CloudFront) -> plain 404. + CustomErrorResponses: + - { ErrorCode: 403, ResponseCode: 404, ResponsePagePath: /index.html, ErrorCachingMinTTL: 60 } + + DeployRole: + Type: AWS::IAM::Role + Properties: + RoleName: gha-copper-site-deploy + Description: Assumed by copper-browser/Copper .github/workflows/site.yml on pushes to fork. + MaxSessionDuration: 3600 + AssumeRolePolicyDocument: + Version: '2012-10-17' + Statement: + - Effect: Allow + Principal: { Federated: !Sub 'arn:aws:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com' } + Action: sts:AssumeRoleWithWebIdentity + Condition: + StringEquals: + token.actions.githubusercontent.com:aud: sts.amazonaws.com + # The repo uses GitHub's immutable subject (owner@id/repo@id); the plain form is kept in case it is turned off. + token.actions.githubusercontent.com:sub: + - repo:copper-browser@336654143/Copper@1383722266:ref:refs/heads/fork + - repo:copper-browser/Copper:ref:refs/heads/fork + Policies: + - PolicyName: deploy-site + PolicyDocument: + Version: '2012-10-17' + Statement: + - Effect: Allow + Action: s3:ListBucket + Resource: !GetAtt SiteBucket.Arn + - Effect: Allow + Action: [s3:GetObject, s3:PutObject, s3:DeleteObject] + Resource: !Sub ${SiteBucket.Arn}/* + - Effect: Allow + Action: [cloudfront:CreateInvalidation, cloudfront:GetInvalidation] + Resource: !Sub arn:aws:cloudfront::${AWS::AccountId}:distribution/${Distribution} + +Outputs: + BucketName: + Value: !Ref SiteBucket + DistributionId: + Value: !Ref Distribution + DistributionDomain: + Value: !GetAtt Distribution.DomainName + DeployRoleArn: + Value: !GetAtt DeployRole.Arn diff --git a/site/README.md b/site/README.md index 63e5356b3..d66fa4577 100644 --- a/site/README.md +++ b/site/README.md @@ -1,7 +1,8 @@ # Copper — website The static landing page for [Copper](https://github.com/copper-browser/Copper), meant to be -served by GitHub Pages at `https://copper-browser.github.io/Copper/`. +served by GitHub Pages at `https://copper-browser.github.io/Copper/` and by Exowatt AWS +(S3 + CloudFront, [`infra/site-aws.yml`](../infra/site-aws.yml)) at `https://copper.exowatt.com`. - `index.html` — the whole page: inline CSS and a little vanilla JS. No framework, no build step, no external fonts, CDNs or trackers.