Skip to content

Commit

Permalink
feat(release): package the release in the DevPod format
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Jun 18, 2023
1 parent bd19409 commit 6dba4d8
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 98 deletions.
90 changes: 29 additions & 61 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on:
push:
branches:
- main
tags:
- "v*.*.*"
pull_request:
branches:
- main
Expand Down Expand Up @@ -76,55 +78,15 @@ jobs:

- run: go version

- name: Install Go dependencies
run: go install ./...

- name: Lint Go
uses: golangci/golangci-lint-action@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get branch names
id: branch-name
uses: tj-actions/branch-names@v6
with:
strip_tag_prefix: v

- name: Generate Docker tag
id: docker
- name: Generate version info
id: version
run: |
if [ "${{ steps.branch-name.outputs.is_tag }}" = "true" ];
then
# Latest tag
IMG_NAME="ghcr.io/${GITHUB_REPOSITORY,,}:latest"
# Tag name (usually vX.Y.Z)
IMG_NAME="${IMG_NAME},ghcr.io/${GITHUB_REPOSITORY,,}:${{ steps.branch-name.outputs.tag }}"
echo "image_name=${IMG_NAME}" >> "$GITHUB_OUTPUT"
echo "platforms=linux/amd64,linux/arm64,linux/arm/v7" >> "$GITHUB_OUTPUT"
else
# Use branch naming convention
TAG="branch-${{ steps.branch-name.outputs.current_branch }}"
# Change "/" for "-"
TAG="${TAG//\//-}"
# Set to lowercase
TAG="${TAG,,}"
echo "image_name=ghcr.io/${GITHUB_REPOSITORY,,}:${TAG}" >> "$GITHUB_OUTPUT"
echo "platforms=linux/amd64" >> "$GITHUB_OUTPUT"
fi
if [ "${{ steps.branch-name.outputs.is_tag }}" = "true" ];
then
echo "version=${{ steps.branch-name.outputs.tag }}" >> "$GITHUB_OUTPUT"
Expand All @@ -135,24 +97,30 @@ jobs:
echo "commit_id=${GITHUB_SHA}" >> "$GITHUB_OUTPUT"
echo "gitRepo=github.com/${GITHUB_REPOSITORY}" >> "$GITHUB_OUTPUT"
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
build-args: |
GIT_COMMIT=${{ steps.docker.outputs.commit_id }}
GIT_REPO=${{ steps.docker.outputs.gitRepo }}
VERSION=${{ steps.docker.outputs.version }}
platforms: ${{ steps.docker.outputs.platforms }}
push: ${{ github.ref == 'refs/heads/main' }}
tags: ${{ steps.docker.outputs.image_name }}

- name: Run GoReleaser
- name: Compile binaries
run: ./hack/build.sh
env:
GIT_COMMIT: ${{ steps.version.outputs.commit_id }}
GIT_REPO: ${{ steps.version.outputs.gitRepo }}
RELEASE_VERSION: ${{ steps.version.outputs.version }}

- uses: actions/setup-node@v3
if: steps.branch-name.outputs.is_tag == 'true'

- name: Changelog
if: steps.branch-name.outputs.is_tag == 'true'
uses: goreleaser/goreleaser-action@v4
run:
npm install -g conventional-changelog-cli

conventional-changelog -o CHANGELOG.md

tail -n +2 CHANGELOG.md > CHANGELOG.tmp

mv CHANGELOG.tmp CHANGELOG.md

