Skip to content

Commit

Permalink
Add CI to build container on push
Browse files Browse the repository at this point in the history
  • Loading branch information
lannonbr committed Aug 4, 2024
1 parent 20879c4 commit 58a437f
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/actions/prune-containers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { Octokit } = require("@octokit/rest");
const dayjs = require("dayjs");

async function run() {
const today = dayjs();

const client = new Octokit({
auth: process.env.GITHUB_TOKEN,
});

const versions =
await client.packages.getAllPackageVersionsForPackageOwnedByAuthenticatedUser(
{
package_type: "container",
package_name: "pkmn-tcg-app",
}
);

let versionsRemoved = 0;

for (const version of versions.data) {
// delete untagged versions that are older than 7 days
if (
today.diff(dayjs(version.created_at), "days") > 7 &&
version.metadata.container.tags.length === 0
) {
await client.packages.deletePackageVersionForAuthenticatedUser({
package_type: "container",
package_name: "pkmn-tcg-app",
package_version_id: version.id,
});
versionsRemoved++;
}
}

console.log(`Image versions pruned: ${versionsRemoved}`);
}

run();
173 changes: 173 additions & 0 deletions .github/actions/prune-containers/package-lock.json

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

16 changes: 16 additions & 0 deletions .github/actions/prune-containers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "prune-containers",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@octokit/rest": "^21.0.1",
"dayjs": "^1.11.12"
}
}
39 changes: 39 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
on:
push:
branches:
- "main"
name: Docker
jobs:
build:
name: Build Container
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/lannonbr/pokemon-tcg-app:latest
- name: Prune old images
run: |
cd .github/actions/prune-containers
npm install
node index.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit 58a437f

Please sign in to comment.