2

I've got a NodeJS project in a Bitbucket repo, and I am struggling to understand how to use Bitbucket Pipelines to get it from there onto my DigitalOcean server, where it can be served on the web.

So far I've got this

image: node:10.15.3

pipelines:
  default:
    - parallel:
        - step:
            name: Build
            caches:
              - node
            script:
              - npm run build

So now the app was built and should be saved as a single file server.js in a theoretical /dist directory.

How now do I dockerize this file and then upload it to my DigitalOcean? I can't find any examples for something like this.

I did find a Docker template in the Bitbucket Pipelines editor, but it only somewhat describes creating a Docker image, and not at all how to actually deploy it to a DigitalOcean server (or anywhere)

  - step:
      name: Build and Test
      script:
        - IMAGE_NAME=$BITBUCKET_REPO_SLUG
        - docker build . --file Dockerfile --tag ${IMAGE_NAME}
        - docker save ${IMAGE_NAME} --output "${IMAGE_NAME}.tar"
      services:
        - docker
      caches:
        - docker
      artifacts:
        - "*.tar"
  - step:
      name: Deploy to Production
      deployment: Production
      script:
        - echo ${DOCKERHUB_PASSWORD} | docker login --username "$DOCKERHUB_USERNAME" --password-stdin
        - IMAGE_NAME=$BITBUCKET_REPO_SLUG
        - docker load --input "${IMAGE_NAME}.tar"
        - VERSION="prod-0.1.${BITBUCKET_BUILD_NUMBER}"
        - IMAGE=${DOCKERHUB_NAMESPACE}/${IMAGE_NAME}
        - docker tag "${IMAGE_NAME}" "${IMAGE}:${VERSION}"
        - docker push "${IMAGE}:${VERSION}"
      services:
        - docker
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363

1 Answers1

1

You would have to SSH into your DigitalOcean VPS and then do some steps there:

  • Pull the current code
  • Build the docker file
  • Deploy the docker file

An example could look like this:

Create some script like "deployment.sh" in your repository root:

cd <path_to_local_repo>
git pull origin master
docker container stop <container_name>
docker container rm <container_name>
docker image build -t <image_name> .
docker container run -itd --name <container_name> <image_name>

and then add the following into your pipeline:

# ...
    - step:
        deployment: staging
        script:
          - cat ./deployment.sh | ssh <ssh_user>@<ssh_host>

You have to add your ssh key for your repository on your server, though. Check out the following link, on how to do this: https://confluence.atlassian.com/display/BITTEMP/Use+SSH+keys+in+Bitbucket+Pipelines

Here is a similar question, but using PHP: Using BitBucket Pipelines to Deploy onto VPS via SSH Access

MauriceNino
  • 6,214
  • 1
  • 23
  • 60
  • `You have to add your ssh key for your repository on your server,` Maurice, is there any way to accomplish this without putting the ssh key on the server? – CodyBugstein Aug 05 '21 at 01:12
  • Maybe there is, but why would you not want to put an ssh key on your server? That's how you connect to servers... @CodyBugstein – MauriceNino Aug 05 '21 at 08:33
  • Maurice, do you know how I can pass variables to `./deployment.sh` ? – CodyBugstein Aug 18 '21 at 05:32
  • Can you elaborate @CodyBugstein? – MauriceNino Aug 18 '21 at 08:32
  • i.e. I want the script to know what branch we're in – CodyBugstein Aug 20 '21 at 03:52
  • According to [this doc](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/), you can use `$BITBUCKET_BRANCH` in your script to pass it - so you will have to write `- cat ./deployment.sh $BITBUCKET_BRANCH | ssh @`. Then in your bash script you can reference this variable with `$1` @CodyBugstein – MauriceNino Aug 20 '21 at 10:26