Skip to content

Commit

Permalink
Move ls, version, and commit to own files. Update ci.yaml
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Wilde <ncwilde43@gmail.com>
  • Loading branch information
nicholaswilde committed May 18, 2021
1 parent 215f3c9 commit cf64924
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 93 deletions.
218 changes: 165 additions & 53 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,99 +13,211 @@ name: ci
# or API.
on:
workflow_dispatch:
# Inputs the workflow accepts.
inputs:
version:
# Friendly description to be shown in the UI instead of 'name'
description: 'Version'
# Default value if no value is explicitly provided
default: '941c0d3'
# Input has to be provided for the workflow to run
required: true
ls:
description: 'ls'
default: 'ls1'
required: true
pull_request:
paths:
- 'Dockerfile'
- 'LS'
- 'VERSION'
push:
branches:
- main
paths:
- 'Dockerfile'
- 'LS'
- 'VERSION'

env:
# How long to sleep before running the tests (gives the application time to start)
GOSS_SLEEP: 30

jobs:
main:
prep:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.prep.outputs.version }}
commit: ${{ steps.prep.outputs.commit }}
ls: ${{ steps.prep.outputs.ls }}
goss: ${{ steps.prep.outputs.goss }}
push: ${{ steps.prep.outputs.push }}
tag: ${{ steps.prep.outputs.version }}-ls${{ steps.prep.outputs.ls }}
repo_name: ${{ steps.prep.outputs.repo_name }}
date: ${{ steps.prep.outputs.date }}

steps:
-
name: Get tag
run: echo "tag=${{ github.event.inputs.version }}-${{ github.event.inputs.ls }}" >> $GITHUB_ENV
-
name: Remove docker from the repo name
run: echo "repo_name=$(echo "${{ github.event.repository.name }}" | sed 's/[^-]*-//')" >> $GITHUB_ENV
-
name: Get current date
run: echo "date=$(date -u +%Y-%m-%dT%H%M%SZ)" >> $GITHUB_ENV
-
name: Checkout
- name: Checkout
uses: actions/checkout@v2.3.4
-
name: Set up QEMU

# Define if tests and push should be run against which versions/platforms
- name: Prepare
id: prep
run: |
VERSION=$(cat ./VERSION)
echo ::set-output name=version::${VERSION}
LS=$(cat ./LS)
echo ::set-output name=ls::${LS}
REPO_NAME=$(echo "${{ github.event.repository.name }}" | sed 's/[^-]*-//')
echo ::set-output name=repo_name::${REPO_NAME}
DATE=$(date -u +%Y-%m-%dT%H%M%SZ)
echo ::set-output name=date::${DATE}
if test -f "./COMMIT"; then
COMMIT=$(cat ./COMMIT)
echo ::set-output name=commit::${COMMIT}
else
echo ::set-output name=commit::""
fi
if test -f "./goss.yaml"; then
echo ::set-output name=goss::true
else
echo ::set-output name=goss::false
fi
if [ "${{github.event_name}}" == "pull_request" ]; then
echo ::set-output name=push::false
else
echo ::set-output name=push::true
fi
tag-does-not-exist:
runs-on: ubuntu-latest
needs: prep
outputs:
exists: ${{ steps.checkTag.outputs.exists }}
steps:
- name: Check if tag already exists
uses: mukunku/tag-exists-action@v1.0.0
id: checkTag
with:
tag: ${{ needs.prep.outputs.tag }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Fail if tag already exists
id: set
run: |
if ${{ steps.checkTag.outputs.exists }} == true; then
echo "${{needs.prep.outputs.tag}} already exists"
exit 1
fi
build:
runs-on: ubuntu-latest
if: always() # Run regardless if tag-does-not-exist fails
needs:
- prep
- tag-does-not-exist
steps:
- name: Checkout
uses: actions/checkout@v2.3.4

- name: Set up QEMU
uses: docker/setup-qemu-action@v1.1.0
-
name: Set up Docker Buildx

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1.3.0
with:
driver-opts: image=moby/buildkit:master
-
name: Cache Docker layers

- name: Cache Docker layers
uses: actions/cache@v2.1.5
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
-
name: Login to DockerHub
# Install the GOSS testing framework
- name: Set up goss/dgoss
uses: e1himself/goss-installation-action@v1.0.3
if: needs.prep.outputs.goss == 'true'
with:
version: 'v0.3.16'

# Creates a local build to run tests on
- name: Build and Load local test-container
uses: docker/build-push-action@v2
if: needs.prep.outputs.goss == 'true'
with:
build-args: |
VERSION=${{ needs.prep.outputs.version }}
COMMIT=${{ needs.prep.outputs.commit }}
context: .
file: ./Dockerfile
load: true
tags: |
ghcr.io/${{ github.repository_owner }}/${{needs.prep.outputs.repo_name}}:test
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

# Run GOSS tests if included with the container
- name: Run GOSS tests
if: needs.prep.outputs.goss == 'true'
env:
GOSS_FILE: ./goss.yaml
run: |
dgoss run ghcr.io/${{ github.repository_owner }}/${{needs.prep.outputs.repo_name}}:test
- name: Login to DockerHub
uses: docker/login-action@v1.9.0
if: github.event_name != 'pull_request' && needs.tag-does-not-exist.outputs.exists == 'false'
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Login to GitHub Container Registry

- name: Login to GitHub Container Registry
uses: docker/login-action@v1.9.0
if: github.event_name != 'pull_request' && needs.tag-does-not-exist.outputs.exists == 'false'
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.CR_PAT }}
-
name: Login to Quay Registry

- name: Login to Quay Registry
uses: docker/login-action@v1.9.0
if: github.event_name != 'pull_request' && needs.tag-does-not-exist.outputs.exists == 'false'
with:
registry: quay.io
username: ${{ secrets.QUAY_USERNAME }}
password: ${{ secrets.QUAY_TOKEN }}
-
name: Build and push

- name: Build and push
uses: docker/build-push-action@v2.4.0
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm/v7,linux/arm64
push: true
push: ${{ needs.prep.outputs.push }}
build-args: |
BUILD_DATE=${{ env.date }}
VERSION=${{ github.event.inputs.version }}
BUILD_DATE=${{ needs.prep.outputs.date }}
VERSION=${{ needs.prep.outputs.version }}
COMMIT=${{ needs.prep.outputs.commit }}
tags: |
${{ github.repository_owner }}/${{ env.repo_name }}:latest
${{ github.repository_owner }}/${{ env.repo_name }}:${{ env.tag }}
ghcr.io/${{ github.repository_owner }}/${{ env.repo_name }}:latest
ghcr.io/${{ github.repository_owner }}/${{ env.repo_name }}:${{ env.tag }}
quay.io/${{ github.repository_owner }}/${{ env.repo_name }}:latest
quay.io/${{ github.repository_owner }}/${{ env.repo_name }}:${{ env.tag }}
-
name: Create Release
${{ github.repository_owner }}/${{needs.prep.outputs.repo_name}}:latest
${{ github.repository_owner }}/${{needs.prep.outputs.repo_name}}:${{needs.prep.outputs.tag}}
ghcr.io/${{ github.repository_owner }}/${{needs.prep.outputs.repo_name}}:latest
ghcr.io/${{ github.repository_owner }}/${{needs.prep.outputs.repo_name}}:${{needs.prep.outputs.tag}}
quay.io/${{ github.repository_owner }}/${{needs.prep.outputs.repo_name}}:latest
quay.io/${{ github.repository_owner }}/${{needs.prep.outputs.repo_name}}:${{needs.prep.outputs.tag}}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

# This ugly bit is necessary if you don't want your cache to grow forever
# till it hits GitHub's limit of 5GB.
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
if: needs.prep.outputs.push == 'true'
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Create Release
id: create_release
uses: actions/create-release@v1.1.4
if: github.event_name != 'pull_request' && needs.tag-does-not-exist.outputs.exists == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ env.tag }}
release_name: ${{ env.tag }}
tag_name: ${{needs.prep.outputs.tag}}
release_name: ${{needs.prep.outputs.tag}}
draft: false
prerelease: false

38 changes: 13 additions & 25 deletions .taskfiles/chk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@
version: '3'

tasks:
_release:
cmds:
- task: _dl
vars:
URL: "https://github.com/{{ .SOURCE_ORG }}/{{ .SOURCE_REPO }}/releases/download/{{ .VERSION }}/{{ .FILENAME }}"
status:
- test {{ .TYPE }} != "release"

_source:
print:
desc: Print the latest commit
silent: true
cmds:
- task: _dl
vars:
URL: "https://github.com/{{ .SOURCE_ORG }}/{{ .SOURCE_REPO }}/archive/{{ .VERSION }}.tar.gz"
status:
- test {{ .TYPE }} != "source"
- test {{ .TYPE }} != "commit"
- printf %s {{ .COMMIT }}
vars:
COMMIT:
sh: curl -sX GET {{ .API_URL }} | jq --raw-output '. | .sha'

_varcheck:
export:
desc: Export the latest commit to COMMIT
cmds:
- cmd: >
(test {{ .TYPE }} = "source" ||
test {{ .TYPE }} = "release" ||
test {{ .TYPE }} = "commit") ||
(echo "TYPE is not 'source','commit', or 'release'"; exit 1)
silent: true

_dl:
cmds: ["wget {{ .URL }} -O- | sha256sum"]
- printf "%s" "{{ .COMMIT }}" > COMMIT
vars:
VERSION:
sh: curl -sX GET {{ .API_URL }} | jq --raw-output '. | .sha'
17 changes: 17 additions & 0 deletions .taskfiles/goss.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
version: '3'

tasks:
run:
desc: Run dgoss
cmds:
- "{{ .DGOSS_RUN }} {{ .TAG_VERSION }}"
preconditions:
- docker images -q {{ .TAG_VERSION }} 2> /dev/null

edit:
desc: Edit dgoss
cmds:
- "{{ .DGOSS_EDIT }} {{ .TAG_VERSION }}"
preconditions:
- docker images -q {{ .TAG_VERSION }} 2> /dev/null
18 changes: 18 additions & 0 deletions .taskfiles/ls.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
version: '3'

tasks:
print:
desc: Print the LS value
silent: true
cmds:
- printf "%s" "{{ .LS }}"

increment:
desc: Increment the LS value
silent: true
cmds:
- |
LS=$(( {{ .LS }} + 1 ))
printf "%s" "$LS" > LS
cat LS
20 changes: 20 additions & 0 deletions .taskfiles/version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
version: '3'

tasks:
print:
desc: Print the latest app version
silent: true
cmds:
- printf %s {{ .VERSION }}
vars:
VERSION:
sh: curl -sX GET {{ .API_URL }} | jq --raw-output '. | .sha' | head -c7

export:
desc: Export the latest app version to VERSION
cmds:
- printf "%s" "{{ .VERSION }}" > VERSION
vars:
VERSION:
sh: curl -sX GET {{ .API_URL }} | jq --raw-output '. | .sha' | head -c7
1 change: 1 addition & 0 deletions COMMIT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
941c0d3a20b88384f303af6311fc33ede9d831b4
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM golang:1.14.15-alpine3.13 as build
ARG VERSION
ARG COMMIT=941c0d3a20b88384f303af6311fc33ede9d831b4
ARG COMMIT
ENV GO111MODULE on
WORKDIR /go/src/github.com/prologic/todo
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
RUN \
echo "**** install packages ****" && \
apk add --no-cache \
git=2.30.1-r0 \
git=2.30.2-r0 \
make=4.3-r0 \
build-base=0.5-r2 && \
echo "**** download todo ****" && \
Expand All @@ -27,6 +27,7 @@ FROM ghcr.io/linuxserver/baseimage-alpine:3.13
ARG BUILD_DATE
ARG VERSION
ENV GOPATH /go
# hadolint ignore=DL3048
LABEL build_version="Version:- ${VERSION} Build-date:- ${BUILD_DATE}"
LABEL maintainer="nicholaswilde"
COPY --from=build --chown=abc:abc /go /go
Expand Down
1 change: 1 addition & 0 deletions LS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Loading

0 comments on commit cf64924

Please sign in to comment.