feat: Cache Docker layers and SSH keys in GitHub Actions workflow #31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: publish | |
on: | |
push: | |
branches: ['master'] | |
env: | |
# Use docker.io for Docker Hub if empty | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.actor }}/linkshrtnr_api:latest | |
jobs: | |
publish: | |
name: publish image | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Cache Docker layers | |
uses: actions/cache@v2 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx- | |
- name: Build and Publish | |
run: | | |
echo " " >> Cargo.toml | |
echo [env] >> Cargo.toml | |
echo DATABASE_URL = \"${{ secrets.DATABASE_URL }}\" >> Cargo.toml | |
docker build . --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} --cache-from=/tmp/.buildx-cache --cache-to=/tmp/.buildx-cache | |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
deploy: | |
needs: publish | |
name: deploy image | |
runs-on: ubuntu-latest | |
steps: | |
- name: Cache SSH keys | |
uses: actions/cache@v2 | |
with: | |
path: ~/.ssh | |
key: ${{ runner.os }}-ssh-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-ssh- | |
- name: install ssh keys | |
run: | | |
install -m 600 -D /dev/null ~/.ssh/id_rsa | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts | |
- name: connect and pull | |
run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd ${{ secrets.WORK_DIR }} && docker compose pull && docker compose up -d && exit" | |
- name: cleanup | |
run: rm -rf ~/.ssh |