The update helped me find a solution. Thanks! It's cleaner than the answer proposed by hussfelt.
Using awscli
Since using the listed command required some research I'll explain how I had to change my .travis.yml
for any others who find this post.
before_deploy: pip install --user awscli
First install awscli
to enable syncing with your S3 bucket. To run on Travis' container-based architecture we can't use sudo
, so install to the home directory with --user
. On Linux, which is the default OS on Travis, binaries installed with this option is located in ~/.local/bin/
-
deploy:
provider: script
Next, use the script
provider to run a custom command as the deployment method.
script: ~/.local/bin/aws s3 sync dist s3://example.com --region=eu-central-1 --delete
This line is were your files are uploaded. The aws s3 sync
is used to sync files between a local machine and buckets. The full documentation is available here.
In my example dist
is the build folder we want to upload to S3. Your build system might call it build
or something else. "example.com" is the name of your bucket. The region argument is required to uniquely identify your bucket.
The really interesting bit in this command is the --delete
switch which is the solution to our problem. When set, aws
will delete any files found in your bucket but not in your build directory.
skip_cleanup: true
on:
branch: master
skip_cleanup
should be set or none of your files will be uploaded. Personally I like to have Travis deploy only commits to master
, but any configuration is possible here. See the docs for more info.
Environment
We need to give aws
our AWS credentials to authorise any interaction. The environment variables used by aws
are AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
. hussfelt writes how to provide these in their answer. The process is also described in the Travis documentation: encryption and AWS specifics.
Full 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