{"id":3485,"date":"2017-04-25T16:00:22","date_gmt":"2017-04-25T14:00:22","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=3485"},"modified":"2020-02-09T10:59:52","modified_gmt":"2020-02-09T09:59:52","slug":"deployment-travis-ci","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/deployment-travis-ci-3485\/","title":{"rendered":"Auto deployment to staging and production server with Travis CI"},"content":{"rendered":"\n
Two weeks ago I wrote a post about deployment via GitLab<\/a>. Now I realized the same behavior for a public GitHub repository with Travis, and this post shows you how I did it.<\/p>\n\n\n\n\n\n\n\n The goal was, like already said, the same like in the GitLab post \u2014 I wanted to move a theme to the staging or production server, depending on if the push goes to Travis works with a At first, we set Be careful not to write sensitive data directly into the file, but use the Travis variables as the file is publicly in the GitHub repo!<\/strong><\/p>\n\n\n\n After that we prevent that we need to confirm the fingerprint of the unknown server<\/a> in the How to get ready for the SSH connection is described in the post \u00bbSSH deploys with Travis CI<\/a>\u00ab on oncletom.io (the encryption does not work without problems on Windows, you can use the Windows Subsystem for Linux on Windows 10. The command I needed to get it working are listed at the end of the post) \u2014 the result is our staging<\/code> or
master<\/code> branch. Travis CI<\/a> is free for public GitHub repos.<\/p>\n\n\n\n
.travis.yml<\/code> file, which stores the CI steps. The finished file looks like that:<\/p>\n\n\n
language: php\n\naddons:\n ssh_known_hosts:\n - $STAGING_SERVER\n - $PRODUCTION_SERVER\n\nbefore_script:\n - echo<\/span> -e \"Host $STAGING_SERVERntStrictHostKeyChecking non\"<\/span> >> ~\/.ssh\/config\n - echo<\/span> -e \"Host $PRODUCTION_SERVERntStrictHostKeyChecking non\"<\/span> >> ~\/.ssh\/config\n\nscript:\n -\nbefore_deploy:\n - openssl aes-256<\/span>-cbc -K $ENCRYPTED_KEY -iv $ENCRYPTED_IV -in deploy_rsa.enc -out \/tmp\/deploy_rsa -d\n - eval<\/span> \"$(ssh-agent -s)\"<\/span>\n - chmod 600<\/span> \/tmp\/deploy_rsa\n - ssh-add \/tmp\/deploy_rsa\n\ndeploy:\n - provider: script\n skip_cleanup: true<\/span>\n script: ssh -p22 $STAGING_SERVER_USER@$STAGING_SERVER \"mkdir -p $STAGING_PATH_STABLE\"<\/span> && ssh -p22 $STAGING_SERVER_USER@$STAGING_SERVER \"mkdir -p $STAGING_PATH_TRUNK\"<\/span> && rsync -rav -e ssh --exclude='.git\/'<\/span> --exclude=scripts\/ --exclude='.travis.yml'<\/span> --delete-excluded .\/ $STAGING_SERVER_USER@$STAGING_SERVER:$STAGING_PATH_TRUNK && rsync -rav -e ssh --exclude='.git\/'<\/span> --exclude=scripts\/ --exclude='.travis.yml'<\/span> --delete-excluded .\/ $STAGING_SERVER_USER@$STAGING_SERVER:$STAGING_PATH_STABLE\n on:\n branch: staging\n - provider: script\n skip_cleanup: true<\/span>\n script: ssh -p22 $PRODUCTION_SERVER_USER@$PRODUCTION_SERVER \"mkdir -p $PRODUCTION_SERVER_THEMES_PATH\/_tmp-bornholm\"<\/span>&& ssh -p22 $PRODUCTION_SERVER_USER@$PRODUCTION_SERVER \"mkdir -p $PRODUCTION_SERVER_THEMES_PATH\/bornholm\"<\/span> && rsync -rav -e ssh --exclude='.git\/'<\/span> --exclude=scripts\/ --exclude='.travis.yml'<\/span> --delete-excluded .\/ $PRODUCTION_SERVER_USER@$PRODUCTION_SERVER:$PRODUCTION_SERVER_THEMES_PATH\/_tmp-bornholm && ssh -p22 $PRODUCTION_SERVER_USER@$PRODUCTION_SERVER \"mv $PRODUCTION_SERVER_THEMES_PATH\/bornholm $PRODUCTION_SERVER_THEMES_PATH\/_old-bornholm && mv $PRODUCTION_SERVER_THEMES_PATH\/_tmp-bornholm $PRODUCTION_SERVER_THEMES_PATH\/bornholm\"<\/span> && ssh -p22 $PRODUCTION_SERVER_USER@$PRODUCTION_SERVER \"rm -rf $PRODUCTION_SERVER_THEMES_PATH\/_old-bornholm\"<\/span>\n on:\n branch: master<\/code><\/div>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n
php<\/code> as the language of our project and add the server addresses to the
known_hosts<\/code>, like described in the docu post \u00bbAdding to SSH Known Hosts<\/a>\u00ab \u2014 the variables can be set under Settings in the repo in Travis.<\/p>\n\n\n\n
before_script<\/code> part. If no
script<\/code> is defined, Travis runs a default procedur depending on the choosen language (for example PHPunit for PHP) \u2013 we only want a deployment, so we set an empty script.<\/p>\n\n\n\n
before_deploy<\/code>.<\/p>\n\n\n\n