Is it possible to transfer caches/artifacts between pipelines? - gitlab

In Gitlab, is it possible to transfer caches or artifacts between pipelines?
I am building a library in one pipeline and I want to build an application with the library in another pipeline.

Yes, it is possible. There are a couple of options to achieve this:
Using Job API and GitLab Premium
The first option is to use Job API to fetch artifacts. This method is available only if you have GitLab Premium. In this option, you use CI_JOB_TOKEN in Job API to fetch artifacts from another pipeline. Read more here.
Here is quick example of a job you would put in your application pipeline configuration:
build_application:
image: debian
stage: build
script:
- apt update && apt install -y unzip
- curl --location --output artifacts.zip "https://gitlab.example.com/api/v4/projects/${PROJECT_ID}/jobs/artifacts/master/download?job=build&job_token=$CI_JOB_TOKEN"
- unzip artifacts.zip
Using S3
The second option is to use some third-party intermediate storage, for instance, AWS S3. To pass artifacts follow below example.
In your library pipeline configuration create the following job:
variables:
TARGET_PROJECT_TOKEN: [get token from Settings -> CI/CD -> Triggers]
TARGET_PROJECT_ID: [get project id from project main page]
publish-artifact:
image: "python:latest"
stage: publish
before_script:
- pip install awscli
script:
- aws s3 cp output/artifact.zip s3://your-s3-bucket-name/artifact.zip.${CI_JOB_ID}
- "curl -X POST -F token=${TARGET_PROJECT_TOKEN} -F ref=master -F variables[ARTIFACT_ID]=${CI_JOB_ID} https://gitlab.com/api/v4/projects/${TARGET_PROJECT_ID}/trigger/pipeline"
Then in your application pipeline configuration retrieve the artifact from the s3 bucket:
fetch-artifact-from-s3:
image: "python:latest"
stage: prepare
artifacts:
paths:
- artifact/
before_script:
- pip install awscli
script:
- mkdir artifact
- aws s3 cp s3://your-s3-bucket-name/artifact.zip.${ARTIFACT_ID} artifact/artifact.zip
only:
variables:
- $ARTIFACT_ID
Once fetch-artifact-from-s3 job is completed you will have your artifact available in artifact/ directory. It can now be consumed in other jobs within application pipeline.

Related

How to access artifacts in next stage in GitLab CI/CD

I am trying to build GitLab CI/CD for the first time. I have two stages build and deploy The job in the build stage produce artifacts. And then the job in deploy stage wants to upload those artifacts to AWS S3. Both the jobs are using same runner but different docker image.
default:
tags:
- dev-runner
stages:
- build
- deploy
build-job:
image: node:14
stage: build
script:
- npm install
- npm run build:prod
artifacts:
paths:
- deploy/build.zip
deploy-job:
image: docker.xx/xx/gitlab-templates/awscli
stage: deploy
script:
- aws s3 cp deploy/build.zip s3://mys3bucket
The build-job is successfully creating the artifacts. GitLab documentation says artifacts will be automatically downloaded and available in the next stage, however it does not specify where & how these artifacts will be available to consume in the next stage.
Question
In the deploy-job will the artifacts available at the same location? like deploy/build.zip
The artifacts should be available to the second job in the same location, where the first job saved them using the 'artifacts' directive.
I think this question already has an answer on the gitlab forum:
https://forum.gitlab.com/t/access-artifact-in-next-task-to-deploy/9295
Maybe you need to make sure the jobs run in the correct order using the dependencies directive, which is also mentioned in the forum discussion accesible via the link above.

Databricks and Gitlab integration/automatization

I would like to create some ci/cd in gitlab. I would like to run update all databricks notebooks in Repos with the most current git code (some developers to not use dbx ui, but IDE such as VScode).
I was able to found azure devops integration, with running some
stages:
- update_dbx_notebooks
update_dbx_notebooks:
stage: update_dbx_notebooks
script: |
-python -m pip install --upgrade databricks-cli
displayName: 'Install dependencies'
script:
-echo "Checking out the $CI_COMMIT_BRANCH branch"
-databricks repos update --path "Repos/databricksUser/SL_dataprovider_staging" --branch "$CI_COMMIT_BRANCH"
I have generated token, so I am able to pull/commit from databricks notebooks against gitlab fine. But I think gitlab runner must authenticate against databricks, right? Makes sense to create VM with gitlabrunner on Azure?
Does anyone have experience with gitlab/github integration?
Create your gitlab-runner on Linux machine, add sudo privilegies (or specific only for your pipeline) to gitlab-runner user
Start runner, register it with your project
Add to home config file with Runner profile ~/.databrickscfg
See databricks cli docs (or you can use default, after manual settings)
Ofcourse you have to verify your gitlab repository against datbricks.
Add pipeline to your gitlab-ci.yml
stages:
- update_dbx_notebooks
update_dbx_notebooks:
stage: update_dbx_notebooks
before_script:
- echo "Installation of databricks-cli via $USER"
- sudo python3 -m pip install databricks-cli
script:
- echo "Test ls dbx space"
- databricks workspace ls /Users/username --profile Runner
- echo "Checking out the $CI_COMMIT_BRANCH branch"
- databricks repos update --path "/Repos/username/staging" --branch "$CI_COMMIT_BRANCH" --profile Runner
voila

Azure Devops install Python package from Azure Artifacts inside Docker

I am trying to install a pip package from Azure Artifacts as part of a Docker image(with Docker#2 task) but whatever I try does not work.
It looks like my pip inside Docker cannot authenticate against Azure Artifacts whatever I try. Closest I got is with
RUN pip install keyring artifacts-keyring
ENV ARTIFACTS_KEYRING_NONINTERACTIVE_MODE true
RUN pip install <> --index-url https://pkgs.dev.azure.com/<>/_packaging/<>/pypi/simple/
but in my Azure devops, i keep getting
ERROR: Could not find a version that satisfies the requirement <> (from versions: none)
ERROR: No matching distribution found for <>
Also - Azure documentation on this seems to very poor, if I switch ENV ARTIFACTS_KEYRING_NONINTERACTIVE_MODE false it prompts my Azure DevOps pipeline to authenticate intercatively which is not what I want.
How can I install a Python package published in Azure Artifacts as part of my Azure Pipeline Docker task automatically?
How can I install a Python package published in Azure Artifacts as part of my Azure Pipeline Docker task automatically?
We could use the PipAuthenticate task to populates the PIP_EXTRA_INDEX_URL environment variable:
It authenticates with your artifacts feed and per the docs, will store
the location of a config file that can be used to connect in the
PYPIRC_PATH environment variable.
Then pass it in the build arg:
arguments: --build-arg INDEX_URL=$(PIP_EXTRA_INDEX_URL)
You could check this document Consuming Azure Pipelines Python artifact feeds in Docker for some more details.
Hope this helps.
To add to the accepted answer, here is a somewhat more complete code example:
azure-pipelines.yml
- task: PipAuthenticate#1
inputs:
artifactFeeds: 'my_artifacts_feed'
# 'onlyAddExtraIndex' populates PIP_EXTRA_INDEX_URL env variable
onlyAddExtraIndex: True
- task: Docker#2
displayName: 'Build Docker Image'
inputs:
command: build
dockerfile: $(dockerfilePath)
tags: |
$(tag)
arguments: --build-arg INDEX_URL=$(PIP_EXTRA_INDEX_URL)
Dockerfile
FROM python:3.8-buster
# add an URL that PIP automatically searches (e.g., Azure Artifact Store URL)
ARG INDEX_URL
ENV PIP_EXTRA_INDEX_URL=$INDEX_URL
COPY requirements.txt
RUN pip install -r requirements.txt

Reuse artifacts from previous pipelines in Bitbucket

I would like to use an artifact from a previous pipeline and checking the documentation I haven't been able to find how.
I've only seen how to reuse them in the same pipeline (https://confluence.atlassian.com/bitbucket/using-artifacts-in-steps-935389074.html)
How can I reuse an existing artifact from a previous pipeline?
This is my current bitbucket-pipelines.yml:
image: php:7.2.18
pipelines:
branches:
delete-me:
- step:
name: Build docker containers
artifacts:
- docker_containers.tar
services:
- docker
script:
- docker/build_containers_if_not_exists.sh
- sleep 30 # wait for docker to start all containers
- docker save $(docker images -q) -o ${BITBUCKET_CLONE_DIR}/docker_containers.tar
- step:
name: Compile styles & js
caches:
- composer
script:
- docker load --input docker_containers.tar
- docker-compose up -d
- composer install
Maybe you can try to use Pipelines Caches feature. You should define your custom cache, for example:
definitions:
caches:
docker_containers: /docker_containers
The cache will be saved after the first successful build and will be available to the next pipelines for the next 7 days. Here is more info about using caches https://confluence.atlassian.com/bitbucket/caching-dependencies-895552876.html

How can one deploy build artifacts from GitLab CI to an Azure app service?

I have a repository hosted on gitlab.com, it has several build jobs associated with it. I would like for an ability to deploy the compiled artifacts of any given build (generally in the form of HTML/CSS/JavaScript compiled files) to azure.
All of the guides/docs/tutorials I've seen so far (1, 2, 3, to name a few), focus on deploying files directly from a git repository, which I can see being useful in some cases, but isn't what I need in this case, as I want the compilation targets, and not the source.
Solutions welcome, we've been bashing our heads over this for several days now.
Alternatives to GitLab in which this is made possible (in case it's not in GitLab), will also be welcomed.
Add a deployment stage that has the build dependencies, from a job or more then a job, and thus downloads the artifacts of those jobs see below .gitlab-ci.yml:
stages:
- build
- ...
- deploy
buildjob:1:
stage: build
script:
- build_to_web_dir.sh
artifacts:
paths:
- web
buildjob:2:
stage: build
script:
- build_to_web_dir.sh
artifacts:
paths:
- web
deploy:
stage: deploy
GIT_STRATEGY: none
image: microsoft/azure-cli
dependencies:
- buildjob:1
- buildjob:2
script:
- export containerName=mynewcontainername
- export storageAccount=mystorageaccount
- az storage blob delete-batch --source ${containerName} --account-name ${storageAccount} --output table
- az storage blob upload-batch --source ./web --destination ${containerName} --account-name ${storageAccount} --output table --no-progress
In the deploy job, only one directory will be in the CI_PROJECT_DIR ./web containing all files that the build jobs have produced.
checkout storage quickstart azure for creating and setting up the storage container, account details etc.
For the deploy stage we can use the microsoft/azure-cli docker image, so we can call from our script the az command, see storage-quickstart-blobs-cli for more detail explanation.
az storage blob upload-batch --source ./web --destination ${containerName} --account-name ${storageAccount} --output blobname --no-progress
will copy ./web to the storage container
we should not export for security reasons in the .gitlab-ci.yml:
export AZURE_STORAGE_ACCOUNT="mystorageaccountname"
export AZURE_STORAGE_ACCESS_KEY="myStorageAccountKey"
but they should be defined in the project_or_group/settings/ci_cd environment variables, so they'll be present in the script environment.

Resources