Skip to content

Instantly share code, notes, and snippets.

@mcnamee
Last active December 14, 2023 03:57
Show Gist options
  • Save mcnamee/aa141afde5c0359c568c169a56f100d7 to your computer and use it in GitHub Desktop.
Save mcnamee/aa141afde5c0359c568c169a56f100d7 to your computer and use it in GitHub Desktop.
Bitbucket Pipelines - Deploy via FTP to shared hosting
# Installation ---
# 1. In Bitbucket, add FTP_USERNAME, FTP_PASSWORD and FTP_HOST as environment variables.
# 2. Commit this file (bitbucket-pipelines.yml) to your repo (in the repo root dir)
# 3. From Bitbucket Cloud > Commits > Commit Number > Run Pipeline > Custom:Init (this will
# push everything and initialize GitFTP)
#
# Usage ---
# - On each commit to master branch, it'll push all files to the $FTP_HOST
# - You also have the option to 'init' (see 'Installation' above) - pushes everything and initialises
# - Finally you can also 'deploy-all' (from Bitbucket Cloud > Commits > Commit Number > Run Pipeline > Custom:deploy-all)
# if multiple deploys fail, you can deploy everything to "catch up"
#
image: wearepvtl/bitbucket-pipelines-git-ftp:latest
pipelines:
custom: # Pipelines that are triggered manually via the Bitbucket GUI
init: # -- First time init
- step:
caches:
- node
- composer
script:
- npm install
- git ftp init -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST
deploy-all: # -- Deploys all files from the selected commit
- step:
caches:
- node
- composer
script:
- npm install
- git ftp push -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST --all
branches: # Automated triggers on commits to branches
master: # -- When committing to master branch
- step:
deployment: production
caches:
- node
- composer
script:
- npm install
- git ftp push -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST
@JustEugen
Copy link

Thank you for this

@Kaapiii
Copy link

Kaapiii commented Jun 6, 2018

Thanks, the custom steps are nice.
If anyone faces the issue that the pipeline fails with the message:

  • apt-get update
    bash: apt-get: command not found

Then you probably used a reserved word in your environement variables.

@zanstro
Copy link

zanstro commented Jul 4, 2018

Thanks, that's helpful.
Is there any modification that can be made to include a staging deployment alongside the production deployment? It would be useful to utilize two branches, one for staging deployment (branch name: staging) and one for production deployment (branch name: master). Once we push to staging branch, the code is deployed to the staging server and correspondingly with the production.

@goatfryed
Copy link

@dryokolaptis If you want to make a staging deployment, add

develop: # -- When committing to developbranch
    - step:
        script:
          - npm install
          - git ftp push -u "$FTP_DEV_USERNAME" -p "$FTP_DEV_PASSWORD" ftp://$FTP_DEV_HOST

to your branches definition along with additional env vars.

@goatfryed
Copy link

Question: If I put node_modules and public/dist on git ignore, i assume the git ftp push will ignore these folders, even if i do npm install and some npm run build.
Is there a way to force git ftp push to also push some ignored files or is there another workaround?

My approach would be to cache the ignored directories, then after install and build use some combination of find and basic ftp

@mcnamee
Copy link
Author

mcnamee commented Aug 19, 2018

Hi @goatfryed
Take a look at https://github.com/git-ftp/git-ftp/blob/master/man/git-ftp.1.md#syncing-untracked-files

Essentially you can add files/directories to .git-ftp-include

@Mochnant
Copy link

Thanks for the sample. I'm trying to make it work with sftp, but it fails with this message:

curl: (1) Protocol sftp not supported or disabled in libcurl

Is there something I can do to fix this? Thanks.

@Mochnant
Copy link

I fixed the error by switching to a different Docker image:

image: madshansen/docker-git-ftp:latest

@FDiskas
Copy link

FDiskas commented Nov 16, 2018

please update node version to latest lts version

@iconifyit
Copy link

I get an error saying no package.json is found in the current directory. Are there some preliminary steps that must be taken in order to use this? My code that is being deployed does not use any packages. Just vanilla HTML & PHP code.

@fu-sen
Copy link

fu-sen commented Feb 15, 2019

@iconifyit I also encountered the same problem. package.json is used with npm install.
Therefore, deleting the 3 lines:

          - npm install

After this fix, you get the expected behavior.

@edyrkaj
Copy link

edyrkaj commented Feb 22, 2019

hi,
How to push only /dist folder after running npm scripts commands.
Thank you

@mahammedzkhan
Copy link

This doesn't work, it gives errors when trying to initialize the repo because of the FTP_URL.

@AhmedCommando
Copy link

hi,
How to push only /dist folder after running npm scripts commands.
Thank you

any luck?

@ezvr
Copy link

ezvr commented Mar 12, 2019

i also want to know this. also i'd like to know how to push to a specific ftp folder

@shaneseaton
Copy link

For anyone else struggling with this, I put my env vars in the settings > deployments > production area. This meant that for the init to work correctly, I had to add the 'deployment: production' line to each of the steps that wanted access to those vars.

e.g

  custom: 
    init: 
    - step:
        deployment: production
        script:
          - git ftp init -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST
    deploy-all: 
    - step:
        deployment: production
        script:
          - git ftp push -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST --all

Hope that saves anyone else some confusion.

@thorst
Copy link

thorst commented Jul 11, 2019

If you need to deploy to a sub-directory (such as htdocs) include the full path as your ftp host.

@makeitTim
Copy link

Should be able to sync just a sub-folder as if it were root like this:

git ftp push --syncroot dist/ -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST

However, none of this is working for me. Bitbucket doesn't think git ftp exists:

+ git ftp push --syncroot as/ -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST
git: 'ftp' is not a git command. See 'git --help'.

@makeitTim
Copy link

makeitTim commented Nov 28, 2019

Already fixed git: 'ftp' is not a git command. I had commented out image: aariacarterweir/lamp-gitftp:latest because it looked like specific to somebody else's environment stack, but it is needed.

This works awesomely! Thanks.

@JosepRoo
Copy link

Question: If I put node_modules and public/dist on git ignore, i assume the git ftp push will ignore these folders, even if i do npm install and some npm run build.
Is there a way to force git ftp push to also push some ignored files or is there another workaround?

My approach would be to cache the ignored directories, then after install and build use some combination of find and basic ftp

Force the add on that folder and the commit it
script:
- npm install
- git add --force dist
- git commit -m "Build"
- git ftp push --syncroot dist -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST

@thorst
Copy link

thorst commented Dec 5, 2020

This started failing last night, docker image no longer there, but execution was much faster than bitbuckets normal image

@mcnamee
Copy link
Author

mcnamee commented Dec 6, 2020

Hi @thorst - thanks for letting me know. I've setup a new Docker image and updated the script above.
Simply update the reference image to image: wearepvtl/bitbucket-pipelines-git-ftp:latest (from image: aariacarterweir/lamp-gitftp:latest)

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