How do I get the URL of a file inside of my runner as a variable

Hey,

I am incredibly new to GitLab CI / CD, and I have only basic knowledge of how the system works. Currently I am compiling my Java application using Maven and it is working correctly, and we have our own deployment system currently in-place - I just need a way to pass along the jar file output from the runners cache into our own deployment system. Optimally, this would be through a URL that contains the final .jar file. How would I achieve this?

stages:                          
  - build                
  - deploy      


before_script:
  - apt-get update -qq 
                                 
maven-build:                     
  image: maven:3-jdk-11
  stage: build                   
  script: "mvn clean package -B"       
  artifacts:                     
    paths:                       
      - public-api-1.0-SNAPSHOT.jar

deploy:
  image: ubuntu:bionic
  stage: deploy
  script:
    - // EXECUTE CUSTOM DEPLOYMENT SYSTEM PASSING THROUGH DIRECT URL FROM COMPILATION

Hi @Swofty

According to your config there, when you get to // EXECUTE CUSTOM DEPLOYMENT... you already have the public-api-1.0-SNAPSHOT.jar file available in the root of your repo.

It will be passed along the pipeline stages automatically, because you’ve specified that it’s an artifact.

This is slightly different from caching, we we would normally use for keeping your maven dependencies and speeding up build times.

Oh alright, thanks. But how do I achieve what I had asked in my post?

Right, I’m not sure I’ve fully understood the question!

What I do, is I have a GitLab Runner on the deployment machine. I tag the runner and the deploy job with the same tag (to ensure that deploy never runs on any other server / VM / …). Then in deploy:script I just copy the steps I would take to manually deploy the app.

If you want to run the deploy job somewhere else, and send the .jar file over the air to your deployment environment, I guess you could just do that with a call to curl or similar, but TBH that seems like overkill to me.

I’m not sure whether that answers your question or not though?

If you want to run the deploy job somewhere else, and send the .jar file over the air to your deployment environment, I guess you could just do that with a call to curl or similar, but TBH that seems like overkill to me.

This is precisely what I want to do. The jar in question is the server backend / minigame frontend for a video game, and our deployment environment is not something that can be re-created inside of GitLab. What URL would I curl if my group is “atlas-network”, my project is “web/public-api”, and my file is inside of “target/file.jar”. Also note that the repository is private.

Right, so this is something that I can’t really answer, because it requires much more knowledge of your systems.

You will need some sort of application running on the deployment VM/server/etc. which will receive that Jar file and do something with it.

For that reason, ISTM that it’s easier to have a runner in that environment, but maybe you have some other constraints that make that difficult.

I can handle what happens after the deployment environment gets the JAR. I was just wondering if there was a way to get the JAR from the runners artifacts as a URL.

So, you could use something like this or you might be able to use the GitLab API.

Either way you will need to authenticate with your instance of GitLab, maybe using a deploy token.

That works, is there any variable for the job ID so that I don’t have to change it every time?

1 Like

Yep, it’s $CI_JOB_ID – and you can find a full list of pre-defined variables here.

1 Like