├── Dockerfile ├── LICENSE ├── README.md ├── action.yml ├── docker-compose.yml └── entrypoint.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM amazon/aws-cli:2.15.40 2 | 3 | RUN yum -y install gzip 4 | 5 | ADD entrypoint.sh /entrypoint.sh 6 | ENTRYPOINT ["/entrypoint.sh"] 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Stanislav 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitHub Action to Sync S3 Bucket and invalidate CloudFront Cache 2 | 3 | > Uses official [amazon/aws-cli](https://hub.docker.com/r/amazon/aws-cli) Docker image to sync a directory with a S3 bucket. 4 | 5 | Before start configure your [AWS credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-creds-create) 6 | 7 | ## Usage 8 | 9 | Example `workflow.yml`: 10 | 11 | ```yaml 12 | 13 | name: S3 Deployment 14 | on: push 15 | 16 | jobs: 17 | deploy: 18 | runs-on: ubuntu-latest 19 | steps: 20 | - name: Checkout 21 | uses: actions/checkout@v3 22 | with: 23 | fetch-depth: 0 24 | - name: Build App 25 | run: npm run build 26 | - name: S3/CloudFront Deploy 27 | uses: staevs/s3-deploy-action@1.0.0 28 | if: success() 29 | with: 30 | args: --follow-symlinks --delete --no-progress 31 | env: 32 | AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} 33 | AWS_REGION: ${{ secrets.AWS_REGION }} 34 | AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }} 35 | AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 36 | CLOUDFRONT_DISTRIBUTION_ID: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} 37 | S3_SOURCE_DIR: 'build' 38 | ``` 39 | 40 | ## Configuration 41 | 42 | > `args` would be passed directly to `aws s3` invocation. Refer to: [AWS S3 docs](https://docs.aws.amazon.com/cli/latest/userguide/cli-services-s3.html) 43 | 44 | | Environment Variable | Required | Description | 45 | |----------------------------|-----------------------------|------------------------------------------------------------------------------------------------------------------| 46 | | AWS_ACCESS_KEY_ID | true | Access Key ID. A unique identifier that's associated with a secret access key | 47 | | AWS_SECRET_ACCESS_KEY | true | Secret Access Key. A key that's used with the access key ID to cryptographically sign programmatic AWS requests. | 48 | | AWS_REGION | true | Your AWS Region | 49 | | AWS_S3_BUCKET | true | Your S3 Bucket name | 50 | | S3_SOURCE_DIR | true | Path to local folder | 51 | | DESTINATION_DIR | false (defaults to S3 root) | Path to folder in a S3 bucket | 52 | | CLOUDFRONT_DISTRIBUTION_ID | false | Your CloudFront distribution | 53 | | CLOUDFRONT_PATHS | false (defaults to /*) | The paths to invalidate (relative to the distribution) | 54 | | USE_GZIP_COMPRESSION | false (defaults to false) | Use gzip for upload | 55 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: "S3/CloudFront Deploy" 2 | description: "Sync a directory to an AWS S3 repository and invalidate CloudFront cache." 3 | branding: 4 | icon: upload-cloud 5 | color: red 6 | author: staevs 7 | runs: 8 | using: docker 9 | image: Dockerfile 10 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | deploy_runner: 3 | image: s3-deploy-action 4 | build: 5 | context: . 6 | environment: 7 | - AWS_S3_BUCKET 8 | - AWS_ACCESS_KEY_ID 9 | - AWS_SECRET_ACCESS_KEY 10 | - AWS_REGION 11 | - S3_DESTINATION_DIR 12 | - S3_SOURCE_DIR 13 | - CLOUDFRONT_DISTRIBUTION_ID 14 | - CLOUDFRONT_PATHS 15 | - USE_GZIP_COMPRESSION 16 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | aws --version 4 | 5 | set -e 6 | 7 | if [ -z "$AWS_S3_BUCKET" ]; then 8 | echo "AWS_S3_BUCKET is not set. Quitting." 9 | exit 1 10 | fi 11 | 12 | if [ -z "$AWS_ACCESS_KEY_ID" ]; then 13 | echo "AWS_ACCESS_KEY_ID is not set. Quitting." 14 | exit 1 15 | fi 16 | 17 | if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then 18 | echo "AWS_SECRET_ACCESS_KEY is not set. Quitting." 19 | exit 1 20 | fi 21 | 22 | if [ -z "$AWS_REGION" ]; then 23 | echo "$AWS_REGION is not set. Quitting." 24 | exit 1 25 | fi 26 | 27 | if [ -z "$S3_SOURCE_DIR" ]; then 28 | echo "$S3_SOURCE_DIR is not set. Quitting." 29 | exit 1 30 | fi 31 | 32 | if [ "$USE_GZIP_COMPRESSION" = true ]; then 33 | echo "Gzipping files..." 34 | 35 | find "./$S3_SOURCE_DIR" -type f -exec gzip "{}" \; -exec mv "{}.gz" "{}" \; 36 | fi 37 | 38 | echo "Uploading files..." 39 | 40 | sh -c "aws s3 sync $S3_SOURCE_DIR s3://$AWS_S3_BUCKET/$DESTINATION_DIR $([ "$USE_GZIP_COMPRESSION" = true ] && echo "--content-encoding gzip" || echo "") $*" 41 | 42 | if [ -n "$CLOUDFRONT_DISTRIBUTION_ID" ]; then 43 | echo "Creating CloudFront invalidation" 44 | aws cloudfront create-invalidation --distribution-id "$CLOUDFRONT_DISTRIBUTION_ID" --paths "${CLOUDFRONT_PATHS:-/*}" 45 | fi 46 | --------------------------------------------------------------------------------