Skip to content

Commit

Permalink
chore: tweak workflows for staging and production environments (#89)
Browse files Browse the repository at this point in the history
* chore: tweak CI workflow to push images as ``latest`` with every merge on develop

* chore: create new workflow to push versioned and ``stable`` images whenever a new release is created
  • Loading branch information
corp-0 authored Mar 3, 2024
1 parent 8b11038 commit d98b6e7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 100 deletions.
101 changes: 45 additions & 56 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ on:
- "*"
push:
branches:
- main
- develop
tags:
- "v*"

env:
IMAGE_NAME: unitystation/central-command
Expand All @@ -17,70 +14,62 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
# because pre-commit uses external mypy
- name: install mypy
run: |
pip install poetry
poetry config virtualenvs.create false
poetry install --only main,typecheck
- name: install mypy
run: |
pip install poetry
poetry config virtualenvs.create false
poetry install --only main,typecheck
# https://github.com/typeddjango/django-stubs/issues/458
- name: create .env file
run: cp example.env .env
- uses: pre-commit/[email protected]
- name: create .env file
run: cp example.env .env
- uses: pre-commit/[email protected]

unit_test:
needs: [ lint ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install poetry
poetry config virtualenvs.create false
poetry install --only main
- name: create .env file
run: cp example.env .env
- name: Run tests
env:
SECRET_KEY: secret
DB_ENGINE: django.db.backends.sqlite3
run: |
cd src
python manage.py makemigrations --check
python manage.py migrate
python manage.py test tests/
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install poetry
poetry config virtualenvs.create false
poetry install --only main
- name: create .env file
run: cp example.env .env
- name: Run tests
env:
SECRET_KEY: secret
DB_ENGINE: django.db.backends.sqlite3
run: |
cd src
python manage.py makemigrations --check
python manage.py migrate
python manage.py test tests/
docker:
needs: [ lint, unit_test ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build docker image
run: |
docker pull $IMAGE_NAME
docker build --pull --cache-from $IMAGE_NAME -t $IMAGE_NAME:latest .
- uses: actions/checkout@v4
- name: Build docker image
run: |
docker pull $IMAGE_NAME
docker build --pull --cache-from $IMAGE_NAME -t $IMAGE_NAME:latest .
- name: Log in into Docker Hub
if: ${{ github.event_name == 'push' }}
run: |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Log in into Docker Hub
if: ${{ github.event_name == 'push' }}
run: |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
# Context:
# For some reason, commits from semantic-release bot does not trigger any actions.
# Build/push steps were copied into release workflow until we figure out a better way.
# - name: Add a release tag to image
# if: contains(github.ref, 'refs/tags/')
# run: |
# docker tag $IMAGE_NAME $IMAGE_NAME:${GITHUB_REF#refs/tags/}

- name: Push image to registry
if: ${{ github.event_name == 'push' }}
run: |
docker push $IMAGE_NAME
- name: Push image to registry
if: ${{ github.event_name == 'push' }}
run: |
docker push $IMAGE_NAME
72 changes: 28 additions & 44 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,54 +1,38 @@
name: Release
on:
push:
branches:
- main
push:
tags:
- "v*"

env:
IMAGE_NAME: unitystation/central-command
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-node@v1
with:
node-version: 12
- name: plugins
run: npm install @semantic-release/changelog -D @semantic-release/git -D
- name: generate release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx semantic-release
docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

deploy:
runs-on: ubuntu-latest
needs: [release]
env:
IMAGE_NAME: unitystation/central-command
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Extract Tag Name
id: extract_tag
run: echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"

- name: Get Latest Tag
id: lasttag
uses: WyriHaximus/[email protected]
- name: Build docker image with tag
run: |
docker pull $IMAGE_NAME:latest
docker build --pull --cache-from $IMAGE_NAME:latest -t $IMAGE_NAME:${{ steps.extract_tag.outputs.tag }} .
# Steps below are copied from main workflow
- name: Build image
run: |
docker pull $IMAGE_NAME
docker build --pull --cache-from $IMAGE_NAME -t $IMAGE_NAME:latest .
- name: Tag image as stable
run: |
docker tag $IMAGE_NAME:${{ steps.extract_tag.outputs.tag }} $IMAGE_NAME:stable
- name: Log in into Docker Hub
run: |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Log in into Docker Hub
run: |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Add a release tag to image
run: |
docker tag $IMAGE_NAME $IMAGE_NAME:${{ steps.lasttag.outputs.tag }}
- name: Push tagged image to registry
run: |
docker push $IMAGE_NAME:${{ steps.extract_tag.outputs.tag }}
- name: Push image to registry
run: |
docker push $IMAGE_NAME
- name: Push stable image to registry
run: |
docker push $IMAGE_NAME:stable

0 comments on commit d98b6e7

Please sign in to comment.