Skip to content

Commit

Permalink
feat: update template to support ghcr, tag builds, updated Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dathan committed Jun 8, 2023
1 parent ec611d4 commit 6fd8ace
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 64 deletions.
68 changes: 32 additions & 36 deletions .github/workflows/dockerpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


#
Expand Down
41 changes: 24 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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`
7 changes: 0 additions & 7 deletions cmd/example2/main.go

This file was deleted.

File renamed without changes.
5 changes: 2 additions & 3 deletions scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash
#!/bin/sh

echo "Executing a script designed to run from within Docker"
/app/example1
/app/example2
/app/go-project-template

0 comments on commit 6fd8ace

Please sign in to comment.