Merge pull request #187 from DevopsGroupC/release/candidate #100
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
# The following secrets are required from the repository's settings: | |
# https://github.com/your_username/itu-minitwit-ci/settings/secrets/actions | |
# DOCKER_USERNAME | |
# DOCKER_PASSWORD | |
# SSH_USER = root | |
# SSH_KEY | |
# SSH_LEADER | |
# DB_CONNECTIONSTRING | |
name: Deploy | |
on: | |
push: | |
branches: | |
- 'develop' | |
- 'main' | |
jobs: | |
# source: https://magnussundstrom.se/blog/github-action-environment-check | |
set-environment: #this is a job named 'set-environment' | |
runs-on: ubuntu-latest #your preferred OS | |
outputs: #the env value needs to be exposed as an output in order to be reached from other jobs | |
current_env: ${{ steps.set_env.outputs.current_env }} #output variable to store the relevant environment | |
steps: | |
- name: Check if prod | |
if: endsWith(github.ref, '/main') #if the triggering branch is 'main' | |
run: | | |
echo "ENVIRONMENT_NAME=production" >> $GITHUB_ENV #retrieve secrets from 'production' environment in Github | |
- name: Check if develop | |
if: endsWith(github.ref, '/develop') #if the triggering branch is 'dev' | |
run: | | |
echo "ENVIRONMENT_NAME=staging" >> $GITHUB_ENV #retrieve secrets from 'staging' environment in Github | |
- name: Set output | |
id: set_env | |
run: echo "current_env=${{ env.ENVIRONMENT_NAME }}" >> $GITHUB_OUTPUT #assign the value of ENVIRONMENT_NAME to the output variable 'current_env' | |
build: | |
runs-on: ubuntu-latest | |
needs: set-environment #this job won't be executed until the set-environment job is finished | |
environment: ${{ needs.set-environment.outputs.current_env }} #this job will now retrieve secret values stored in either 'staging' or 'production' repo environments, depending on the value set in previous job | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Build and push csharptwit to DockerHub | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./csharp-minitwit | |
file: ./csharp-minitwit/Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKER_USERNAME }}/csharptwit-${{ needs.set-environment.outputs.current_env }}:latest | |
cache-from: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/csharptwit-${{ needs.set-environment.outputs.current_env }}:webbuildcache | |
cache-to: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/csharptwit-${{ needs.set-environment.outputs.current_env }}:webbuildcache,mode=max | |
build-args: ENVIRONMENT=${{ needs.set-environment.outputs.current_env }} | |
- name: Configure SSH | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "$SSH_KEY" > ~/.ssh/ssh_key | |
chmod 600 ~/.ssh/ssh_key | |
env: | |
SSH_KEY: ${{ secrets.SSH_KEY }} | |
- name: Copy files to server | |
# Updating stack files on remote server from the infrastructure/stack folder | |
run: > | |
scp -i ~/.ssh/ssh_key -o StrictHostKeyChecking=no | |
./infrastructure/stack/* $SSH_USER@$SSH_LEADER:~/ | |
env: | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SSH_LEADER: ${{ secrets.SSH_LEADER }} | |
- name: Deploy to server | |
# Pulls minitwit image from Docker Hub and updates the swarm | |
run: | | |
ssh -i ~/.ssh/ssh_key -o StrictHostKeyChecking=no $SSH_USER@$SSH_LEADER << 'EOF' | |
current_env="${{ needs.set-environment.outputs.current_env }}" | |
case "$current_env" in | |
production) export STAGE="Production" ;; | |
staging) export STAGE="Staging" ;; | |
*) echo "Unknown environment: $current_env" >&2; exit 1 ;; | |
esac | |
export ConnectionStrings__DefaultConnection='${{ secrets.DB_CONNECTIONSTRING }}' | |
export DOCKER_USERNAME='${{ secrets.DOCKER_USERNAME }}' | |
export STAGE='${{ needs.set-environment.outputs.current_env }}' | |
echo "Deploying stack 'minitwit'..." | |
docker stack deploy minitwit -c minitwit_stack.yml | |
EOF | |
env: | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SSH_LEADER: ${{ secrets.SSH_LEADER }} |