Merge pull request #1 from maazh10/feat/blog-app-pipeline #50
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: Build and Deploy | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
env: | |
PORTFOLIO_IMAGE_NAME: portfolio | |
BLOG_IMAGE_NAME: blog | |
jobs: | |
push: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'push' | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Log into Docker Hub | |
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin | |
- name: Build Portfolio Image | |
run: docker build . --file Dockerfile --tag $PORTFOLIO_IMAGE_NAME | |
- name: Build Blog Image | |
run: docker build blog/ --file blog/Dockerfile --tag $BLOG_IMAGE_NAME | |
- name: Log into GitHub Container Registry | |
run: echo "${{ secrets.CR_PAT }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Push Portfolio App Image to GitHub Container Registry | |
run: | | |
PORTFOLIO_IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$PORTFOLIO_IMAGE_NAME | |
PORTFOLIO_IMAGE_ID=$(echo $PORTFOLIO_IMAGE_ID | tr '[A-Z]' '[a-z]') | |
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | |
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') | |
[ "$VERSION" == "main" ] && VERSION=latest | |
docker tag $PORTFOLIO_IMAGE_NAME $PORTFOLIO_IMAGE_ID:$VERSION | |
docker push $PORTFOLIO_IMAGE_ID:$VERSION | |
- name: Push Blog App Image to GitHub Container Registry | |
run: | | |
BLOG_IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$BLOG_IMAGE_NAME | |
BLOG_IMAGE_ID=$(echo $BLOG_IMAGE_ID | tr '[A-Z]' '[a-z]') | |
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | |
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') | |
[ "$VERSION" == "main" ] && VERSION=latest | |
docker tag $BLOG_IMAGE_NAME $BLOG_IMAGE_ID:$VERSION | |
docker push $BLOG_IMAGE_ID:$VERSION | |
- name: Pull Images and Restart Containers on Google Cloud VM | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.HOST }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.KEY }} | |
script: | | |
cd portfolio-website | |
docker-compose down | |
docker-compose pull | |
docker-compose up -d |