Skip to content

Commit

Permalink
[wip] feat: Add highscore cleanup job
Browse files Browse the repository at this point in the history
Adds cleanup job for no-longer-needed highscores in rust.
Similar to the one in fsharp that was removed in 03f5eb6
  • Loading branch information
Christian Fosli committed Jul 13, 2023
1 parent 18183c9 commit d14c6a1
Show file tree
Hide file tree
Showing 17 changed files with 397 additions and 51 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/highscore_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Build with docker
- name: Prepare docker compose file(s)
run: |
tags="-t ghcr.io/christianfosli/snake/highscore-api:$(git rev-parse --short HEAD)"
printf 'Deleting docker-compose.override.yml (it is only used during development)\n'
rm docker-compose.override.yml
printf "Adding commit sha as build tag\n"
COMMIT_SHA="$(git rev-parse --short HEAD)" yq -i \
'.services.api.build.tags[0] = strenv(COMMIT_SHA)' docker-compose.yml
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
printf 'Banch is main! Tagging with latest\n'
tags="$tags -t ghcr.io/christianfosli/snake/highscore-api:latest"
printf 'Banch is main! Adding latest as build tag\n'
yq -i '.services.api.build.tags[1] = "latest"' docker-compose.yml
fi
docker build -f highscore-api/Dockerfile $tags .
printf '\n\n'
yq '.services.api' docker-compose.yml
- name: Build with docker compose
run: docker compose build api
env:
DOCKER_BUILDKIT: 1

Expand Down
92 changes: 92 additions & 0 deletions .github/workflows/highscore_cleanup_job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: highscore_cleanup_job

on:
push:
branches: [main]
paths:
- highscore-cleanup-job/**
- .github/workflows/highscore_cleanup_job.yaml
pull_request:
branches: [main]
paths:
- highscore-cleanup-job/**
- .github/workflows/highscore_cleanup_job.yaml

permissions:
# required for federated credentials to access azure
id-token: write
# required for federated credentials to access azure
contents: read
# required to push to ghcr
packages: write

jobs:
build:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Prepare docker compose file(s)
run: |
printf 'Deleting docker-compose.override.yml (it is only used during development)\n'
rm docker-compose.override.yml
printf "Adding commit sha as build tag\n"
COMMIT_SHA="$(git rev-parse --short HEAD)" yq -i \
'.services.cleanup-job.build.tags[0] = strenv(COMMIT_SHA)' docker-compose.yml
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
printf 'Banch is main! Adding latest as build tag\n'
yq -i '.services.cleanup-job.build.tags[1] = "latest"' docker-compose.yml
fi
printf '\n\n'
yq '.services.cleanup-job' docker-compose.yml
- name: Build with docker compose
run: docker compose build api
env:
DOCKER_BUILDKIT: 1

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

- name: Push
run: docker push --all-tags ghcr.io/christianfosli/snake/highscore-cleanup-job

deploy_staging:
needs: build
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- uses: azure/login@v1
with:
client-id: ${{ secrets.ARM_CLIENT_ID }}
subscription-id: ${{ secrets.ARM_SUBSCRIPTION_ID }}
tenant-id: ${{ secrets.ARM_TENANT_ID }}

- run: |
az containerapp update -n caj-snakehighscorecleanup-staging -g rg-snake-staging \
--image "ghcr.io/christianfosli/snake/highscore-cleanup-job:$(git rev-parse --short $HEAD)"
deploy_prod:
if: github.event_name != 'pull_request'
needs: deploy_staging
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- uses: azure/login@v1
with:
client-id: ${{ secrets.ARM_CLIENT_ID }}
subscription-id: ${{ secrets.ARM_SUBSCRIPTION_ID }}
tenant-id: ${{ secrets.ARM_TENANT_ID }}

- run: |
az containerapp update -n caj-snakehighscorecleanup-prod -g rg-snake-prod \
--image "ghcr.io/christianfosli/snake/highscore-cleanup-job:$(git rev-parse --short $HEAD)"
47 changes: 34 additions & 13 deletions .github/workflows/wasm_app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,48 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Set variables for staging
- name: Prepare docker compose file(s) - PR
if: github.event_name == 'pull_request'
run: |
echo "BUILD_VERSION=pr_build_$(git rev-parse --short HEAD)" >> $GITHUB_ENV
echo "HIGHSCORE_API_BASE_URL=https://highscores-staging.playsnake.no" >> $GITHUB_ENV
printf 'Deleting docker-compose.override.yml (it is only used for development)\n'
rm docker-compose.override.yml
- name: Set variables for prod
printf 'Adding build args and tags docker-compose.yml\n'
COMMIT_SHA="$(git rev-parse --short HEAD)" \
HIGHSCORE_API_BASE_URL=https://highscores-staging.playsnake.no \
yq -i '
.services.app.build.tags[0] = strenv(COMMIT_SHA) |
.services.app.build.args.VERSION = strenv(COMMIT_SHA) |
.services.app.build.args.HIGHSCORE_API_BASE_URL= strenv(HIGHSCORE_API_BASE_URL)
' docker-compose.yml
printf '\n\n'
yq '.services.app' docker-compose.yml
- name: Prepare docker compose file(s) - Prod
if: github.event_name != 'pull_request'
run: |
printf 'Deleting docker-compose.override.yml (it is only used for development)\n'
rm docker-compose.override.yml
printf 'Adding build args and tags to docker-compose.yml\n'
COMMIT_SHA="$(git rev-parse --short HEAD)" \
HIGHSCORE_API_BASE_URL=https://highscores.playsnake.no \
yq -i '
.services.app.build.tags[0] = strenv(COMMIT_SHA) |
.serviecs.app.build.tags[1] = "latest" |
.services.app.build.args.VERSION = strenv(COMMIT_SHA) |
.services.app.build.args.HIGHSCORE_API_BASE_URL= strenv(HIGHSCORE_API_BASE_URL)
' docker-compose.yml
printf '\n\n'
yq '.services.app' docker-compose.yml
echo "BUILD_VERSION=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
echo "HIGHSCORE_API_BASE_URL=https://highscores.playsnake.no" >> $GITHUB_ENV
- name: Docker build
run: |
docker build \
--build-arg VERSION=$BUILD_VERSION \
--build-arg HIGHSCORE_API_BASE_URL=$HIGHSCORE_API_BASE_URL \
-f front-end/Dockerfile \
-t ghcr.io/christianfosli/snake/wasm-app:$BUILD_VERSION \
-t ghcr.io/christianfosli/snake/wasm-app:latest \
.
run: docker compose build

- name: Login to ghcr
if: github.event_name != 'pull_request'
Expand All @@ -76,7 +97,7 @@ jobs:
- name: Extract files to deploy
# Since we deploy to azure static web app rather than container we need to extract the build files to deploy
run: |
container="$(docker create ghcr.io/christianfosli/snake/wasm-app:latest)"
container="$(docker create ghcr.io/christianfosli/snake/wasm-app:$(git rev-parse --short HEAD))"
docker cp "$container:/usr/share/nginx/html" out
- name: Deploy
Expand Down
54 changes: 36 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
members = [
"front-end",
"highscore-api",
"highscore-types"
"highscore-types",
"highscore-cleanup-job"
]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
Snake with optional vim navigation.
Visit [playsnake.no](https://www.playsnake.no) to play!

![wasm\_app](https://github.com/christianfosli/visnake-wasm/workflows/wasm_app/badge.svg)
![highscore\_api](https://github.com/christianfosli/visnake-wasm/workflows/highscore_api/badge.svg)
![wasm\_app](https://github.com/christianfosli/snake/workflows/wasm_app/badge.svg)
![highscore\_api](https://github.com/christianfosli/snake/workflows/highscore_api/badge.svg)
![highscore\_cleanup\_job](https://github.com/christianfosli/snake/workflows/highscore_cleanup_job/badge.svg)
![terraform](https://github.com/christianfosli/snake/actions/workflows/terraform.yml/badge.svg)

## Architecture 🏗
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
app:
build:
args:
BUILD_PROFILE: dev
api:
build:
args:
CARGO_INSTALL_OPTIONS: --debug
cleanup-job:
build:
args:
CARGO_INSTALL_OPTIONS: --debug

Loading

0 comments on commit d14c6a1

Please sign in to comment.