From 1d2149269152033e737a960b783579e9cb909435 Mon Sep 17 00:00:00 2001 From: Justin Wagner Date: Sun, 11 May 2025 19:37:08 -0600 Subject: [PATCH] Initial setup of CI workflow --- .github/release.yml | 16 ++++ .github/workflows/docker-build-publish.yaml | 84 +++++++++++++++++++++ .markdownlint.yaml | 24 ++++++ Dockerfile | 6 ++ README.md | 3 +- makefile | 41 ++++++++++ 6 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 .github/release.yml create mode 100644 .github/workflows/docker-build-publish.yaml create mode 100644 .markdownlint.yaml create mode 100644 Dockerfile create mode 100644 makefile diff --git a/.github/release.yml b/.github/release.yml new file mode 100644 index 0000000..b697af9 --- /dev/null +++ b/.github/release.yml @@ -0,0 +1,16 @@ +# .github/release.yml + +changelog: + exclude: + labels: + - ignore-for-release + categories: + - title: Breaking Changes 🛠️ + labels: + - breaking-change + - title: Exciting New Features 🎉 + labels: + - enhancement + - title: Other Changes + labels: + - "*" \ No newline at end of file diff --git a/.github/workflows/docker-build-publish.yaml b/.github/workflows/docker-build-publish.yaml new file mode 100644 index 0000000..083618f --- /dev/null +++ b/.github/workflows/docker-build-publish.yaml @@ -0,0 +1,84 @@ +name: Docker Build and Publish + +on: + push: + branches: ["main"] + # Publish semver tags as releases. + tags: ["v*.*.*"] + pull_request: + branches: ["main"] + +# Only allow a single running job for each PR/tag/branch +# and cancel any running jobs if a new one is created on top of it +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + lint-build-publish: + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + attestations: write + id-token: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Lint + run: make lint + + - name: Log in to the Container registry + uses: docker/login-action@v3.4.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5.7.0 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + id: push + uses: docker/build-push-action@v6.16.0 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + - name: Export release tag + if: github.event_name == 'push' && startsWith( github.ref, 'refs/tags/' ) + run: echo "RELEASE_TAG=${GITHUB_REF_NAME}" >> $GITHUB_ENV + + - name: Create release + if: github.event_name == 'push' && startsWith( github.ref, 'refs/tags/' ) + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + try { + const response = await github.rest.repos.createRelease({ + draft: false, + generate_release_notes: true, + name: process.env.RELEASE_TAG, + owner: context.repo.owner, + prerelease: false, + repo: context.repo.repo, + tag_name: process.env.RELEASE_TAG, + }); + core.exportVariable('RELEASE_ID', response.data.id); + core.exportVariable('RELEASE_UPLOAD_URL', response.data.upload_url); + } catch (error) { + core.setFailed(error.message); + } diff --git a/.markdownlint.yaml b/.markdownlint.yaml new file mode 100644 index 0000000..7b4d8aa --- /dev/null +++ b/.markdownlint.yaml @@ -0,0 +1,24 @@ +# MD013/line-length - Line length +MD013: + # Number of characters + line_length: 120 + # Number of characters for headings + heading_line_length: 80 + # Number of characters for code blocks + code_block_line_length: 100 + # Include code blocks + code_blocks: true + # Include tables + tables: false + # Include headings + headings: true + # Include headings + headers: true + # Strict length checking + strict: false + # Stern length checking + stern: false + +# MD041/first-line-heading/first-line-h1 First line in a file should be a top-level +# This complains about our github badges +MD041: false diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8258789 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +# This is a very simple Dockerfile, it's contents aren't important, as this repo is mainly for showing how to setup the +# CI for docker image publishing repos + +FROM scratch + +COPY ./README.md /README.md \ No newline at end of file diff --git a/README.md b/README.md index d66ebba..c2ef8e3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # docker-image-example-template -Template repository for building a docker image, including all of the GitHub actions to verify and publish the image. + +Template repository for building a docker image including all of the GitHub actions to verify and publish the image. diff --git a/makefile b/makefile new file mode 100644 index 0000000..ca90727 --- /dev/null +++ b/makefile @@ -0,0 +1,41 @@ + +# These variables are used for controlling how the image gets tagged when it's built, and they can be overridden when +# the call to the make command is made +IMAGE_NAME = ghcr.io/jrwagz/docker-image-example-template + +# Here we default to an image tag that makes it obvious that it was a local build, and that it isn't coming from CI +IMAGE_TAG:=$(shell whoami)-$(shell git describe --always)-dirty + +# These variables control what images and tags are used for the various linting tasks +MD_LINT_IMAGE:=ghcr.io/igorshubovych/markdownlint-cli:v0.44.0 +DOCKERFILE_LINT_IMAGE:=ghcr.io/hadolint/hadolint:v2.12.0 +DIVE_IMAGE:=ghcr.io/wagoodman/dive:v0.13.1 + +.PHONY: build +build: + docker build -t $(IMAGE_NAME):$(IMAGE_TAG) . + docker run --rm -it \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -v "$(PWD)":"$(PWD)" \ + -w "$(PWD)" \ + -e CI=true \ + $(DIVE_IMAGE) $(IMAGE_NAME):$(IMAGE_TAG) + @echo SUCCESS $(FULL_NAME):$(IMAGE_TAG) is built and has been scanned by dive + +MD_FILES:=$(shell find . -name "*.md") +.PHONY: lint_markdown +lint_markdown: + docker run --rm \ + -v "${PWD}":"${PWD}" \ + -w "${PWD}" \ + $(MD_LINT_IMAGE) $(MD_FILES) + +.PHONY: lint_dockerfile +lint_dockerfile: + docker run --rm -i $(DOCKERFILE_LINT_IMAGE) < Dockerfile + +# Aliases +.PHONY: lint +lint: lint_dockerfile lint_markdown +.PHONY: ready +ready: lint build \ No newline at end of file