How can I empty the S3 bucket before deploying travis

This is my .travis.yml :

 deploy: provider: s3 access_key_id: $AWS_ACCESS_KEY secret_access_key: $AWS_SECRET_KEY bucket: domain.com skip_cleanup: true acl: public_read region: ap-northeast-1 endpoint: domain.com.s3-website-ap-northeast-1.amazonaws.com detect_encoding: true on: branch: master 

But this is only downloading files to the trash, not synchronization. How can I sync or clear S3 Bucket files?

+8
amazon-s3 amazon-web-services travis-ci
source share
3 answers

The update helped me find a solution. Thank you This is cleaner than the answer suggested by hussfelt.

Using awscli

Since using the specified command required some research, I will explain how I had to change my .travis.yml for everyone who found this post.

 before_deploy: pip install --user awscli 

Install awscli first to enable synchronization with the S3 bucket. We cannot use sudo to work with the container architecture of Travis, so install --user in the home directory. On Linux, which is the default OS on Travis, binaries installed with this option are located in ~/.local/bin/ -

 deploy: provider: script 

Then use the script provider to run the user command as a deployment method.

  script: ~/.local/bin/aws s3 sync dist s3://example.com --region=eu-central-1 --delete 

This line loaded your files. aws s3 sync used to synchronize files between the local machine and buckets. Full documentation is available here .

In my dist example, there is an assembly folder that we want to upload to S3. Your build system might call it build or something else. "example.com" is the name of your bucket. An area argument is required to uniquely identify your bucket.

A really interesting bit in this command is the --delete switch, which is the solution to our problem. When installed, aws will delete all files found in your bucket, but not in your build directory.

  skip_cleanup: true on: branch: master 

skip_cleanup must be installed or none of your files will be downloaded. Personally, I like that the deployment of Travis is only fixed to master , but any configuration is possible here. See docs for more details.

Environment

We need to provide aws our AWS credentials to allow any interaction. The environment variables used by aws are AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY . hussfelt writes how to provide them in his answer. This process is also described in the Travis documentation: encryption and AWS Features .

Complete solution

 # Deploy using awscli to enable pruning of removed files before_deploy: pip install --user awscli deploy: provider: script script: ~/.local/bin/aws s3 sync dist s3://example.com --region=eu-central-1 --delete skip_cleanup: true on: branch: master 
+4
source share

To solve this problem, I installed AWS cli from pip and ran a script before deploying it.

This is what you need in your .travis.yml:

 before_install: - pip install --user awscli - export PATH=$PATH:$HOME/.local/bin before_deploy: bin/deploy.sh 

You also need to protect two environment variables inside your .travis.yml, which is ready for aws-cli:

 travis encrypt AWS_ACCESS_KEY_ID=YOUR_KEY_HERE --add travis encrypt AWS_SECRET_ACCESS_KEY=YOUR_SECRET_HERE --add 

Your bin / deploy.sh should look something like this:

 #!/bin/sh echo "Clearing bucket: s3://your-bucket/path/inside/bucket/if/you/want" aws s3 rm s3://your-bucket/path/inside/bucket/if/you/want --recursive --region eu-central-1 

Not that we indicate an area that seems mandatory for aws cli here.

Hope this helps!

+2
source share

Thanks guys, the above scripts helped me build the next one that worked for me

before_script: - pip install awscli - export PATH=$PATH:$HOME/.local/bin - AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY aws s3 rm s3://$BUCKET --recursive --region=$REGION

0
source share

All Articles