user465677
user465677

Reputation: 1

Configuring gitlab-ci to push to Azure Container Registry using Docker image

The problem is that when the code is on GitLab, it says, No related tags found. Here is my gitlab-ci.yml file. All of the variables are stored in the GitLab CI/CD variables.

stages:
  - build
  - push
  - deploy

variables:
  TAG: $CI_COMMIT_SHORT_SHA
  PROJECT_NAME: project-name

build:
  stage: build
  image: docker:20.10.16
  services:
    - docker:20.10.16-dind
  tags:
    - docker
  before_script:
    - docker login $ACR_LOGIN_SERVER -u $ACR_USERNAME -p $ACR_PASSWORD
  script:
    - docker build --pull --rm -f "backend/Dockerfile" -t webapi:$TAG "im_v2/backend"
    - docker build --pull --rm -f "frontend/Dockerfile" -t client:$TAG "im_v2/frontend"
    - docker build --pull --rm -f "deploy/gateway/Dockerfile" -t gateway:$TAG "im_v2/deploy/gateway"
  artifacts:
    paths:
      - deploy/production/docker-compose.yml
  only:
    - /^development.*$/
    - /^deployment.*$/

push:
  stage: push
  image: docker:20.10.16
  services:
    - docker:20.10.16-dind
  tags:
    - docker
  script:
    - docker push webapi:$TAG
    - docker push client:$TAG
    - docker push gateway:$TAG
  only:
    - /^development.*$/
    - /^deployment.*$/

deploy:
  stage: deploy
  image: mcr.microsoft.com/azure-cli
  script:
    - az login --service-principal --username $AZURE_CLIENT_ID --password $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID
    - az webapp config container set --name $AZURE_APP_SERVICE_NAME --resource-group $AZURE_RESOURCE_GROUP --multicontainer-config-type compose --multicontainer-config-file deploy/production/docker-compose.yml
  when: manual
  environment:
    name: production
    url: https://$AZURE_APP_SERVICE_NAME.azurewebsites.net
  only:
    - /^development.*$/
    - /^deployment.*$/

I am just trying to create CI/CD pipeline and make it work.

I tried different ways, but none of them worked.

Upvotes: 0

Views: 288

Answers (1)

Sampath
Sampath

Reputation: 3478

I have done few changes in the yml at login and build stage .

I followed this documentation and so link to build and push container images to a container registry in GitLab.Thanks @yamenk for the explanation.

image: docker:20.10.16

services:
  - docker:20.10.16-dind

variables:
  DOCKER_HOST: tcp://docker:2376
  DOCKER_TLS_CERTDIR: "/certs"
  CONTAINER_IMAGE: $ACR_LOGIN_SERVER/samples/nginx:$CI_COMMIT_REF_SLUG

stages:
  - login
  - build
  - push

login:
  stage: login
  script:
    - apk add --no-cache curl jq
    - az login --service-principal -u $AZURE_SERVICE_PRINCIPAL_ID -p $AZURE_SERVICE_PRINCIPAL_PASSWORD --tenant $AZURE_TENANT_ID
    - az acr login --name $ACR_NAME

build:
  stage: build
  script:
    - docker pull nginx
    - docker tag nginx $CONTAINER_IMAGE
    - docker build -t $CONTAINER_IMAGE .
    - docker images

push:
  stage: push
  script:
    - echo "Pushing image to ACR..."
    - docker push $CONTAINER_IMAGE
  only:
    - main
# optional
cleanup:
  stage: cleanup
  script:
    - docker rmi $CONTAINER_IMAGE
    - az acr repository delete --name $ACR_NAME --image samples/nginx:latest
  when: manual


If you are using Azure Container Registry with a username and password, you can log in to Registry as shown below:

docker login $ACR_LOGIN_SERVER -u $ACR_USERNAME -p $ACR_PASSWORD

Output:

enter image description here

enter image description here

Upvotes: 0

Related Questions