Skip to content

Commit 3ca4d5b

Browse files
authored
chore: add release workflow (#6)
1 parent e8451aa commit 3ca4d5b

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

.github/workflows/release.yaml

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: release
2+
on:
3+
push:
4+
tags:
5+
- "v*"
6+
7+
permissions:
8+
# Required to publish a release
9+
contents: write
10+
# Necessary to push docker images to ghcr.io.
11+
packages: write
12+
# Necessary for GCP authentication (https://github.com/google-github-actions/setup-gcloud#usage)
13+
id-token: write
14+
15+
concurrency: ${{ github.workflow }}-${{ github.ref }}
16+
17+
jobs:
18+
release:
19+
name: Build and publish
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v3
23+
24+
- name: Echo Go Cache Paths
25+
id: go-cache-paths
26+
run: |
27+
echo "GOCACHE=$(go env GOCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
28+
echo "GOMODCACHE=$(go env GOMODCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
29+
30+
- name: Go Build Cache
31+
uses: actions/cache@v3
32+
with:
33+
path: ${{ steps.go-cache-paths.outputs.GOCACHE }}
34+
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.**', '**.go') }}
35+
36+
- uses: actions/setup-go@v3
37+
with:
38+
go-version: "~1.20"
39+
40+
- name: Get Version
41+
run: echo "version=$(./scripts/version.sh)" >> $GITHUB_OUTPUT
42+
id: version
43+
44+
- name: Build
45+
run: ./scripts/build.sh
46+
47+
- name: Docker Login
48+
uses: docker/login-action@v2
49+
with:
50+
registry: ghcr.io
51+
username: ${{ github.actor }}
52+
password: ${{ secrets.GITHUB_TOKEN }}
53+
54+
- name: Push Image
55+
run: |
56+
VERSION=$(./scripts/version.sh)
57+
BASE=ghcr.io/coder/coder-xray
58+
IMAGE=$BASE:$VERSION
59+
docker tag coder-xray:latest $IMAGE
60+
docker tag coder-xray:latest $BASE:latest
61+
docker push $IMAGE
62+
docker push $BASE:latest
63+
64+
- name: Authenticate to Google Cloud
65+
uses: google-github-actions/auth@v1
66+
with:
67+
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_ID_PROVIDER }}
68+
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
69+
70+
- name: Setup GCloud SDK
71+
uses: "google-github-actions/setup-gcloud@v1"
72+
73+
- name: Publish Helm Chart
74+
run: |
75+
set -euo pipefail
76+
version="$(./scripts/version.sh)"
77+
./scripts/helm.sh --version $version
78+
79+
mkdir -p build/helm
80+
cp "build/${version}.tgz" build/helm
81+
gsutil cp gs://helm.coder.com/coder-xray/index.yaml build/helm/index.yaml
82+
helm repo index build/helm --url https://helm.coder.com/coder-xray --merge build/helm/index.yaml
83+
gsutil -h "Cache-Control:no-cache,max-age=0" cp build/helm/${version}.tgz gs://helm.coder.com/coder-xray
84+
gsutil -h "Cache-Control:no-cache,max-age=0" cp build/helm/index.yaml gs://helm.coder.com/coder-xray
85+
86+
- name: Create Release
87+
uses: actions/create-release@v1
88+
id: create_release
89+
env:
90+
GITHUB_TOKEN: ${{ github.token }}
91+
with:
92+
release_name: ${{ steps.version.outputs.version }}
93+
tag_name: ${{ github.ref }}
94+
95+
- name: Upload Helm Release Asset
96+
uses: actions/upload-release-asset@v1
97+
env:
98+
GITHUB_TOKEN: ${{ github.token }}
99+
with:
100+
upload_url: ${{ steps.create_release.outputs.upload_url }}
101+
asset_path: build/${{ steps.version.outputs.version }}.tgz
102+
asset_name: helm.tar.gz
103+
asset_content_type: application/gzip

scripts/Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM scratch
2+
3+
COPY ./coder-xray /coder-xray
4+
5+
ENTRYPOINT ["/coder-xray", "scan"]

scripts/build.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
cd $(dirname "${BASH_SOURCE[0]}")
4+
set -euxo pipefail
5+
6+
CGO_ENABLED=0 go build -ldflags "-s -w" -o ./coder-xray ../
7+
docker build -t coder-xray:latest

scripts/helm.sh

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env bash
2+
3+
# This script creates a Helm package for the given version. It will output a
4+
# .tgz file at the specified path, and may optionally push it to the Coder OSS
5+
# repo.
6+
#
7+
# ./helm.sh [--version 1.2.3] [--output path/to/coder.tgz] [--push]
8+
#
9+
# If no version is specified, defaults to the version from ./version.sh.
10+
#
11+
# If no output path is specified, defaults to
12+
# "$repo_root/build/coder_xray_helm_$version.tgz".
13+
#
14+
# If the --push parameter is specified, the resulting artifact will be published
15+
# to the Coder OSS repo. This requires `gsutil` to be installed and configured.
16+
17+
set -euo pipefail
18+
cd $(dirname $(dirname "${BASH_SOURCE[0]}"))
19+
20+
log() {
21+
echo "$*" 1>&2
22+
}
23+
24+
version=""
25+
output_path=""
26+
push=0
27+
28+
args="$(getopt -o "" -l version:,output:,push -- "$@")"
29+
eval set -- "$args"
30+
while true; do
31+
case "$1" in
32+
--version)
33+
version="$2"
34+
shift 2
35+
;;
36+
--output)
37+
output_path="$(realpath "$2")"
38+
shift 2
39+
;;
40+
--push)
41+
push="1"
42+
shift
43+
;;
44+
--)
45+
shift
46+
break
47+
;;
48+
*)
49+
error "Unrecognized option: $1"
50+
;;
51+
esac
52+
done
53+
54+
version="${version}"
55+
if [[ "$version" == "" ]]; then
56+
version="$(./scripts/version.sh)"
57+
fi
58+
59+
if [[ "$output_path" == "" ]]; then
60+
mkdir -p build
61+
output_path="$(realpath "build/$version.tgz")"
62+
fi
63+
64+
# Make a destination temporary directory, as you cannot fully control the output
65+
# path of `helm package` except for the directory name :/
66+
temp_dir="$(mktemp -d)"
67+
68+
cd ./
69+
log "--- Packaging helm chart for version $version ($output_path)"
70+
helm package \
71+
--version "$version" \
72+
--app-version "$version" \
73+
--destination "$temp_dir" \
74+
. 1>&2
75+
76+
log "Moving helm chart to $output_path"
77+
cp "$temp_dir"/*.tgz "$output_path"
78+
rm -rf "$temp_dir"
79+
80+
if [[ "$push" == 1 ]]; then
81+
log "--- Publishing helm chart..."
82+
# TODO: figure out how/where we want to publish the helm chart
83+
fi

scripts/version.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
cd $(dirname "${BASH_SOURCE[0]}")
5+
6+
last_tag="$(git describe --tags --abbrev=0)"
7+
version="$last_tag"
8+
9+
echo "${version}"

0 commit comments

Comments
 (0)