A basic CI/CD deployment from Bitbucket to Cloudways
Deploying code to servers is a perennial problem, especially for lone freelancers like me who don’t have nice Devops people to help. FTP (by which I mean SFTP, of course) is cumbersome and time-consuming. There are great services like DeployHQ but these come at a cost and I go through phases where I just don’t do that much deploying.
But I’ve found myself working on a handful of small projects and thought the time had come to get a basic CI/CD pipeline working (which is, I guess, exactly what DeployHQ is doing, or its equivalent anyway).
I found this very helpful video, but I’m going to go through the basics here since Bitbucket has updated a little since the video was published.
Initial context…
- My code is held in a git repository
- The git repo is hosted by Bitbucket
- The files are hosted by Cloudways
Cloudways settings…
- Go to the Access details screen of the application
- Click + Add SFTP user and add the user details
- Go to Application settings
- Enable SSH access
Bitbucket settings…
- Go to Repository settings of the repo
- Under Pipelines click Settings
- Enable pipelines
- Also under Pipelines click Repository variables
- In the Name field type “SFTP_USERNAME”, and in the Value field type (or copy/paste) the username you chose for the SFTP username in Cloudways above. Leave secured ticked and click Add
- Again, in the Name field type “SFTP_LOCATION”, and in the Value field type (or copy/paste) the IP address of your Cloudways application’s Public IP (under Access details > Application Credentials). Leave secured ticked and click Add
- Again under Pipelines click SSH keys
- Click Generate keys and the Copy public key
Back in Cloudways…
- Go to the Access details screen of the application and click View SSH keys under “SSH/SFTP details”
- Give it a label (e.g., “Bitbucket deployment”), paste the key you copied from Bitbucket, and click Submit
- Copy the Public IP
Back to Bitbucket again…
- Still on the SSH keys screen under Known hosts, paste the IP address into the Host address field and click Fetch
- When the key is returned, click Add host
Right, back in my code…
- Create a new file called “bitbucket-pipelines.yml”
- Paste this into the file:
pipelines:
branches:
main:
- step:
name: "Deploy to production"
script:
- pipe: atlassian/sftp-deploy:0.10.0
variables:
USER: $SFTP_USERNAME
SERVER: $SFTP_LOCATION
REMOTE_PATH: "public_html/"
LOCAL_PATH: "./"
- Then, in the terminal:
>git add --all
>git commit -m "Added pipelines config"
>git push
Now, when you next >git push it’ll deploy automatically
Problems I encountered..
Getting the indenting / formatting of the .yml file was problematic. This was helpful:
https://bitbucket.org/product/pipelines/validator
I’ve also found I have to be careful about setting the path correctly.
More info…
https://bitbucket.org/product/features/pipelines/integrations?category=deployment&