Last active
October 29, 2024 10:04
-
-
Save sagar290/4a7eb7057718be8a65c2d9857426d5fe to your computer and use it in GitHub Desktop.
Worker file for digital ocean
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 Push Docker Image | |
on: | |
push: | |
branches: | |
- master # Specify the branches that should trigger the workflow | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Checkout the repository | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
# Step 2: Set up Docker tag with a random tag | |
- name: Set up Docker tag | |
run: echo "IMAGE_TAG=$(date +%s)-${GITHUB_SHA::7}" >> $GITHUB_ENV | |
# Step 3: Log in to Docker registry in DigitalOcean | |
- name: Log in to Docker Hub | |
env: | |
DOCKER_USERNAME: ${{ secrets.DIGITALOCEAN_USERNAME }} | |
DOCKER_PASSWORD: ${{ secrets.DIGITALOCEAN_REGISTRY_SECRETS }} | |
run: echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin registry.digitalocean.com | |
# Step 4: Build the Docker image with the random tag | |
- name: Build Docker image | |
run: docker build -t registry.digitalocean.com/repository/repository-name:${{ env.IMAGE_TAG }} . | |
# Step 5: Push the Docker image to DigitalOcean's registry | |
- name: Push Docker image to DigitalOcean | |
run: docker push registry.digitalocean.com/repository/repository-name:${{ env.IMAGE_TAG }} | |
# Step 6: Output the image tag for reference | |
- name: Output Image Tag | |
run: echo "Image has been tagged with:" ${{ env.IMAGE_TAG }} | |
# Log into the DigitalOcean Droplet and pull the image | |
- name: Deploy to DigitalOcean Droplet | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
DROPLET_IP: ${{ secrets.DIGITALOCEAN_DROPLET_IP }} | |
USERNAME: ${{ secrets.DIGITALOCEAN_DROPLET_USER }} | |
run: | | |
mkdir -p ~/.ssh | |
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
# Add the droplet to known hosts to prevent SSH verification issues | |
ssh-keyscan -H "$DROPLET_IP" >> ~/.ssh/known_hosts | |
# SSH into the droplet and pull the latest Docker image | |
ssh "$USERNAME@$DROPLET_IP" << EOF | |
docker login -u "${DOCKER_USERNAME}" -p "${DOCKER_PASSWORD}" registry.digitalocean.com | |
docker pull registry.digitalocean.com/ecourier/ecourier-api:${{ env.IMAGE_TAG }} | |
cd /webapps/ecourier | |
sed -i "s/^TAG=.*/TAG=${{ env.IMAGE_TAG }}/" .env | |
docker compose down --timeout 0 --remove-orphans && docker compose up -d --build && docker compose ps | |
# Remove old Docker images (except the newly pulled one) | |
docker images --format="{{.Repository}}:{{.Tag}} {{.ID}}" | awk '!/${{ env.IMAGE_TAG }}/' | grep ecourier-api | cut -d' ' -f2 | xargs docker rmi | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment