Skip to content

Instantly share code, notes, and snippets.

@woudsma
Last active July 24, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woudsma/288635310216de7950666281417d7daa to your computer and use it in GitHub Desktop.
Save woudsma/288635310216de7950666281417d7daa to your computer and use it in GitHub Desktop.
Deploy project with Dockerfile to Dokku using Github Actions and Github Container Registry (ghcr.io). Deploy either staging or production based on branch.
# Example repository with Dockerfile and NGINX config: https://github.com/woudsma/es6-boilerplate
name: Deploy to Dokku
on:
pull_request:
types:
- closed
branches:
- develop
- master
push:
branches:
- develop
- master
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
REMOTE_USER: root
# Specify remote host by IP or domain that resolves to Dokku host IP
REMOTE_HOST: 123.123.123.123
# Container registry
REGISTRY: ghcr.io
# Dokku app name
APP_NAME: my-app.com
jobs:
build:
runs-on: ubuntu-latest
if: "! contains(github.event.head_commit.message, '[skip ci]')"
steps:
- uses: actions/checkout@v2
# Store branch name in current workflow env
- name: Set environment
run: echo "BRANCH=$(echo ${GITHUB_REF##*/})" >> $GITHUB_ENV
# Prepend 'staging.' to app name if branch name is develop
# This enables you to deploy branches of the same repo to different URL's
# E.g. develop branch deploys to staging.my-app.com, master branch deploys to my-app.com
- name: Change to staging environment
if: env.BRANCH == 'develop'
run: echo "APP_NAME=staging.$APP_NAME" >> $GITHUB_ENV
- name: Set image tag and remote host variables
run: |
echo "IMAGE_TAG=$(echo ${REPOSITORY_OWNER}/${APP_NAME})" >> $GITHUB_ENV
echo "REMOTE=$(echo ${REMOTE_USER}@${REMOTE_HOST})" >> $GITHUB_ENV
- name: Build and push to container registry
uses: docker/build-push-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
# Add a Personal Access Token to the repo secrets to access the Github Container Registry
# Docs: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token
# While creating the PAT, select these scopes: repo, workflow, write:packages, delete:packages
password: ${{ secrets.CR_PAT }}
repository: ${{ env.IMAGE_TAG }}
# Optional: add build arguments, separate multiple arguments with a comma
build_args: PUBLIC_URL=https://${{ env.APP_NAME }},REACT_APP_MY_VAR='some variable'
tags: latest
# Tip: create a local SSH key specifically for deploying to your Dokku host, name it ~/.ssh/id_rsa_dokku for example
# Make sure that this key can be used to deploy to Dokku by following the Dokku docs
# Docs: https://dokku.com/docs/deployment/user-management/
# Add a new repo secret SSH_PRIVATE_KEY and copy-paste the contents of ~/.ssh/id_rsa_dokku as the secret value
# (use the private key, not the contents of the public key in ~/.ssh/id_rsa_dokku.pub)
- name: Deploy
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" | tr -d '\r' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
ssh-keyscan -H ${{ env.REMOTE_HOST }} >> ~/.ssh/known_hosts
ssh ${{ env.REMOTE }} docker login ${{ env.REGISTRY }} -u ${{ github.actor }} -p ${{ secrets.CR_PAT }}
ssh ${{ env.REMOTE }} docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_TAG }}
ssh ${{ env.REMOTE }} docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_TAG }} dokku/${{ env.APP_NAME }}:${{ github.sha }}
ssh ${{ env.REMOTE }} dokku tags:deploy ${{ env.APP_NAME }} ${{ github.sha }}
# Optional: post message to a slack channel using Slack 'Incoming Webhooks'
- name: Slack notify
uses: rtCamp/action-slack-notify@v2.0.2
if: ${{ success() }}
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_TITLE: 'https://${{ env.APP_NAME }} deployed:'
SLACK_USERNAME: Github
SLACK_ICON_EMOJI: ':cloud:'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment