Automate Bitbucket to DIV Harbor

Requirements:

  • Accounted created at DIV Harbor & Acces to a project repositry
  • Harbor CLI Secret
  • Docker
  • BitBucket Account & Repository
  • custom project with a dockerfile that you want to build and push
Harbor Bitbucket

Bitbucket Automated Linting, Building & Pushing project

bitbucket-pipelines.yml

image: atlassian/default-image:3

pipelines:
  default:
    - parallel:
        - step:
            name: Build and Test
            runs-on:
              - self.hosted
              - linux
            script:
              - IMAGE_NAME=$BITBUCKET_REPO_SLUG
              - docker build . --file Dockerfile --tag ${IMAGE_NAME}
            services:
              - docker
            caches:
              - docker
        - step:
            name: Lint the Dockerfile
            image: hadolint/hadolint:latest-debian
            script:
              - hadolint Dockerfile
  branches:
    master:
      - step:
          name: Build and Test
          runs-on:
              - self.hosted
              - linux
          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
          runs-on:
              - self.hosted
              - linux
          deployment: Production
          script:
            - echo ${HARBOR_PASSWORD} | docker login --username "$HARBOR_USERNAME" --password-stdin harbor.divsphere.net
            - IMAGE_NAME=$BITBUCKET_REPO_SLUG
            - docker load --input "${IMAGE_NAME}.tar"
            - VERSION="prod-0.1.${BITBUCKET_BUILD_NUMBER}"
            - IMAGE=harbor.divsphere.net/${HARBOR_PROJECT}/${IMAGE_NAME}
            - docker tag "${IMAGE_NAME}" "${IMAGE}:${VERSION}"
            - docker push "${IMAGE}:${VERSION}"
          services:
            - docker
  • ${HARBOR_PASSWORD} specifies the CLI Secret set in the repository variables
  • ${HARBOR_PROJECT} specifies the Harbor Project the image repo needs to be created
  • $HARBOR_USERNAME specifies the username of DIV Harbor