Update CICD.yml with env for username #22
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: CICD | |
on: | |
push: | |
branches: | |
- prod | |
jobs: | |
run_tests: | |
runs-on: ubuntu-latest | |
steps: | |
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it | |
- uses: actions/checkout@v3 | |
- name: setup the testing server with required packages | |
run: | | |
sudo apt-get -y upgrade && sudo apt-get -y update && sudo apt-get -y install python3.9 | |
sudo apt -y install python3-wheel vim | |
export FLASK_APP=blogging.py | |
export FLASK_CONFIG=testing | |
python3 -m pip install -r requirements/common.txt | |
python3 -m flask test | |
build_container_and_push_to_dockerhub: | |
runs-on: ubuntu-latest | |
needs: run_tests | |
steps: | |
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it | |
- uses: actions/checkout@v3 | |
- name: create the required env variable files from the secrets | |
# touch .env-mysql is needed since even though we are not building mysql, the docker-compose file has the mysql service there too | |
# and it still looks for it, so just make it to satisfy this unnecessity. | |
run: | | |
touch .env-mysql | |
touch .env-blogging | |
echo FLASK_APP=${{ secrets.FLASK_APP }} >> .env-blogging | |
echo FLASK_CONFIG=${{ secrets.FLASK_CONFIG }} >> .env-blogging | |
echo SECRET_KEY=${{ secrets.SECRET_KEY }} >> .env-blogging | |
echo DATABASE_URL=${{ secrets.DATABASE_URL }} >> .env-blogging | |
- name: build the docker image for the blogging service, keep the mysql service intact | |
run: docker-compose build --no-cache blogging | |
- name: Log in to docker hub | |
run: docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Tag image | |
run: | |
# Usage of tag command is: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] | |
# With this setup, the image, which was built untagged, is now given the "latest" tag | |
docker tag aldosebastian/blogging aldosebastian/blogging:latest | |
- name: Push image to dockerhub | |
# We have push the using the tag instead of the image name; it's just the way things are | |
run: docker push aldosebastian/blogging:latest | |
# After pushing, we go to the server, download the image and run it there | |
deploy_service_on_server: | |
runs-on: ubuntu-latest | |
needs: build_container_and_push_to_dockerhub | |
steps: | |
- name: Deploy to server via SSH action | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSHHOST1 }} | |
username: ${{ secrets.SSHUSERNAME1 }} | |
key: ${{ secrets.SSHKEY1}} | |
script: | | |
# Move to directory where the docker-compose.yml is | |
cd /home/aldo/ | |
# Login to dockerhub registry | |
docker login -u aldosebastian -p ${{ secrets.DOCKERHUB_PASSWORD }} | |
# Stop and remove any blogging service container | |
docker stop blogging_service && docker rm blogging_service | |
# Delete the image that ran the deleted container (i.e. the one with the tag latest), since now this image is outdated | |
# (this is to save space, if you want you can skip this. If skipped, after docker pull the old image will have tag None) | |
docker rmi aldosebastian/blogging:latest | |
# Pull the latest image | |
docker pull aldosebastian/blogging | |
# Run the blogging service container using the docker-compose.yml in the server. | |
# The below actually will start both the blogging and its db containers, but if a db | |
# container already is running, it will say it is still up to date | |
docker-compose up -d |