CI/CD Integration
Automate building and pushing container images in your CI/CD pipelines.
8 min read
GitHub Actions
Create .github/workflows/docker.yml:
name: Build and Push Docker Image
on:
push:
branches: [main]
release:
types: [published]
env:
REGISTRY: registry.nugethosting.com
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: api
password: ${{ secrets.NUGETHOSTING_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=sha
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }} Important: Add NUGETHOSTING_TOKEN to your repository secrets (Settings → Secrets → Actions).
GitLab CI
Create .gitlab-ci.yml:
variables:
REGISTRY: registry.nugethosting.com
IMAGE_NAME: $CI_PROJECT_PATH
stages:
- build
build-image:
stage: build
image: docker:24
services:
- docker:24-dind
before_script:
- echo "$NUGETHOSTING_TOKEN" | docker login $REGISTRY -u api --password-stdin
script:
- docker build -t $REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHA .
- docker push $REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHA
- |
if [ "$CI_COMMIT_BRANCH" == "main" ]; then
docker tag $REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHA $REGISTRY/$IMAGE_NAME:latest
docker push $REGISTRY/$IMAGE_NAME:latest
fi
only:
- main
- tagsAzure DevOps
Create azure-pipelines.yml:
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
registry: 'registry.nugethosting.com'
imageName: 'your-username/your-app'
steps:
- task: Docker@2
displayName: 'Login to Registry'
inputs:
command: login
containerRegistry: 'NugetHostingRegistry'
- task: Docker@2
displayName: 'Build and Push'
inputs:
command: buildAndPush
repository: $(imageName)
dockerfile: '**/Dockerfile'
containerRegistry: 'NugetHostingRegistry'
tags: |
$(Build.BuildId)
latestNote: Create a service connection named "NugetHostingRegistry" in Azure DevOps with your registry credentials.
Best Practices
Use semantic versioning for tags
Tag releases with version numbers like v1.2.3.
Include commit SHA in tags
Enables tracing images back to specific commits.
Use multi-stage builds
Reduces final image size and improves security.
Scan images for vulnerabilities
Add image scanning to your pipeline before pushing.
Never hardcode tokens in pipelines
Always use secret variables or vault services.