Skip to content

Commit

Permalink
feat: self-update docker image (#14)
Browse files Browse the repository at this point in the history
* add entrypoint example

* Update entrypoint.sh

* chore: release workflow

* fix: add version check script

* feat: add probe version to ws query

Co-authored-by: Dmitriy Akulov <dakulovgr@gmail.com>
  • Loading branch information
zarianec and jimaek authored Mar 29, 2022
1 parent 1766a9f commit b383161
Show file tree
Hide file tree
Showing 17 changed files with 17,548 additions and 7,251 deletions.
1 change: 1 addition & 0 deletions .commitlintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: [ '@commitlint/config-conventional' ] };
45 changes: 45 additions & 0 deletions .github/workflows/_docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Publish Docker image

on:
workflow_call:
inputs:
tag:
description: Docker image tag
required: true
type: string

jobs:
publish:
name: Publish Docker image
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v3
with:
ref: v${{ inputs.tag }}

- name: Log into registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/${{ github.repository }}
tags: type=semver,pattern={{version}},value=${{ inputs.tag }}

- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and test
name: Run tests

on:
workflow_call: {}
Expand All @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
with:
node-version: 16.x
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ on:

jobs:
test:
uses: ./.github/workflows/_test.yaml
uses: ./.github/workflows/_tests.yaml
69 changes: 42 additions & 27 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
name: Release probe
name: Release

on:
push:
tags:
- 'v*.*.*'
workflow_dispatch: {}

jobs:
test:
uses: ./.github/workflows/_test.yaml
uses: ./.github/workflows/_tests.yaml

release:
name: Release new version

publish:
runs-on: ubuntu-latest
container: node:16-bullseye-slim

needs: test

permissions:
contents: read
packages: write
contents: write
issues: write
pull-requests: write

outputs:
new_release_published: ${{ steps.semantic.outputs.new_release_published }}
new_release_version: ${{ steps.semantic.outputs.new_release_version }}

steps:
- uses: actions/checkout@v2
- name: Install git :)
run: apt update && apt install -y git

- name: Log into registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/${{ github.repository }}
tags: type=semver,pattern={{version}}
- name: Build
run: |
npm install
npm run build
- name: Build and push
uses: docker/build-push-action@v2
- name: Prune dev dependencies
run: npm install --only=prod

- name: Release
id: semantic
uses: cycjimmy/semantic-release-action@v3
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
extra_plugins: |
@semantic-release/git
@semantic-release/exec
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

publish:
needs: release

if: needs.release.outputs.new_release_published == 'true'

uses: ./.github/workflows/_docker.yaml
with:
tag: ${{ needs.release.outputs.new_release_version }}
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run lint
22 changes: 22 additions & 0 deletions .releaserc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
branches: [ 'master' ],
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
[ '@semantic-release/github', {
assets: [
{ 'path': 'globalping-probe.bundle.tar.gz', 'label': 'globalping-probe.bundle.tar.gz' }
]
} ],
[ '@semantic-release/npm', {
npmPublish: false
} ],
[ '@semantic-release/exec', {
prepareCmd: 'tar -czf globalping-probe.bundle.tar.gz dist/ config/ node_modules/ package.json'
} ],
[ '@semantic-release/git', {
assets: [ 'package.json', 'package-lock.json' ],
message: 'chore(release): [skip ci] bump version to ${nextRelease.version}'
} ]
]
};
17 changes: 9 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ FROM node:16-bullseye-slim
ARG node_env=production
ENV NODE_ENV=$node_env

WORKDIR /build

RUN apt update && apt install -y iputils-ping traceroute \
RUN apt update && apt install -y iputils-ping traceroute dnsutils curl jq tini \
&& apt clean && apt autoremove -y \
&& rm -rf /var/lib/{apt,dpkg,cache,log}/

COPY --from=builder /app/dist /build/dist
COPY --from=builder /app/config /build/config
COPY --from=builder /app/package.json /app/package-lock.json /build/
COPY --from=builder /app/dist /app/dist
COPY --from=builder /app/config /app/config
COPY --from=builder /app/package.json /app/package-lock.json /app/
COPY bin/entrypoint.sh /entrypoint.sh

RUN cd /app && npm install --production

RUN npm install --production
ENTRYPOINT ["/usr/bin/tini", "--"]

CMD ["node", "./dist/index.js"]
CMD ["/entrypoint.sh"]
35 changes: 35 additions & 0 deletions bin/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

function run_probe() {
node /app/dist/index.js
return
}

echo "Checking for the latest version"

response=$(curl -XGET -s "https://api.github.com/repos/jsdelivr/globalping-probe/releases/latest")
latestVersion=$(jq -r ".tag_name" <<<"${response}" | sed 's/v//')
latestBundle=$(jq -r ".assets[] .browser_download_url" <<<"${response}")

currentVersion=$(jq -r ".version" "/app/package.json")

echo "Current version $currentVersion"
echo "Latest version $latestVersion"

if [ "$latestVersion" != "$currentVersion" ]; then
loadedTarball="globalping-probe-${latestVersion}"

echo "Start self-update process"

curl -Ls -XGET "${latestBundle}" -o "/tmp/${loadedTarball}.tar.gz"
tar -xzf "/tmp/${loadedTarball}.tar.gz" --one-top-level="/tmp/${loadedTarball}"

rm -rf "/app"
mv "/tmp/${loadedTarball}" "/app"

rm -rf "/tmp/${loadedTarball}.tar.gz"

echo "Self-update finished"
fi

run_probe
5 changes: 5 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"api": {
"host": "ws://api.globalping.io"
},
"update": {
"releaseUrl": "https://api.github.com/repos/jsdelivr/globalping-probe/releases/latest",
"interval": 300,
"maxDeviation": 300
}
}
5 changes: 5 additions & 0 deletions config/development.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"api": {
"host": "ws://localhost:3000"
},
"update": {
"releaseUrl": "https://api.github.com/repos/jsdelivr/globalping-probe/releases/latest",
"interval": 10,
"maxDeviation": 5
}
}
Loading

0 comments on commit b383161

Please sign in to comment.