Hello,
So I'm creating CICD with bitbucket pipelines and digital ocean.
my step are to create an image and push it.
to do this, I need to install `doctl` and login.
is there any way that I can keep doctl cached to be used with other steps instead of each time installing it?
Also, if possible to know, in the below PR section, is it running in case of PRs on the development branch as well? although I'm protecting the development to accept only PRs?
if so, is there a way to run all PR except for `master` and `dev`?
here is my yml
# Template NodeJS build
# This template allows you to validate your NodeJS code.
# The workflow allows running tests and code linting on the default branch.
image: node:16
pipelines:
pull-requests:
'**':
- step:
name: Build, Lint and test
caches:
- node
script:
- npm install
- npm run precommit
- npm test
branches:
dev:
- step:
name: Build, Lint and test
caches:
- node
script:
- npm install
- npm run precommit
- npm test
- step:
name: Build docker and Test
script:
- IMAGE_NAME=$BITBUCKET_REPO_SLUG
- docker build . --file ./docker/dev/backend/Dockerfile --tag ${IMAGE_NAME}
- docker save ${IMAGE_NAME} --output "${IMAGE_NAME}.tar"
services:
- docker
caches:
- docker
artifacts:
- "*.tar"
- step:
name: push image
services:
- docker
script:
- IMAGE_NAME=$BITBUCKET_REPO_SLUG
- VERSION="dev-0.1.${BITBUCKET_BUILD_NUMBER}"
- IMAGE=${DOCKERHUB_NAMESPACE}/${IMAGE_NAME}
- wget https://github.com/digitalocean/doctl/releases/download/v1.86.0/doctl-1.86.0-linux-amd64.tar.gz
- tar xf doctl-1.86.0-linux-amd64.tar.gz
- mv doctl /usr/local/bin
- which doctl
- docker load --input "${IMAGE_NAME}.tar"
- doctl auth init -t $DIGITALOCEAN_ACCESS_TOKEN
- doctl account get
- doctl registry login --expiry-seconds 600
- docker tag "${IMAGE_NAME}" "${IMAGE}:${VERSION}"
- docker push "${IMAGE}:${VERSION}"
- step:
name: deploy to development
deployment: Staging
services:
- docker
script:
- IMAGE_NAME=$BITBUCKET_REPO_SLUG
- IMAGE=${DOCKERHUB_NAMESPACE}/${IMAGE_NAME}
- VERSION="dev-0.1.${BITBUCKET_BUILD_NUMBER}"
- pipe: atlassian/ssh-run:0.4.1
variables:
SSH_USER: 'root'
SERVER: $HOST
COMMAND: '
doctl registry login --expiry-seconds 600;
docker rm -f app_dev 2> /dev/null;
docker run --name app_dev -d "${IMAGE}:${VERSION}"
docker network connect app_dev-network app_dev
'
SSH_KEY: $SSH_PRIVATE_KEY
EXTRA_ARGS: '-o StrictHostKeyChecking=no'
ENV_VARS: >-
IMAGE=${IMAGE}
VERSION=${VERSION}
DB_HOST=${DB_HOST}
DB_PORT=${DB_PORT}
DB_USER=${DB_USER}
DB_PASS=${DB_PASS}
DB_NAME=${DB_NAME}
APP_PORT=${APP_PORT}
master:
- step:
name: Build, Lint and test
caches:
- node
script:
- npm install
- npm run precommit
- npm test
- step:
name: Build docker and Test
script:
- IMAGE_NAME=$BITBUCKET_REPO_SLUG
- wget https://github.com/digitalocean/doctl/releases/download/v1.86.0/doctl-1.86.0-linux-amd64.tar.gz
- tar xf doctl-1.86.0-linux-amd64.tar.gz
- mv doctl /usr/local/bin
- which doctl
- doctl registry login
- docker build . --file ./docker/prod/backend/Dockerfile --tag ${IMAGE_NAME}
- docker save ${IMAGE_NAME} --output "${IMAGE_NAME}.tar"
services:
- docker
caches:
- docker
artifacts:
- "*.tar"
- step:
name: push image
services:
- docker
script:
- IMAGE_NAME=$BITBUCKET_REPO_SLUG
- VERSION="prod-0.1.${BITBUCKET_BUILD_NUMBER}"
- IMAGE=${DOCKERHUB_NAMESPACE}/${IMAGE_NAME}
- wget https://github.com/digitalocean/doctl/releases/download/v1.86.0/doctl-1.86.0-linux-amd64.tar.gz
- tar xf doctl-1.86.0-linux-amd64.tar.gz
- mv doctl /usr/local/bin
- which doctl
- docker load --input "${IMAGE_NAME}.tar"
- doctl auth init -t $DIGITALOCEAN_ACCESS_TOKEN
- doctl registry login --expiry-seconds 600
- docker tag "${IMAGE_NAME}" "${IMAGE}:${VERSION}"
- docker push "${IMAGE}:${VERSION}"
- step:
name: deploy to production
trigger: manual
deployment: Production
services:
- docker
script:
- IMAGE=${DOCKERHUB_NAMESPACE}/${IMAGE_NAME}
- VERSION="prod-0.1.${BITBUCKET_BUILD_NUMBER}"
- pipe: atlassian/ssh-run:0.4.1
variables:
SSH_USER: 'root'
SERVER: $HOST
COMMAND: '
doctl registry login --expiry-seconds 600;
docker rm -f app_prod 2> /dev/null;
docker run --name app_prod -d "${IMAGE}:${VERSION}"
docker network connect app_dev-network app_prod
'
SSH_KEY: $SSH_PRIVATE_KEY
EXTRA_ARGS: '-o StrictHostKeyChecking=no'
ENV_VARS: >-
IMAGE=${IMAGE}
VERSION=${VERSION}
DB_HOST=${DB_HOST}
DB_PORT=${DB_PORT}
DB_USER=${DB_USER}
DB_PASS=${DB_PASS}
DB_NAME=${DB_NAME}
APP_PORT=${APP_PORT}