Skip to content

Experimenting with variables 6 #31

Experimenting with variables 6

Experimenting with variables 6 #31

Workflow file for this run

name: Deployment
on:
push:
branches:
- devops_core_1
jobs:
generate_id:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get-version.outputs.version }}
env:
IMAGE_NAME: ""
IMAGE_VERSIOBN: ""
steps:
- name: "Create the default version for this commit"
id: "get-version"
run: |
## get the random version
## make an API call to the server to get last release ..
## Or Use RANDOM
echo "version=$( echo $RANDOM )" >> $GITHUB_OUTPUT
## store this variable to $GITHUB_OUTPUT global environmental variable
# echo version >> $GITHUB_OUTPUT
- name: "Use the version generated in the step above"
run: |
echo ${{ steps.get-version.outputs.version }}
app_deployment:
runs-on: ubuntu-latest
needs:
- generate_id
env:
VERSION: ${{ needs.generate_id.outputs.version }}
steps:
- name: 'Checkout the repo'
uses: actions/checkout@v4
- name: "Build an image"
run: |
docker build -t theoafactor/booksfinder:${{ needs.generate_id.outputs.version }} .
- name: 'Log into Docker'
run: |
docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }}
- name: "Push the image to Docker Hub"
run: |
docker push theoafactor/booksfinder:${{ needs.generate_id.outputs.version }}
- name: "SSH into EC2 instance"
env:
SSH_KEY: ${{ secrets.EC2_PRIVATE_KEY }}
run: |
echo "$SSH_KEY" > ssh_key && chmod 600 ssh_key
ssh -o StrictHostKeyChecking=no -i ssh_key [email protected] version=$( echo ${{ needs.generate_id.outputs.version }} ) '
sudo apt-get update &&
sudo apt-get install ca-certificates curl -y &&
sudo install -m 0755 -d /etc/apt/keyrings &&
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc &&
sudo chmod a+r /etc/apt/keyrings/docker.asc &&
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null &&
sudo apt-get update &&
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y &&
sudo docker stop $( sudo docker ps -a -q ) &&
sudo docker rm $( sudo docker ps -a -q ) &&
sudo docker rmi $( sudo docker images -q ) &&
sudo docker pull theoafactor/booksfinder:$version &&
sudo docker run -d -p 80:80 theoafactor/booksfinder:$version
'