- name: Release
if: steps.branch-name.outputs.is_tag == 'true'
uses: softprops/action-gh-release@v1
with:
version: latest
args: release --clean
env:
GIT_REPO: ${{ steps.docker.outputs.gitRepo }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
body_path: CHANGELOG.md
files: dist/*
37 changes: 0 additions & 37 deletions .goreleaser.yaml

This file was deleted.

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<!-- toc -->

* [Usage](#usage)
* [Open in Gitpod](#open-in-gitpod)
* [Open in a container](#open-in-a-container)

Expand All @@ -12,6 +13,17 @@
[DevPod](https://devpod.sh/) on [Hetzner](https://hetzner.cloud/?ref=UWVUhEZNkm6p).
This is based upon the [DigitalOcean provider](https://github.com/loft-sh/devpod-provider-digitalocean).

## Usage

To use this provider in your DevPod setup, you will need to do the following steps:

1. See the [DevPod documentation](https://devpod.sh/docs/managing-providers/add-provider)
for how to add a provider
1. Use the reference `mrsimonemms/devpod-provider-hetzner` to download the latest
release from GitHub
1. Get an [API token](https://docs.hetzner.com/cloud/api/getting-started/generating-api-token/)
from Hetzner. This will be used to manage resources.

## Open in Gitpod

* [Open in Gitpod](https://gitpod.io/from-referrer/)
Expand Down
Binary file added assets/hetzner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions hack/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash
set -e

export GO111MODULE=on
# export GOFLAGS=-mod=vendor

PROVIDER_ROOT=$(git rev-parse --show-toplevel)
COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null)
DATE=$(date "+%Y-%m-%d")
BUILD_PLATFORM=$(uname -a | awk '{print tolower($1);}')

echo "Current working directory is $(pwd)"
echo "PATH is $PATH"
echo "GOPATH is $GOPATH"

if [[ "$(pwd)" != "${PROVIDER_ROOT}" ]]; then
echo "you are not in the root of the repo" 1>&2
echo "please cd to ${PROVIDER_ROOT} before running this script" 1>&2
exit 1
fi

GO_BUILD_CMD="go build"
GO_BUILD_LDFLAGS="-w -s -X ${GIT_REPO}/cmd.Version=${RELEASE_VERSION} -X ${GIT_REPO}/cmd.GitCommit=${GIT_COMMIT}"

if [[ -z "${PROVIDER_BUILD_PLATFORMS}" ]]; then
PROVIDER_BUILD_PLATFORMS="linux windows darwin"
fi

if [[ -z "${PROVIDER_BUILD_ARCHS}" ]]; then
PROVIDER_BUILD_ARCHS="amd64 arm64"
fi

# Create the dist directory
mkdir -p "${PROVIDER_ROOT}/dist"

for OS in ${PROVIDER_BUILD_PLATFORMS[@]}; do
for ARCH in ${PROVIDER_BUILD_ARCHS[@]}; do
NAME="devpod-provider-hetzner-${OS}-${ARCH}"
if [[ "${OS}" == "windows" ]]; then
NAME="${NAME}.exe"
fi

# darwin 386 is deprecated and shouldn't be used anymore
if [[ "${ARCH}" == "386" && "${OS}" == "darwin" ]]; then
echo "Building for ${OS}/${ARCH} not supported."
continue
fi

# arm64 build is only supported for darwin
if [[ "${ARCH}" == "arm64" && "${OS}" == "windows" ]]; then
echo "Building for ${OS}/${ARCH} not supported."
continue
fi

echo "Building for ${OS}/${ARCH}"
GOARCH=${ARCH} GOOS=${OS} ${GO_BUILD_CMD} -ldflags "${GO_BUILD_LDFLAGS}"\
-o "${PROVIDER_ROOT}/dist/${NAME}" main.go
shasum -a 256 "${PROVIDER_ROOT}/dist/${NAME}" | cut -d ' ' -f 1 > "${PROVIDER_ROOT}/dist/${NAME}".sha256
done
done

# generate provider.yaml
go run "${PROVIDER_ROOT}/hack/provider/main.go" ${RELEASE_VERSION} > "${PROVIDER_ROOT}/dist/provider.yaml"
62 changes: 62 additions & 0 deletions hack/provider/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"strings"
)

var checksumMap = map[string]string{
"./dist/devpod-provider-hetzner-linux-amd64": "##CHECKSUM_LINUX_AMD64##",
"./dist/devpod-provider-hetzner-linux-arm64": "##CHECKSUM_LINUX_ARM64##",
"./dist/devpod-provider-hetzner-darwin-amd64": "##CHECKSUM_DARWIN_AMD64##",
"./dist/devpod-provider-hetzner-darwin-arm64": "##CHECKSUM_DARWIN_ARM64##",
"./dist/devpod-provider-hetzner-windows-amd64.exe": "##CHECKSUM_WINDOWS_AMD64##",
}

func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Expected version as argument")
os.Exit(1)
return
}

content, err := os.ReadFile("./hack/provider/provider.yaml")
if err != nil {
panic(err)
}

replaced := strings.Replace(string(content), "##VERSION##", os.Args[1], -1)
for k, v := range checksumMap {
checksum, err := File(k)
if err != nil {
panic(fmt.Errorf("generate checksum for %s: %v", k, err))
}

replaced = strings.Replace(replaced, v, checksum, -1)
}

fmt.Print(replaced)
}

// File hashes a given file to a sha256 string
func File(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer func() {
err = file.Close()
}()

hash := sha256.New()
_, err = io.Copy(hash, file)
if err != nil {
return "", err
}

return strings.ToLower(hex.EncodeToString(hash.Sum(nil))), err
}
Loading

0 comments on commit 6dba4d8

Please sign in to comment.