Skip to content

Commit

Permalink
Add support for arm64/amd64 containers
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-marza-mambu committed Apr 22, 2024
1 parent 77d395a commit d71e0e8
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 9 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
env:
GO_VERSION: "1.22.1"
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
IMAGE_NAME: quay.io/${{ secrets.REGISTRY_USERNAME }}/terraform-docs

jobs:
build:
Expand Down Expand Up @@ -85,6 +86,9 @@ jobs:

docker:
runs-on: ubuntu-latest
strategy:
matrix:
architecture: [amd64, arm64]
if: "!contains(github.event.head_commit.message, '[ci skip]')"
steps:
- name: Checkout
Expand All @@ -110,6 +114,18 @@ jobs:
env:
DOCKER_TAG: edge

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build Docker image
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile
load: true
tags: ${{ env.IMAGE_NAME }}:latest-${{ matrix.architecture }}
platforms: ${{ matrix.architecture }}/linux

publish:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[ci skip]')"
Expand Down
47 changes: 39 additions & 8 deletions .github/workflows/prerelease.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
env:
GO_VERSION: "1.22.1"
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
IMAGE_NAME: quay.io/${{ secrets.REGISTRY_USERNAME }}/terraform-docs

jobs:
prerelease:
Expand Down Expand Up @@ -43,16 +44,24 @@ jobs:

docker:
runs-on: ubuntu-latest
needs: prerelease
if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
matrix:
platform: [linux/amd64, linux/arm64]
include:
- platform: linux/amd64
suffix: -amd64
- platform: linux/arm64
suffix: -arm64
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set version output
id: vars
run: echo ::set-output name=tag::${GITHUB_REF:11} # tag name without leading 'v'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker
uses: docker/login-action@v3
Expand All @@ -63,8 +72,30 @@ jobs:
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Build and push Docker image
run: make docker push
env:
DOCKER_TAG: ${{ steps.vars.outputs.tag }}
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile
platforms: ${{ matrix.platform }}
tags: ${{ env.IMAGE_NAME }}:${{ github.ref_name }}${{ matrix.suffix }}
push: true

manifest:
runs-on: ubuntu-latest
needs: docker
if: "!contains(github.event.head_commit.message, '[ci skip]')"
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to Docker
uses: docker/login-action@v3
with:
registry: quay.io
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Create and push Docker manifest
run: |
docker manifest create ${{ env.IMAGE_NAME }}:${{ github.ref_name }} ${{ env.IMAGE_NAME }}:${{ github.ref_name }}-amd64 ${{ env.IMAGE_NAME }}:${{ github.ref_name }}-arm64
docker manifest push ${{ env.IMAGE_NAME }}:${{ github.ref_name }}
15 changes: 15 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
env:
GO_VERSION: "1.22.1"
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
IMAGE_NAME: quay.io/${{ secrets.REGISTRY_USERNAME }}/terraform-docs

jobs:
assets:
Expand Down Expand Up @@ -46,6 +47,20 @@ jobs:
id: vars
run: echo ::set-output name=tag::${GITHUB_REF:11} # tag name without leading 'v'

- name: Build and push Docker images
uses: docker/setup-buildx-action@v2
with:
version: latest

- name: Build and push Docker images
run: |
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.tag }}-amd64 -t ${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.tag }}-arm64 --push .
- name: Create and push Docker manifest
run: |
docker manifest create ${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.tag }} ${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.tag }}-amd64 ${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.tag }}-arm64
docker manifest push ${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.tag }}
- name: Update Chocolatey package
run: ./scripts/release/update-choco.sh "${{ steps.vars.outputs.tag }}"

Expand Down
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@ RUN go mod download
COPY . .
RUN make build

# Detect architecture and prepare a generic output path
RUN ARCH=$(uname -m); \
if [ "$ARCH" = "x86_64" ]; then \
cp /go/src/terraform-docs/bin/linux-amd64/terraform-docs /go/src/terraform-docs/bin/terraform-docs; \
elif [ "$ARCH" = "aarch64" ]; then \
cp /go/src/terraform-docs/bin/linux-arm64/terraform-docs /go/src/terraform-docs/bin/terraform-docs; \
else \
echo "Unsupported architecture"; exit 1; \
fi

################

FROM alpine:3.19.0

# Mitigate CVE-2023-5363
RUN apk add --no-cache --upgrade "openssl>=3.1.4-r1"

COPY --from=builder /go/src/terraform-docs/bin/linux-amd64/terraform-docs /usr/local/bin/
COPY --from=builder /go/src/terraform-docs/bin/terraform-docs /usr/local/bin/

ENTRYPOINT ["terraform-docs"]
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ DEFAULT_TAG ?= $(shell echo "$(CUR_VERSION)" | tr -d 'v')
DOCKER_IMAGE := quay.io/$(PROJECT_OWNER)/$(PROJECT_NAME)
DOCKER_TAG ?= $(DEFAULT_TAG)

# Docker multi-architecture variables
DOCKER_ARCH := $(GOARCH)
ifeq ($(DOCKER_ARCH),amd64)
DOCKER_ARCH_SUFFIX = -amd64
else ifeq ($(DOCKER_ARCH),arm64)
DOCKER_ARCH_SUFFIX = -arm64
else
$(error Unsupported architecture)
endif

# Binary versions
GOLANGCI_VERSION := v1.55.2

Expand Down Expand Up @@ -104,6 +114,26 @@ push: ## Push Docker image
@ $(MAKE) --no-print-directory log-$@
docker push $(DOCKER_IMAGE):$(DOCKER_TAG)

.PHONY: docker-multiarch
docker-multiarch: ## Build Docker image for multiple architectures
@ $(MAKE) --no-print-directory log-$@
docker build --pull --tag $(DOCKER_IMAGE):$(DOCKER_TAG)$(DOCKER_ARCH_SUFFIX) --file Dockerfile .

.PHONY: push-multiarch
push-multiarch: ## Push Docker image with architecture suffix
@ $(MAKE) --no-print-directory log-$@
docker push $(DOCKER_IMAGE):$(DOCKER_TAG)$(DOCKER_ARCH_SUFFIX)

.PHONY: push-multiarch-manifests
push-multiarch-manifests: ## Create and push multiarch manifests
@ $(MAKE) --no-print-directory log-$@
docker pull $(DOCKER_IMAGE):$(DEFAULT_TAG)-amd64
docker pull $(DOCKER_IMAGE):$(DEFAULT_TAG)-arm64
docker manifest create $(DOCKER_IMAGE):$(DEFAULT_TAG) \
$(DOCKER_IMAGE):$(DEFAULT_TAG)-amd64 \
$(DOCKER_IMAGE):$(DEFAULT_TAG)-arm64
docker manifest push --purge $(DOCKER_IMAGE):$(DEFAULT_TAG)

.PHONY: docs
docs: ## Generate document of formatter commands
@ $(MAKE) --no-print-directory log-$@
Expand Down

0 comments on commit d71e0e8

Please sign in to comment.