Skip to content

Commit 640660c

Browse files
committed
feat: create ghcr release workflow
TICKET: VL-3317
1 parent 2b75146 commit 640660c

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
name: Release to GHCR
3+
4+
on:
5+
push:
6+
branches:
7+
- master
8+
9+
permissions:
10+
contents: write # Needed to create new releases
11+
packages: write # Needed to push to GHCR
12+
id-token: write # Needed to create an ephemeral cross-repo token
13+
14+
jobs:
15+
get-context:
16+
name: Generate release context
17+
runs-on: ubuntu-latest
18+
outputs:
19+
new-version: ${{ steps.compute-context.outputs.new-version }}
20+
current-version: ${{ steps.compute-context.outputs.current-version }}
21+
version-changed: ${{ steps.compute-context.outputs.version-changed }}
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
with:
26+
ref: ${{ github.sha }}
27+
fetch-depth: 0 # Fetch all history for git describe to work
28+
29+
- name: Compute the context for this release
30+
id: compute-context
31+
run: |
32+
current_version=$(cat package.json | jq -r .version)
33+
34+
# Check if the version in package.json is using semantic versioning
35+
if [[ "$current_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
36+
echo "Current version is a valid semantic version: $current_version"
37+
else
38+
echo "Current version format is not a standard semantic version: $current_version"
39+
# This repo might be using semantic-release, so we'll get the latest tag instead
40+
latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.1.0")
41+
if [[ -z "$latest_tag" ]]; then
42+
latest_tag="0.1.0"
43+
fi
44+
# Remove 'v' prefix if present
45+
current_version=${latest_tag#v}
46+
echo "Using latest git tag as current version: $current_version"
47+
fi
48+
49+
echo "current-version=$current_version" >> $GITHUB_OUTPUT
50+
51+
# Check if the version in package.json was changed in the last commit
52+
previous_commit=$(git rev-parse HEAD~1)
53+
previous_version=$(git show $previous_commit:package.json 2>/dev/null | jq -r .version || echo "")
54+
55+
echo "Previous version: $previous_version"
56+
echo "Current version: $current_version"
57+
58+
if [ "$current_version" != "$previous_version" ] && [[ "$current_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
59+
echo "Version changed from $previous_version to $current_version in the last commit"
60+
echo "version-changed=true" >> $GITHUB_OUTPUT
61+
echo "new-version=$current_version" >> $GITHUB_OUTPUT
62+
else
63+
echo "Version unchanged or not following semantic versioning format"
64+
echo "version-changed=false" >> $GITHUB_OUTPUT
65+
echo "new-version=$current_version" >> $GITHUB_OUTPUT
66+
fi
67+
68+
create-release:
69+
name: Create GitHub release
70+
needs: get-context
71+
if: ${{ needs.get-context.outputs.version-changed == 'true' }}
72+
runs-on: ubuntu-latest
73+
outputs:
74+
release-id: ${{ steps.create-release.outputs.id }}
75+
release-url: ${{ steps.create-release.outputs.html_url }}
76+
steps:
77+
- name: Create release
78+
id: create-release
79+
uses: actions/github-script@v6
80+
with:
81+
script: |
82+
const release = await github.rest.repos.createRelease({
83+
owner: context.repo.owner,
84+
repo: context.repo.repo,
85+
tag_name: `v${process.env.VERSION}`,
86+
name: `v${process.env.VERSION}`,
87+
body: 'Automated release created by GitHub Actions',
88+
draft: false,
89+
prerelease: false,
90+
generate_release_notes: true
91+
});
92+
return release.data;
93+
env:
94+
VERSION: ${{ needs.get-context.outputs.new-version }}
95+
96+
build-and-push:
97+
name: Build and push image to GHCR
98+
needs: [get-context, create-release]
99+
if: ${{ needs.get-context.outputs.version-changed == 'true' }}
100+
runs-on: ubuntu-latest
101+
steps:
102+
- name: Checkout
103+
uses: actions/checkout@v4
104+
105+
- name: Set up Docker Buildx
106+
uses: docker/setup-buildx-action@v2
107+
108+
- name: Login to GitHub Container Registry
109+
uses: docker/login-action@v2
110+
with:
111+
registry: ghcr.io
112+
username: ${{ github.actor }}
113+
password: ${{ secrets.GITHUB_TOKEN }}
114+
115+
- name: Extract metadata for Docker
116+
id: meta
117+
uses: docker/metadata-action@v4
118+
with:
119+
images: ghcr.io/${{ github.repository }}
120+
tags: |
121+
type=semver,pattern={{version}},value=${{ needs.get-context.outputs.new-version }}
122+
type=semver,pattern={{major}}.{{minor}},value=${{ needs.get-context.outputs.new-version }}
123+
type=semver,pattern={{major}},value=${{ needs.get-context.outputs.new-version }}
124+
type=raw,value=latest
125+
126+
- name: Build and push Docker image
127+
uses: docker/build-push-action@v4
128+
with:
129+
context: .
130+
push: true
131+
tags: ${{ steps.meta.outputs.tags }}
132+
labels: ${{ steps.meta.outputs.labels }}
133+
build-args: |
134+
BUILD_VERSION=${{ needs.get-context.outputs.new-version }}
135+
BUILD_DATE=${{ github.event.repository.updated_at }}
136+
VCS_REF=${{ github.sha }}
137+
cache-from: type=gha
138+
cache-to: type=gha,mode=max

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bitgo/advanced-wallets",
3-
"version": "0.0.0-semantically-released",
3+
"version": "0.0.1",
44
"description": "Advanced Wallets - On-Premises Key Management with BitGo Express",
55
"main": "./dist/src/index.js",
66
"types": "./dist/src/index.d.ts",

0 commit comments

Comments
 (0)