Skip to content

Instantly share code, notes, and snippets.

@masutaka
Created April 16, 2017 10:25
Show Gist options
  • Save masutaka/049178f0ab55baba8ce6b4e12e42fba8 to your computer and use it in GitHub Desktop.
Save masutaka/049178f0ab55baba8ce6b4e12e42fba8 to your computer and use it in GitHub Desktop.
capistrano deployment using CircleCI 2.0
version: 2
jobs:
build:
docker:
- image: ruby:2.3.3-alpine
working_directory: /home/circleci/masutaka.net
steps:
- setup_remote_docker:
# https://circleci.com/docs/2.0/docker-layer-caching/
reusable: true
- run:
name: Install System Dependencies
# See also https://circleci.com/docs/2.0/custom-images/#required-tools
command: apk add --update --no-cache git openssh-client tar gzip build-base tzdata
- run:
name: Set timezone to Asia/Tokyo
command: cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
- checkout
- restore_cache:
name: Restore bundler cache
keys:
- gems-{{ .Environment.COMMON_CACHE_KEY }}-{{ checksum "Gemfile.lock" }}
- gems-{{ .Environment.COMMON_CACHE_KEY }}-
- run:
name: Install Ruby Dependencies
command: bundle check || bundle install --jobs=4 --retry=3
- save_cache:
name: Save bundler cache
key: gems-{{ .Environment.COMMON_CACHE_KEY }}-{{ checksum "Gemfile.lock" }}
paths:
- /usr/local/bundle
- add_ssh_keys:
fingerprints:
# for ssh to masutaka.net
# See https://circleci.com/gh/masutaka/masutaka.net/edit#ssh
- "43:7f:b2:..."
- deploy:
name: Maybe Deploy
command: |
if [ "${CIRCLE_BRANCH}" != "master" ]; then
exit 0
fi
if [ -z "${BUNDLE_UPDATE}" ]; then
./script/deploy-circleci.sh
else
gem install -N bundler circleci-bundle-update-pr
circleci-bundle-update-pr CircleCI circleci@example.com
fi
#!/bin/sh -eu
cat <<EOF >> $HOME/.ssh/config
User masutaka
ForwardAgent yes
EOF
# Add the preferred key for getting GitHub Permission
# See https://circleci.com/gh/masutaka/masutaka.net/edit#checkout
eval $(ssh-agent)
ssh-add
bundle exec cap prod deploy
@masutaka
Copy link
Author

masutaka commented Apr 16, 2017

The target repository is https://github.com/masutaka/masutaka.net which is a private one.

@autobolt
Copy link

autobolt commented Nov 2, 2017

Thanks for sharing the circleci config! It has been very useful!

@stevepaulo
Copy link

When I try to adapt this setup to my own project, CircleCI asks for my deploy user's password when it gets to the cap production deploy step. But that doesn't happen when I run that command manually. I'm assuming I have the wrong SSH keys set up in the wrong place? Do you have any insight into this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment