Skip to content

Commit

Permalink
refactor: ci code to be from kcl
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Jan 10, 2025
1 parent 2d7a8f0 commit afda625
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 119 deletions.
73 changes: 0 additions & 73 deletions .github/workflows/build.yml

This file was deleted.

54 changes: 54 additions & 0 deletions .github/workflows/docker-production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Deploy docker Production
'on':
push:
tags:
- '*'
- '!*-*'
workflow_dispatch: {}
jobs:
job:
name: Deploy docker Production
runs-on: ubuntu-22.04
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Docker login
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login --username darkwind8 --password-stdin
- name: Install Darklab ssh key
run: |
mkdir ~/.ssh | true
echo "${{ secrets.SSH_KEY_BASE64 }}" | base64 --decode > ~/.ssh/id_rsa
echo "Host darkbot" > ~/.ssh/config
echo " HostName 37.27.207.42" >> ~/.ssh/config
echo " User root" >> ~/.ssh/config
echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config
echo " IdentitiesOnly yes" >> ~/.ssh/config
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
ssh-keyscan -H 37.27.207.42 >> ~/.ssh/known_hosts | true
ssh-keyscan -H darkbot >> ~/.ssh/known_hosts | true
- name: Get version from tag
run: |
set -x
echo "BUILD_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
id: version
- name: Build image darkwind8/darkbot
run: |
set -ex
export tag_version=${{ steps.version.outputs.BUILD_VERSION }}
docker build --build-arg "BUILD_VERSION=$tag_version" --tag darkwind8/darkbot:$tag_version .
docker tag darkwind8/darkbot:$tag_version darkwind8/darkbot:production
docker push darkwind8/darkbot:$tag_version
sleep 5
docker push darkwind8/darkbot:production
sleep 5
- name: Switch service image darkwind8/darkbot for swarm to new one
run: |
set -ex
docker pull darkwind8/darkbot:production
sleep 5
docker service update --image darkwind8/darkbot:production darkbot-production
env:
DOCKER_HOST: ssh://root@darkbot
53 changes: 53 additions & 0 deletions .github/workflows/docker-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Deploy docker staging
'on':
push:
tags:
- '*-*'
workflow_dispatch: {}
jobs:
job:
name: Deploy docker staging
runs-on: ubuntu-22.04
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Get version from tag
run: |
set -x
echo "BUILD_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
id: version
- name: Docker login
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login --username darkwind8 --password-stdin
- name: Install Darklab ssh key
run: |
mkdir ~/.ssh | true
echo "${{ secrets.SSH_KEY_BASE64 }}" | base64 --decode > ~/.ssh/id_rsa
echo "Host darkbot" > ~/.ssh/config
echo " HostName 37.27.207.42" >> ~/.ssh/config
echo " User root" >> ~/.ssh/config
echo " IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config
echo " IdentitiesOnly yes" >> ~/.ssh/config
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
ssh-keyscan -H 37.27.207.42 >> ~/.ssh/known_hosts | true
ssh-keyscan -H darkbot >> ~/.ssh/known_hosts | true
- name: Build image darkwind8/darkbot
run: |
set -ex
export tag_version=${{ steps.version.outputs.BUILD_VERSION }}
docker build --build-arg "BUILD_VERSION=$tag_version" --tag darkwind8/darkbot:$tag_version .
docker tag darkwind8/darkbot:$tag_version darkwind8/darkbot:staging
docker push darkwind8/darkbot:$tag_version
sleep 5
docker push darkwind8/darkbot:staging
sleep 5
- name: Switch service image darkwind8/darkbot for swarm to new one
run: |
set -ex
docker pull darkwind8/darkbot:staging
sleep 5
docker service update --image darkwind8/darkbot:staging darkbot-staging
env:
DOCKER_HOST: ssh://root@darkbot
50 changes: 24 additions & 26 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
name: Pages

on:
'on':
push:
branches: ["master"]

