fix: write key to json file #91
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: Setup SSH | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Deploy to SSH server | |
env: | |
REGION: ${{ secrets.GCE_REGION }} | |
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} | |
GITHUB_SHA: ${{ github.sha }} | |
run: | | |
# Base64 encode the GCP_SA_KEY to avoid issues with special characters | |
GCP_SA_KEY_BASE64=$(echo '${{ secrets.GCP_SA_KEY }}' | base64 -w 0) | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} | |
cd ${{ secrets.PROJECT_PATH }} | |
if [ ! -f docker-compose.yml ]; then | |
echo "docker-compose.yml not found in ${{ secrets.PROJECT_PATH }}" | |
exit 1 | |
fi | |
# Decode the GCP_SA_KEY and save it to a file | |
echo "$GCP_SA_KEY_BASE64" | base64 --decode > gcp-sa-key.json | |
# Authenticate Docker with Google Cloud | |
cat gcp-sa-key.json | docker login -u _json_key --password-stdin https://${{ secrets.GCE_REGION }}-docker.pkg.dev | |
# Update docker-compose.yml to use the key file | |
sed -i 's|GCP_SA_KEY=.*|GCP_SA_KEY=/path/to/gcp-sa-key.json|g' docker-compose.yml | |
# Set other environment variables | |
sed -i 's|REGION=.*|REGION=${{ env.REGION }}|g' docker-compose.yml | |
sed -i 's|PROJECT_ID=.*|PROJECT_ID=${{ env.PROJECT_ID }}|g' docker-compose.yml | |
sed -i 's|GITHUB_SHA=.*|GITHUB_SHA=${{ github.sha }}|g' docker-compose.yml | |
# Pull and restart containers | |
docker-compose pull | |
docker-compose down | |
docker-compose up -d | |
# Clean up the key file | |
rm gcp-sa-key.json |