fix : docker cicd #69
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
# This workflow uses actions that are not certified by GitHub. | |
# They are provided by a third-party and are governed by | |
# separate terms of service, privacy policy, and support | |
# documentation. | |
# GitHub recommends pinning actions to a commit SHA. | |
# To get a newer version, you will need to update the SHA. | |
# You can also reference a tag or branch, but the action may change without warning. | |
name: Publish Docker image | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
release: | |
types: [published] | |
workflow_dispatch: | |
jobs: | |
push_to_registry: | |
name: Push Docker image to Docker Hub | |
runs-on: ubuntu-latest | |
env: | |
DOCKER_IMAGE_NAME_STAGING: url-shortener | |
DOCKER_CONTAINER_NAME_STAGING: url-shortener-staging | |
DOCKER_IMAGE_NAME: url-shortener | |
DOCKER_CONTAINER_NAME: url-shortner | |
steps: | |
- name: Check out the repo | |
uses: actions/checkout@v4 | |
- name: Decrypt large secret | |
shell: bash | |
run: | | |
./scripts/decrypt_secret.sh | |
env: | |
LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: jerryokafor/url-shortener | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
- name: Setup gcloud Auth | |
id: account | |
uses: google-github-actions/auth@v2 | |
with: | |
credentials_json: ${{ secrets.GCP_SA_KEY }} | |
- name: Setup gcloud SDK | |
uses: google-github-actions/setup-gcloud@v2 | |
with: | |
project_id: ${{ secrets.GCP_PROJECT_ID }} | |
- name: Access gcloud SDK | |
run: | | |
gcloud info | |
gcloud auth configure-docker | |
- name: Build docker-compose.yml | |
run: | | |
sed "s/DOCKER_IMAGE_NAME_STAGING/${{secrets.DOCKER_USERNAME}}\/${{env.DOCKER_IMAGE_NAME_STAGING}}/g" docker-compose-template.yml > docker-compose.yml | |
sed -i "s/DOCKER_IMAGE_NAME/${{secrets.DOCKER_USERNAME}}\/${{env.DOCKER_IMAGE_NAME}}/g" docker-compose.yml | |
sed -i "s/DOCKER_CONTAINER_NAME_STAGING/${{env.DOCKER_CONTAINER_NAME_STAGING}}/g" docker-compose.yml | |
sed -i "s/DOCKER_CONTAINER_NAME/${{env.DOCKER_CONTAINER_NAME}}/g" docker-compose.yml | |
cat docker-compose.yml | |
- name: Compose and copy .env file | |
run: | | |
gcloud compute scp docker-compose.yml jerryhanksokafor@${{vars.GCP_INSTANCE_NAME}}:"/home/jerryhanksokafor/url-shortener/docker-compose.yml" --zone "us-central1-a" --project "urls-4b" | |
- name: SSH into VM | |
id: compute-ssh | |
uses: google-github-actions/ssh-compute@v1 | |
with: | |
instance_name: ${{ vars.GCP_INSTANCE_NAME }} | |
zone: ${{ vars.GCP_INSTANCE_ZONE }} | |
ssh_private_key: ${{ secrets.GCP_SSH_PRIVATE_KEY }} | |
command: | | |
sudo docker-compose -f /home/jerryhanksokafor/url-shortener/docker-compose.yml --profile staging down | |
sudo docker-compose -f /home/jerryhanksokafor/url-shortener/docker-compose.yml --profile staging build --pull | |
sudo docker-compose -f /home/jerryhanksokafor/url-shortener/docker-compose.yml --profile staging up -d | |
sudo docker image prune -f |