Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 566
Madara's Ghost Avatar asked Nov 24 '17 08:11

Madara's Ghost


People also ask

Does GitLab work with Azure?

all tiers. self-managed. You can enable the Microsoft Azure OAuth 2.0 OmniAuth provider and sign in to GitLab with your Microsoft Azure credentials.

What are build artifacts GitLab?

Artifacts are files created as part of a build process that often contain metadata about that build's jobs like test results, security scans, etc. These can be used for reports that are displayed directly in GitLab or can be published to GitLab Pages or in some other way for users to review.


1 Answers

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.

like image 180
Danny Avatar answered Oct 17 '22 22:10

Danny