Skip to content

Commit

Permalink
Add continuous deployment job
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-ni committed Sep 16, 2023
1 parent a3511b8 commit 0936c00
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 58 deletions.
47 changes: 0 additions & 47 deletions .github/workflows/test.yaml

This file was deleted.

95 changes: 95 additions & 0 deletions .github/workflows/test_and_deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Test 🧪 and deploy 🚀

on:
push:
branches:
- "*"
pull_request:
branches: ["master"]

jobs:
test:
runs-on: ubuntu-latest
env:
HKNWEB_MODE: dev
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.9"
- uses: snok/install-poetry@v1
with:
version: 1.6.1
virtualenvs-create: true
virtualenvs-in-project: true
- uses: actions/cache@v3
with:
path: .venv
key: venv-${{ hashFiles('poetry.lock') }}

- run: poetry install --no-interaction --no-root

- name: Run formatting check
run: poetry run black . --check

- name: Run unit tests
run: |
poetry run coverage run
poetry run coverage report
- name: Deploy
run: |
poetry run fab deploy
- name: Run end-to-end tests
run: |
poetry run curl -f localhost:8000
deploy:
needs: test
runs-on: ubuntu-latest
if: success() && github.ref == 'refs/heads/master'
environment:
name: Production
url: https://dev-hkn.eecs.berkeley.edu/
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.9"
- uses: snok/install-poetry@v1
with:
version: 1.6.1
virtualenvs-create: true
virtualenvs-in-project: true
- uses: actions/cache@v3
with:
path: .venv
key: venv-${{ hashFiles('poetry.lock') }}

- run: poetry install --no-interaction --no-root

- name: Unseal secrets
uses: jrmcdonald/blackbox-github-actions@v0.2.0
with:
bb_actions_subcommand: "postdeploy"
env:
BLACKBOX_PUBKEY: ${{ secrets.BLACKBOX_PUBLIC_KEY }}
BLACKBOX_PRIVKEY: ${{ secrets.BLACKBOX_PRIVATE_KEY }}

- name: Setup SSH key
run: |
mkdir -p ~/.ssh/
echo "$SSH_PRIVATE_KEY" > ~/.ssh/pkey
sudo chmod 600 ~/.ssh/pkey
echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
shell: bash
env:
SSH_PRIVATE_KEY: ${{ secrets.OCF_SSH_KEY }}
SSH_KNOWN_HOSTS: ${{ secrets.OCF_SSH_KNOWN_HOSTS }}

- name: Deploy
run: |
poetry run fab deploy -i ~/.ssh/pkey --revision ${{ github.sha }}
env:
HKNWEB_MODE: ${{ vars.HKNWEB_MODE }}
1 change: 0 additions & 1 deletion config/deploy/prod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"deploy": {
"branch": "modernization",
"name": "prod",
"user": "hkn",
"host": "apphost.ocf.berkeley.edu",
Expand Down
22 changes: 12 additions & 10 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ def global_defaults():
return merge_dicts(Config.global_defaults(), hkn_shared)


def setup(c: Connection, commit=None, release=None):
def setup(c: Connection, revision=None, release=None):
print("== Setup ==")

# Returns the server date-time, encoded as YYYYMMSS_HHMMSS.
timestamp = c.run("date +%Y%m%d_%H%M%S").stdout.strip()

# Point the connection to the correct git config
c.release = release if release else timestamp
c.commit = commit if commit else c.deploy.branch
c.revision = revision if revision else c.deploy.branch
print(f"release: {c.release}")
print(f"commit: {c.commit}")
print(f"revision: {c.revision}")

# Setup paths
c.deploy_path = posixpath.join(c.deploy.path.root, c.deploy.name)
Expand Down Expand Up @@ -65,7 +65,7 @@ def update(c: Connection):
c.deploy.repo_url = (
c.run("git config --get remote.origin.url").stdout.strip() + ".git"
)
c.commit = c.run("git rev-parse HEAD").stdout.strip()
c.revision = c.run("git rev-parse HEAD").stdout.strip()

c.run(f"git clone --bare {c.deploy.repo_url} {c.repo_path}", echo=True)

Expand All @@ -76,19 +76,21 @@ def update(c: Connection):
with c.cd(c.repo_path):
c.run(f"git remote set-url origin {c.deploy.repo_url}", echo=True)
c.run("git remote update", echo=True)
c.run(f"git fetch origin {c.commit}:{c.commit}", echo=True)
c.run(f"git fetch origin {c.revision}:{c.revision}", echo=True)
else: # clone
c.run(f"git clone --bare {c.deploy.repo_url} {c.repo_path}", echo=True)

with c.cd(c.repo_path):
print("-- Creating git archive for release")
revision = c.commit
revision = c.revision
revision_number = c.run(
f"git rev-list --max-count=1 {revision} --", echo=True
).stdout.strip()
c.commit = revision_number
c.revision = revision_number

c.run(f"git archive {c.commit} | tar -x -f - -C '{c.release_path}'", echo=True)
c.run(
f"git archive {c.revision} | tar -x -f - -C '{c.release_path}'", echo=True
)

with c.cd(c.release_path):
print("-- Symlinking shared files")
Expand Down Expand Up @@ -137,9 +139,9 @@ def publish(c: Connection) -> None:


@task
def deploy(c, target=None, commit=None):
def deploy(c, target=None, revision=None):
with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
setup(c, commit=commit)
setup(c, revision=revision)
update(c)
publish(c)

Expand Down

0 comments on commit 0936c00

Please sign in to comment.