updated cd.yml #6
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: Deploy | |
on: | |
push: | |
branches: | |
- main | |
env: | |
IMAGE_NAME: ${{ github.event.repository.name }} | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
DB_USER_NAME: ${{ secrets.DB_USER_NAME }} # Add here environment variables first and then when reference it when you build the docker image | |
DB_USER_PASSWORD: ${{ secrets.DB_USER_PASSWORD }} # Add here environment variables first and then when reference it when you build the docker image | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- id: lower-repo | |
name: Repository to lowercase | |
run: | | |
echo "repository=${GITHUB_REPOSITORY@L}" >> $GITHUB_OUTPUT | |
- name: Build and Push Docker Image | |
run: | | |
docker buildx build --platform linux/amd64,linux/arm64 \ | |
--tag ghcr.io/${{ steps.lower-repo.outputs.repository }}:latest \ | |
--build-arg DB_USER_NAME=${{ env.DB_USER_NAME }} \ | |
--build-arg DB_USER_PASSWORD=${{ env.DB_USER_PASSWORD }} \ | |
--push . | |
- name: Verify Image | |
run: | | |
docker pull ghcr.io/${{ github.repository_owner }}/${{ github.repository }}:latest | |
docker images | |
- name: Image digest | |
run: echo ${{ steps.build-and-push.outputs.digest }} | |
- uses: appleboy/[email protected] | |
name: Pull image and start container | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
docker container rm -f ${{ env.IMAGE_NAME }} 2> /dev/null || true | |
docker image rm ghcr.io/${{ steps.lower-repo.outputs.repository }}:latest 2> /dev/null || true | |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin | |
docker pull ghcr.io/${{ steps.lower-repo.outputs.repository }}:latest | |
docker run -d -p 8080:8080 --name ${{ env.IMAGE_NAME }} ghcr.io/${{ steps.lower-repo.outputs.repository }}:latest | |
# Explainaition for the last line: | |
# docker run -d -p [externalPort]:[internalPort] --name [imageName] ghcr.io/[repositoryOwner]/[repositoryName]:latest |