branches:
- master
workflow_dispatch: {}
jobs:
github-pages:
name: Github Pages
job:
name: Pages
runs-on: ubuntu-22.04
permissions:
pages: write
id-token: write
contents: read
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Check out source repository
uses: actions/checkout@v2
- uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Ensure pip
run: >
python3 --version
python3 -m ensurepip
- name: Install requirements.txt
run: pip install -r requirements.txt -c constraints.txt
- name: Build docs
run: mkdocs build
- uses: actions/upload-pages-artifact@v1
with:
name: github-pages
path: ./site
- name: Deploy to pages
uses: actions/deploy-pages@v1
id: deployment
- name: Check out source repository
uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: ensure pip
run: python3 -m ensurepip
- name: Install requirements.txt
run: pip install -r requirements.txt -c constraints.txt
- name: Build docs
run: mkdocs build
- uses: actions/upload-pages-artifact@v1
with:
name: github-pages
path: './site'
- uses: actions/deploy-pages@v1
id: deployment
35 changes: 15 additions & 20 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Test

on:
'on':
push:
branches: ["master"]
branches:
- master
schedule:
# every sunday
- cron: '0 10 1 * *'

- cron: '0 10 1 * *'
workflow_dispatch: {}
jobs:
test:
job:
name: Test
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Install Taskfile
uses: darklab8/infra/.github/actions/install-taskfile@master

- name: Set up Go
uses: darklab8/infra/.github/actions/install-go@master

- name: Test
run: task test
- name: Check out source repository
uses: actions/checkout@v3
- name: Install Go
uses: darklab8/infra/.github/actions/install-go@master
- name: Install Taskfile
uses: darklab8/infra/.github/actions/install-taskfile@master
- name: test
run: task test
4 changes: 4 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ tasks:
cmds:
- task: profile:render
- task: profile:render2

kcl:build:
cmds:
- kcl kcls/build.k
15 changes: 15 additions & 0 deletions kcls/build.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import .wf_docker_staging
import .wf_docker_production
import .wg_test
import .wg_pages

import yaml

_workflows = [
wf_docker_staging._workflow
wf_docker_production._workflow
wg_test._workflow
wg_pages._workflow
]

_run = [yaml.dump_to_file(a, ".github/workflows/${a._filename}", ignore_private=True, ignore_none=True) for a in _workflows]
1 change: 1 addition & 0 deletions kcls/common.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_image_name = "darkwind8/darkbot"
7 changes: 7 additions & 0 deletions kcls/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "kcls"
edition = "v0.11.0"
version = "0.0.1"

[dependencies]
infra = { path = "../../infra", version = "0.0.1" }
5 changes: 5 additions & 0 deletions kcls/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependencies]
[dependencies.infra]
name = "infra"
full_name = "infra_0.0.1"
version = "0.0.1"
38 changes: 38 additions & 0 deletions kcls/wf_docker_production.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import infra.kcls.models.github_workflow as gw
import infra.kcls.common.github as ghc
import infra.kcls.common.docker
import .common as c

_workflow_name = "Deploy docker Production"
_workflow = gw.Workflow {
_filename = "docker-production.yml"
name = _workflow_name
on = {
workflow_dispatch: {}
push.tags: ghc.TagsSemverProduction
}
env = ghc.DockerHostEnv
jobs = {
job = _production_deploy_job
}
}
_production_deploy_job: gw.Job = {
name = _workflow_name
steps = [
ghc.CheckoutRepo
ghc.DockerLogin
ghc.InstallDarklabSshKey
ghc.GetVersionFromTag
docker.Build(docker.BuildOpts {
image_name = c._image_name
tag = ghc.GetVersionOutput
extra_tags = ["production"]
push = True
})
docker.DeploySwarm(docker.DeploySwarmOpts {
image_name = c._image_name
tag = "production"
service_name = "darkbot-production"
})
]
}
Loading

0 comments on commit afda625

Please sign in to comment.