Skip to content

Commit

Permalink
[CLD-8329]Create custom image for atlantis tool to run terraform (#1)
Browse files Browse the repository at this point in the history
* [CLD-8329]Create custom image for atlantis tool to run terraform

* [CLD-8329]Keep atlantis version in makefile only

* [CLD-8329]comment trivy block because there's no image in the registry yet

* [CLD-8329]uncomment trivy block

* [CLD-8329]comment trivy block because there's no image in the registry yet
  • Loading branch information
andrleite authored Sep 13, 2024
1 parent c962e2d commit 53f75d9
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/actions/docker-build/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: docker-build
description: Reusable action to build repos' docker images

runs:
using: composite
steps:
- name: ci/prepare-docker-environment
uses: ./.github/actions/docker-prepare

# - name: cd/scan-docker-security
# uses: aquasecurity/trivy-action@062f2592684a31eb3aa050cc61e7ca1451cecd3d # v0.18.0
# with:
# image-ref: "mattermost/atlantis"
# format: "table"
# ignore-unfixed: true
# vuln-type: "os,library"
# severity: "CRITICAL"

- name: cd/push-image-pr
run: "make push-image-pr"
shell: bash
if: github.event_name == 'pull_request'

- name: cd/push-image
run: "make build"
shell: bash
if: github.event_name != 'pull_request'
11 changes: 11 additions & 0 deletions .github/actions/docker-prepare/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: "docker-prepare"
description: "Install docker requirements"

runs:
using: "composite"
steps:
- name: ci/setup-buildx
uses: docker/setup-buildx-action@0d103c3126aa41d772a8362f6aa67afac040f80c # v3.1.0
with:
version: v0.12.0
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: ci

on:
pull_request:
push:
branches:
- main

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: ci/checkout-repo
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
fetch-depth: 0

- name: ci/build-docker
env:
DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}
uses: ./.github/actions/docker-build
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ARG ATLANTIS_VERSION

FROM ghcr.io/runatlantis/atlantis:${ATLANTIS_VERSION}

USER root

RUN apk --no-cache add \
jq~=1 \
yq~=4 \
kubectl~=1 \
aws-cli \
&& rm -rf /var/cache/apk/*

USER atlantis
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["server"]
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
ATLANTIS_VERSION ?= v0.27.3
MATTERMOST_ATLANTIS_IMAGE ?= mattermost/atlantis:test
PLATFORMS ?= linux/amd64,linux/arm64
MATTERMOST_ATLANTIS_REPO=mattermost/atlantis

build-image:
@if [ -z "$(DOCKER_USERNAME)" ] || [ -z "$(DOCKER_PASSWORD)" ]; then \
echo "DOCKER_USERNAME and/or DOCKER_PASSWORD not set. Skipping Docker login."; \
else \
echo $(DOCKER_PASSWORD) | docker login --username $(DOCKER_USERNAME) --password-stdin; \
fi
docker buildx build \
--build-arg ATLANTIS_VERSION=$(ATLANTIS_VERSION) \
--platform=$(PLATFORMS) \
-t $(MATTERMOST_ATLANTIS_IMAGE) \
--push .

build-image-with-tag:
@if [ -z "$(DOCKER_USERNAME)" ] || [ -z "$(DOCKER_PASSWORD)" ]; then \
echo "DOCKER_USERNAME and/or DOCKER_PASSWORD not set. Skipping Docker login."; \
else \
echo $(DOCKER_PASSWORD) | docker login --username $(DOCKER_USERNAME) --password-stdin; \
fi
docker buildx build \
--build-arg ATLANTIS_VERSION=$(ATLANTIS_VERSION) \
--platform=$(PLATFORMS) \
-t $(MATTERMOST_ATLANTIS_IMAGE) \
-t $(MATTERMOST_ATLANTIS_REPO):${TAG} \
--push .

.PHONY: push-image-pr
push-image-pr:
@echo Push Image PR
./scripts/push-image-pr.sh

.PHONY: push-image
push-image:
@echo Push Image
./scripts/push-image.sh
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# mattermost-atlantis-docker

This repository contains the necessary files to build a custom Docker image for the [Atlantis](https://github.com/runatlantis/atlantis) tool using Docker Buildx. It supports building multi-platform images and pushing them to Docker Hub.

## Prerequisites

Before you begin, ensure you have the following installed:

- [Docker](https://www.docker.com/) with [Buildx](https://docs.docker.com/buildx/working-with-buildx/) enabled
- Make sure you are logged into Docker Hub or have the credentials available for login

## Environment Variables

To push the image to Docker Hub, the following environment variables should be set:

- `DOCKER_USERNAME`: Your Docker Hub username
- `DOCKER_PASSWORD`: Your Docker Hub password or access token

## Variables

The Makefile accepts the following optional variables:

- `ATLANTIS_VERSION`: The version of Atlantis to include in the Docker image. Defaults to `v0.27.3`.
- `MATTERMOST_ATLANTIS_IMAGE`: The name of the Docker image to build. Defaults to `mattermost/atlantis:test`.
- `PLATFORMS`: The platforms for which to build the image. Defaults to `linux/amd64,linux/arm64`.
- `MATTERMOST_ATLANTIS_REPO`: The Docker repository where the image will be pushed. Defaults to `mattermost/atlantis`.

## Makefile Targets

### `build-image`

This command builds the Docker image and pushes it to the Docker repository specified by `MATTERMOST_ATLANTIS_IMAGE`.

```bash
make build-image
5 changes: 5 additions & 0 deletions scripts/push-image-pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -e
set -u

make build-image
12 changes: 12 additions & 0 deletions scripts/push-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -e

if [ -n "${TAG}" ]
then
echo "Pushing ${TAG} for release ..."
else
echo "Pushing latest for ${GITHUB_REF_NAME} ..."
export TAG="latest"
fi

make build-image-with-tag

0 comments on commit 53f75d9

Please sign in to comment.