How to build and deploy jar in same step in bitbucket-pipelines.yml file?

Viewed 40

I have a bitbucket-pipelines.yml file actually steps are working good. But what I want to do is deploy the backend jar after build is finished. But as you see build image and ubuntu image are in different steps and I cannot integrate them together.

# This is an example Starter pipeline configuration
pipelines:
  default:
    
    - step:
        name: 'Frontend Build'
        image: node:16.4.2
        script:
          - cd frontend
          - npm install
    - step:
        name: 'Backend Build and Package'
        image: maven:3.8.3-openjdk-17
        script:
          - cd backend
          - mvn clean package
    - step:
        name:  'Deploy'
        image: ubuntu:16.04
        script:
          - apt-get update
          - apt-get install curl -y
          - apt-get install sshpass -y
          - sshpass -f "sshkey.pub" scp backend/target/backend-0.0.1-SNAPSHOT.jar root@164.65.55.278:/root/artifacts2

If I put build and deploy steps togetger as follows:

- step:
        name:  'Deploy'
        image: ubuntu:16.04
        script:
          - apt-get update
          - apt install maven
          - apt install -y openjdk-17-jdk
          - cd backend
          - mvn clean package
          - apt-get install curl -y
          - apt-get install sshpass -y
          - sshpass -f "sshkey.pub" scp backend/target/backend-0.0.1-SNAPSHOT.jar root@167.71.54.246:/root/artifacts2

Then I get storage error:

          Need to get 65.5 MB of archives.
After this operation, 175 MB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.

If I don't put deploy in same step with build then it cannot find the jar file... How can I achieve this in different steps ?

1 Answers
  1. The message you refer to as 'storage error' looks like a prompt from apt install maven - it is missing the -y option which stands for automatic 'yes' to all prompts (and you're using it correctly in the steps below).

  2. Downloading missing packages dynamically in the pipeline step with a package manager is an acceptable solution, though it slows down the step execution. To speed things up, you can create your custom build environment image and host it either publicly or privately. You can tailor this image for your needs, so that it contains all the dependencies needed for the step pre-installed. Check out this page for details.

  3. Finally, if possible, I would advise to not combine the build and deploy steps into a single one. Build step should produce an immutable artefact, which is then deployed to multiple environments - and you'll have a guarantee that it's the exact same artefact. Also, it is quite handy for rollbacks - re-deploys will be much faster (no time wasted on building the artefact), and again you'll be confident that you're rolling back to the exact version of the artefact required.

Related