Merge pull request #72 from dotslashf/development #114
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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- id: "auth" | |
uses: "google-github-actions/auth@v2" | |
with: | |
credentials_json: "${{ secrets.GCP_SA_KEY }}" | |
- name: "Set up Cloud SDK" | |
uses: "google-github-actions/setup-gcloud@v2" | |
- name: "Use gcloud CLI" | |
run: "gcloud info" | |
- name: Configure Docker for Artifact Registry | |
run: | | |
gcloud auth configure-docker ${{ secrets.GCE_REGION }}-docker.pkg.dev | |
- name: Build and push Docker image | |
env: | |
GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} | |
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} | |
GITHUB_SHA: ${{ github.sha }} | |
GCE_REGION: ${{ secrets.GCE_REGION }} | |
BUCKET_NAME: ${{ secrets.BUCKET_NAME }} | |
run: | | |
echo "$GCP_SA_KEY" > gcp-service-account-key.json | |
docker build --build-arg DATABASE_URL=${{ secrets.DATABASE_URL }} --build-arg GCS_BUCKET_NAME=$BUCKET_NAME --build-arg GCP_SA_KEY="$GCP_SA_KEY" -t $GCE_REGION-docker.pkg.dev/$PROJECT_ID/arsip-template/app:$GITHUB_SHA . | |
docker push $GCE_REGION-docker.pkg.dev/$PROJECT_ID/arsip-template/app:$GITHUB_SHA | |
- name: Copy docker-compose.yml to SSH server | |
uses: appleboy/scp-action@v0.1.7 | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
source: "docker-compose.yml" | |
target: "${{ secrets.PROJECT_PATH }}/" | |
- name: Deploy to SSH server | |
uses: appleboy/ssh-action@v0.1.7 | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
export REGION=${{ secrets.GCE_REGION }} | |
export PROJECT_ID=${{ secrets.GCP_PROJECT_ID }} | |
export GITHUB_SHA=${{ github.sha }} | |
cd ${{ secrets.PROJECT_PATH }} | |
if [ ! -f docker-compose.yml ]; then | |
echo "docker-compose.yml not found in ${{ secrets.PROJECT_PATH }}" | |
exit 1 | |
fi | |
# Pull the latest image from Google Artifact Registry | |
docker-compose pull | |
docker-compose down | |
docker-compose up -d |