From 6fd8acefa53763116525a02104366ca352046265 Mon Sep 17 00:00:00 2001 From: dathan Date: Thu, 8 Jun 2023 16:02:01 -0700 Subject: [PATCH] feat: update template to support ghcr, tag builds, updated Dockerfile --- .github/workflows/dockerpublish.yml | 68 +++++++++---------- Dockerfile | 2 +- Makefile | 41 ++++++----- cmd/example2/main.go | 7 -- cmd/{example1 => go-project-template}/main.go | 0 scripts/entrypoint.sh | 5 +- 6 files changed, 59 insertions(+), 64 deletions(-) delete mode 100644 cmd/example2/main.go rename cmd/{example1 => go-project-template}/main.go (100%) diff --git a/.github/workflows/dockerpublish.yml b/.github/workflows/dockerpublish.yml index a06a599..b659ed0 100644 --- a/.github/workflows/dockerpublish.yml +++ b/.github/workflows/dockerpublish.yml @@ -13,44 +13,40 @@ on: # Run tests for any PRs. pull_request: +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + jobs: - # Push image to GitHub Packages. - # See also https://docs.docker.com/docker-hub/builds/ - build: - runs-on: ubuntu-latest - if: github.event_name == 'push' - steps: - #needed to access private repos with ssh - # - uses: shimataro/ssh-key-action@v2.0.1 - # with: - # key: ${{ secrets.SSH_PRIV_KEY }} - # known_hosts: ${{ secrets.KNOWN_HOSTS }} - # name: id_rsa # optional - - - uses: actions/checkout@v2 - - - name: Build Container - run: make docker-build - - publish: + build_and_publish: runs-on: ubuntu-latest - if: startsWith(github.ref, 'refs/tags/v') steps: - #needed to access private repos with ssh - # - uses: shimataro/ssh-key-action@v2.0.1 - # with: - # key: ${{ secrets.SSH_PRIV_KEY }} - # known_hosts: ${{ secrets.KNOWN_HOSTS }} - # name: id_rsa # optional - - - uses: actions/checkout@v2 - - - name: Log into registry - #note the registry doesn't allow github_token a PAT is needed. So, you will need to create that post install - run: echo "${{ secrets.GITHUB_TOKEN }}" |docker login docker.pkg.github.com --username publisher --password-stdin - - - name: Publish Build - run: make docker-push - + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.CR_PAT }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=tag + type=sha,prefix=commit- + + + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index 5935c81..40ea390 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,7 +42,7 @@ LABEL stage=release COPY --from=alpineCerts /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=baseGo /root/gocode/bin /app COPY --from=baseGo /root/gocode/scripts /app -ENTRYPOINT ["/app/entrypoint.sh"] +ENTRYPOINT ["/app/go-project-template"] # diff --git a/Makefile b/Makefile index 448536b..8d0aed8 100644 --- a/Makefile +++ b/Makefile @@ -1,60 +1,67 @@ # Go parameters BINARY_NAME=go-project-template BINARY_UNIX=$(BINARY_NAME)_unix -REPO=docker.pkg.github.com/dathan/go-project-template/go-project-template +REPO=ghcr.io/dathan/go-project-template/go-project-template .PHONY: all all: lint test build .PHONY: lint lint: - golangci-lint run ./... + golangci-lint run ./... + +.PHONY: buildruntest +buildruntest: build build-linux customrun + +.PHONY: customrun +customrun: + ./scripts/customrun.sh .PHONY: build build: - go build -o ./bin ./cmd/... + go build -o ./bin ./cmd/... .PHONY: test test: - go test -p 6 -covermode=count -coverprofile=test/coverage.out test/*.go + go test -p 6 -covermode=count -coverprofile=test/coverage.out test/*.go .PHONY: clean clean: - go clean - find . -type d -name '.tmp_*' -prune -exec rm -rvf {} \; + go clean + find . -type d -name '.tmp_*' -prune -exec rm -rvf {} \; .PHONY: run run: - go run ./cmd/$(BINARY_NAME)/*.go + go run ./cmd/$(BINARY_NAME)/*.go .PHONY: vendor vendor: - go mod vendor + go mod vendor # Cross compilation .PHONY: build-linux build-linux: - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/$(BINARY_UNIX) -v cmd/$(BINARY_NAME)/ + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/$(BINARY_UNIX) -v cmd/$(BINARY_NAME)/*.go # Build docker containers .PHONY: docker-build docker-build: - # docker build \ - # --build-arg GITHUB_SSH_PRIV_KEY="`cat ~/.ssh/id_rsa`" \ - # -t $(or ${dockerImage},$(BINARY_NAME)-release) . - docker build \ - -t $(or ${dockerImage},$(BINARY_NAME)-release) . + # docker build \ + # --build-arg GITHUB_SSH_PRIV_KEY="`cat ~/.ssh/id_rsa`" \ + # -t $(or ${dockerImage},$(BINARY_NAME)-release) . + docker build \ + -t $(or ${dockerImage},$(BINARY_NAME)-release) . .PHONY: docker-tag docker-tag: - docker tag `docker image ls --filter 'reference=$(BINARY_NAME)-release' -q` $(REPO):`git rev-parse HEAD` + docker tag `docker image ls --filter 'reference=$(BINARY_NAME)-release' -q` $(REPO):`git rev-parse HEAD` # Push the container .PHONY: docker-push docker-push: docker-build docker-tag - docker push $(REPO):`git rev-parse HEAD` + docker push $(REPO):`git rev-parse HEAD` .PHONY: docker-clean docker-clean: - docker rmi `docker image ls --filter 'reference=$(BINARY_NAME)-*' -q` + docker rmi `docker image ls --filter 'reference=$(BINARY_NAME)-*' -q` diff --git a/cmd/example2/main.go b/cmd/example2/main.go deleted file mode 100644 index 4f5081d..0000000 --- a/cmd/example2/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("hello world2") -} diff --git a/cmd/example1/main.go b/cmd/go-project-template/main.go similarity index 100% rename from cmd/example1/main.go rename to cmd/go-project-template/main.go diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 20dae27..9c97881 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,5 +1,4 @@ -#!/bin/bash +#!/bin/sh echo "Executing a script designed to run from within Docker" -/app/example1 -/app/example2 \ No newline at end of file +/app/go-project-template