Skip to content

Commit

Permalink
Merge pull request #152 from rios0rios0/fix/golang-docker
Browse files Browse the repository at this point in the history
fix(golang-docker): changed `docker.yaml` Golang delivery stage to handle image tag name gracefully
  • Loading branch information
rios0rios0 authored Feb 4, 2025
2 parents f185280 + 7f0ef85 commit ed8b994
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 57 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Exceptions are acceptable depending on the circumstances (critical bug fixes tha
- added `go1.23.4.yaml` template to Golang Docker delivery stage
- added another stage's template `acr-container-deployment.yaml`, introduce new test steps' template: `test.yaml` and new test stage: `acr.yaml` to the GoLang pipeline to log in into ACR before running tests
- added `arm-container.yaml` to run container in `azure container instance`
- added Azure global `docker.yaml` delivery template to be used by all languages

### Changed

Expand All @@ -32,6 +33,8 @@ Exceptions are acceptable depending on the circumstances (critical bug fixes tha
- corrected miss-used template files for .NET in Azure DevOps pipelines
- updated the image tag for the Golang Docker delivery stage to retrieve the complete tag name from an environment variable.
- changed `go.yaml` Golang's test stage to use `test.yaml` template
- changed `docker.yaml` Azure's Golang delivery stage to use global `docker.yaml` template and remove unnecessary execution of `./config.sh` script since it's already done by the `go1.23.4.yaml` template
- changed `docker.yaml` Azure's Javascript delivery stage to use global `docker.yaml` template since it was being repeated

### Fixed

Expand Down
48 changes: 48 additions & 0 deletions azure-devops/global/abstracts/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
parameters:
- name: 'DOCKER_FILE'
type: 'string'
- name: 'DOCKER_CACHE_DIR'
type: 'string'
- name: 'DOCKER_REGISTRY_SERVICE_CONNECTION'
type: 'string'
- name : 'CONTAINER_REGISTRY_SERVER'
type: 'string'

steps:
- task: 'Cache@2'
inputs:
key: "$(Agent.JobName)|$(DOCKER_FILE)"
path: "$(DOCKER_CACHE_DIR)"
displayName: 'Cache Docker Buildx'
continueOnError: true

- task: Docker@2
displayName: Docker Login
inputs:
containerRegistry: '$(DOCKER_REGISTRY_SERVICE_CONNECTION)'
command: login

- task: CmdLine@2
displayName: Build and Push Docker Image
inputs:
script: |
imageName="$(CONTAINER_REGISTRY_SERVER)/$(Build.Repository.Name)"
TAGS="$imageName:latest"
if [[ "$(Build.SourceBranch)" == refs/tags/* ]]; then
TAGS="$TAGS -t $imageName:$(Build.SourceBranchName)"
echo "##vso[task.setvariable variable=DOCKER_CONTAINER_TAG;isOutput=true]$(Build.SourceBranchName)"
else
echo "##vso[task.setvariable variable=DOCKER_CONTAINER_TAG;isOutput=true]latest"
fi
echo "image tag: $TAGS"
docker buildx create --use
docker buildx build \
--platform linux/amd64,linux/arm64 \
--tag $TAGS \
--cache-from=type=local,src=$(DOCKER_CACHE_DIR) \
--cache-to=type=local,dest=$(DOCKER_CACHE_DIR),mode=max \
--file ${{ parameters.DOCKER_FILE }} \
--push .
35 changes: 12 additions & 23 deletions azure-devops/golang/stages/40-delivery/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,26 @@ stages:
displayName: Build and push stage
condition: and(not(failed()), or(startsWith(variables['Build.SourceBranch'], 'refs/tags/'), startsWith(variables['Build.SourceBranch'], 'refs/heads/main')))
jobs:

- job: Build
displayName: Build job
variables:
imageName: $(Build.Repository.Name)
GOPATH: "$(Pipeline.Workspace)/.go"
DOCKER_CACHE_DIR: '$(Agent.TempDirectory)/docker-cache'
steps:
- template: '../../abstracts/go1.23.4.yaml'

- task: Docker@2
displayName: Docker Login
inputs:
containerRegistry: '$(DOCKER_REGISTRY_SERVICE_CONNECTION)'
command: login

#TODO: create a template for this
- script: |
set -e
mkdir -p build_output
chmod +x config.sh
./config.sh
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build_output/main ./cmd
CGO_ENABLED=0 GOOS=linux GOARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/') go build -o build_output/main ./cmd
displayName: "Build Go Application"
- task: CmdLine@2
displayName: Build and Push Docker Image
inputs:
script: |
docker run --privileged --rm tonistiigi/binfmt --install arm64
docker run --privileged --rm tonistiigi/binfmt
docker buildx create --use
docker buildx build --platform linux/amd64,linux/arm64 \
-t $(REGISTRY_SERVER)/$(imageName):$(Build.SourceBranchName) \
--push \
--file Dockerfile \
--build-context main=./build_output .
- template: '../../../global/abstracts/docker.yaml'
parameters:
DOCKER_FILE: './Dockerfile'
DOCKER_CACHE_DIR: '$(DOCKER_CACHE_DIR)'
DOCKER_REGISTRY_SERVICE_CONNECTION: '$(DOCKER_REGISTRY_SERVICE_CONNECTION)'
CONTAINER_REGISTRY_SERVER: '$(CONTAINER_REGISTRY_SERVER)'

- template: '../../../global/stages/40-delivery/release.yaml'
40 changes: 6 additions & 34 deletions azure-devops/javascript/stages/40-delivery/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,18 @@ stages:
ARTIFACT_NAME: "$(BUILD_ARTIFACT_NAME)"
ARTIFACT_PATH: "$(BUILD_ARTIFACT_PATH)"

- task: 'Cache@2'
inputs:
key: "$(Agent.JobName)|.ci/40-delivery/app.Dockerfile"
path: "$(DOCKER_CACHE_DIR)"
displayName: 'Cache Docker Buildx'
continueOnError: true

- task: 'DownloadPipelineArtifact@2'
inputs:
artifactName: "$(BUILD_ARTIFACT_NAME)"
targetPath: 'build'
displayName: 'Download Build Artifact'

- task: 'Docker@2'
inputs:
command: 'login'
containerRegistry: "$(DOCKER_REGISTRY_ENDPOINT)"
displayName: 'Docker Login'

- script: |
set -e
docker buildx create --use
TAGS="$(DOCKER_CONTAINER_IMAGE):latest"
if [[ "$(Build.SourceBranch)" == refs/tags/* ]]; then
TAGS="$TAGS -t $(DOCKER_CONTAINER_IMAGE):$(Build.SourceBranchName)"
echo "##vso[task.setvariable variable=DOCKER_CONTAINER_TAG;isOutput=true]$(Build.SourceBranchName)"
else
echo "##vso[task.setvariable variable=DOCKER_CONTAINER_TAG;isOutput=true]latest"
fi
- template: '../../../global/abstracts/docker.yaml'
parameters:
DOCKER_FILE: '.ci/40-delivery/app.Dockerfile'
DOCKER_CACHE_DIR: '$(DOCKER_CACHE_DIR)'
DOCKER_REGISTRY_SERVICE_CONNECTION: '$(DOCKER_REGISTRY_ENDPOINT)'
CONTAINER_REGISTRY_SERVER: '$(CONTAINER_REGISTRY_SERVER)'

docker buildx build \
--platform linux/amd64,linux/arm64 \
--cache-from=type=local,src=$(DOCKER_CACHE_DIR) \
--cache-to=type=local,dest=$(DOCKER_CACHE_DIR),mode=max \
--file .ci/40-delivery/app.Dockerfile \
--tag $TAGS \
--push .
name: 'build'
displayName: 'Docker Build and Push'
continueOnError: false
- template: '../../../global/stages/40-delivery/release.yaml'

0 comments on commit ed8b994

Please sign in to comment.