philschmid RSS feed 09月30日
GitHub Actions自动化部署S3应用教程
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

Nat Friedman介绍GitHub Actions作为工作流编排API,GitHub管理执行、反馈和安全性。本文演示如何5分钟内设置GitHub Action自动部署S3 web应用并创建CloudFront缓存失效。支持React、Vue、Angular等框架,需提前准备S3桶和CloudFront分布。教程涵盖创建工作流文件、配置触发条件、设置环境变量和 Secrets,使用jakejarvis/s3-sync-action和chetan/invalidate-cloudfront-action实现自动化部署。

📁 GitHub Actions是GitHub提供的API,用于编排基于任何事件的工作流,GitHub负责执行、提供反馈和保障每一步的安全性,使得工作流和步骤成为存储库中的代码,可创建、共享、重用和分支软件开发实践。

⚙️ 本文演示如何5分钟内设置GitHub Action自动部署S3 web应用并创建CloudFront缓存失效,支持React、Vue、Angular等框架,前提是已部署S3桶和CloudFront分布。

📄 教程步骤包括:创建.github/workflows/deploy-app-on-s3.yaml文件,配置触发条件(如master分支pull request关闭时触发),设置环境变量(S3桶名、CloudFront ID)和Secrets(AWS访问密钥)。

🚀 使用社区构建的jakejarvis/s3-sync-action上传应用到S3,并使用chetan/invalidate-cloudfront-action创建缓存失效,可通过修改配置调整源目录、AWS区域等参数。

🔑 需要提前创建IAM用户并获取AWS访问密钥/密钥ID,存储在GitHub Secrets中,确保Action有权限部署到S3和操作CloudFront。

Nat Friedman described Github Actions as an API “… to orchestrate any workflow, based on any event, while GitHubmanages the execution, provides rich feedback and secures every step along the way. With GitHub Actions, workflows andsteps are just code in a repository, so you can create, share, reuse, and fork your software development practices.”

You can read his full blog post here.

This blog post explains how to set up a GitHub action within 5 minutes to automatically deploy your hosted web app on S3and create an automatic CloudFront cache Invalidation. You will be able to deploy any app that runs on S3 be it React,Vue, Angular or svelte.

This Action is using 2 community-built actions fromjakejarvis and chetan.


TL;DR

If you don´t want to read the complete post. Copy the action from this Github repository and add at the Github secretsto your repository. If you fail, come back and read the article!


Requirements

This post assumes that you have already deployed a working web app on s3 with CloudFront distribution before. So therequirements are a working web app, with build script in the package.json, a static hosting bucket on s3, a workingCloudFront distribution and IAM User with programmatic access and enough permissions to deploy to s3 and create aCloudFront chance invalidation

Now let’s get started with the tutorial.


Create folders & files

The first thing we have to do is create the folder .github with a folder workflows in it on your project root level.Afterward create the deploy-app-on-s3.yaml file in it.

Creating the Github Action

Copy this code snippet into the deploy-app-on-s3.yaml file.

name: deploy-app-on-s3 on:  pull_request:    branches: [master]    types: [closed]jobs:  deploy:    runs-on: ubuntu-latest    env:      AWS_S3_BUCKET_NAME: your-bucket-name      AWS_CF_DISTRIBUTION_ID: your-cloudfront-id    strategy:      matrix:        node-version: [10.x]    steps:      - uses: actions/checkout@master      - name: Use Node.js ${{ matrix.node-version }}        uses: actions/setup-node@v1        with:          node-version: ${{ matrix.node-version }}      - name: Install Dependencies        run: npm install      - name: Build Application        run: npm run-script build      - uses: jakejarvis/s3-sync-action@master        name: Upload App to S3 Bucket        with:          args: --follow-symlinks --delete --cache-control max-age=2592000        env:          AWS_S3_BUCKET: ${{ env.AWS_S3_BUCKET_NAME }}          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}          AWS_REGION: 'eu-central-1'          SOURCE_DIR: 'dist'      - name: Create CloudFront Cache Invalidation        uses: chetan/invalidate-cloudfront-action@master        env:          DISTRIBUTION: ${{ env.AWS_CF_DISTRIBUTION_ID }}          PATHS: '/*'          AWS_REGION: 'eu-central-1'          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

This code snippet describes the action. The Github Action will be triggered after your pull request on the masterbranch is successfully closed. You can change this by adjusting the on section in the snippet. If you want a differenttrigger for your action look here. Itcould be possible that you have to adapt for example the SOURCE_DIR from "dist" to your build directory or theAWS_REGION.

Adjust environment variables

The third step is to adjust all environment variables. In this action, we have the bucket name AWS_S3_BUCKET_NAME andthe CloudFront distribution ID AWS_CF_DISTRIBUTION_ID as an environment variable. The value of AWS_S3_BUCKET_NAME isthe name of your S3 Bucket you can find in the management console and the value of AWS_CF_DISTRIBUTION_ID is the id ofthe CloudFront distribution.

You can get the ID for the AWS_CF_DISTRIBUTION_ID variable of the CloudFront distribution via the management consoleby navigating to the "CloudFront" service and then going on "Distribution".

The table has a column "ID" with the value we need. You can recognize the correct row by identifying your S3 Bucket namein the column "ohttps://www.philschmid.de/static/blog/set-up-a-ci-cd-pipeline-for-your-web-app-on-aws-s3-with-github-actions/cloudfront.pngub-actions/cloudfront.png" alt="AWS Cloudfront Service">

Add secrets to your repository

The fourth and last step is to add secrets to your Github repository. For this Github Action, we need the access key IDand secret access key from IAM User as secrets called AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

If you are not sure on how to create an IAM user for the access key ID and secret access key you can readhere.

Adding the secrets

To add the secrets you have to go the "shttps://www.philschmid.de/static/blog/set-up-a-ci-cd-pipeline-for-your-web-app-on-aws-s3-with-github-actions/navigation.pngb-app-on-aws-s3-with-github-actions/navigation.png" alt="Github Repository Naivhttps://www.philschmid.de/static/blog/set-up-a-ci-cd-pipeline-for-your-web-app-on-aws-s3-with-github-actions/settings.png-ci-cd-pipeline-for-your-web-app-on-aws-s3-with-github-actions/settings.png" alt="Github Repository Settings">

and on the secrets page, you can add youhttps://www.philschmid.de/static/blog/set-up-a-ci-cd-pipeline-for-your-web-app-on-aws-s3-with-github-actions/secrets.pngsrc="/static/blog/set-up-a-ci-cd-pipeline-for-your-web-app-on-aws-s3-with-github-actions/secrets.png" alt="Github Repository Secrets">

Crab a coffee and enjoy it

Lastly, you have to create a pull request from a fhttps://www.philschmid.dehttps://www.philschmid.de/static/blog/set-up-a-ci-cd-pipeline-for-your-web-app-on-aws-s3-with-github-actions/github-action.pngation.


I created a demo repository with a vue app as example. You can find the repositoryhere. If something is unclear let me know and i willadjust it.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

GitHub Actions S3部署 自动化工作流 CloudFront缓存失效 前端开发 DevOps
相关文章