This is a simple Golang Model-Controller template using Functions Framework for Go and mongodb.com as the database host. It is compatible with Google Cloud Function CI/CD deployment.
Start here: Just Fork this repo
The first thing to do is prepare a Mongo database using this template:
-
Sign up for mongodb.com and create one instance of Data Services of mongodb.
-
Go to Network Access menu > + ADD IP ADDRESS > ALLOW ACCESS FROM ANYWHERE

-
Download MongoDB Compass, connect with your mongo string URI from mongodb.com
-
Import this json into reply collection.



-
Create a profile collection, and insert this JSON document with your 30-day token and WhatsApp number.


{ "token":"v4.public.asoiduasoijfiun98erjg98egjpoikr", "phonenumber":"6281111222333" }
This boilerplate has several folders with different functions, such as:
- .github: GitHub Action yml configuration.
- config: all apps configuration like database, API, token.
- controller: all of the endpoints functions
- model: all of the type structs used in this app
- helper: helper folder with a list of functions only called by others file
- route: all routes URL
Untuk melakukan deploy Azure Function menggunakan Golang dan GitHub Actions, Anda bisa mengikuti langkah-langkah berikut:
-
Buat Proyek Azure Function: Pastikan Anda sudah mengikuti langkah-langkah di atas untuk membuat proyek Azure Function dengan Golang.
-
Inisialisasi Git: Inisialisasi repository Git di folder proyek Anda:
git init
-
Buat Repository di GitHub: Buat repository baru di GitHub dan hubungkan ke repository lokal Anda:
git remote add origin https://github.com/username/repository.git
-
Buat Folder Workflow: Buat folder
.github/workflowsdi dalam root proyek Anda. -
Buat File Workflow: Buat file baru di dalam folder
.github/workflowsdengan namadeploy.ymldan tambahkan konfigurasi berikut:name: Deploy Azure Function on: push: branches: - main jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up Go uses: actions/setup-go@v2 with: go-version: '1.16' - name: Install Azure CLI run: | curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash - name: Login to Azure uses: azure/login@v1 with: creds: ${{ secrets.AZURE_CREDENTIALS }} - name: Deploy to Azure Functions run: | func azure functionapp publish <your_function_app_name>
-
Buat Service Principal: Buat Service Principal untuk otentikasi dengan Azure:
az group list --output table az account list --output table func init --worker-runtime custom func new --template "HTTP trigger" --name gocroot az account set --subscription <your_subscription_id> az functionapp config appsettings set --name <your_function_app_name> --resource-group <your_resource_group> --settings FUNCTIONS_WORKER_RUNTIME=custom az ad sp create-for-rbac --name "myGitHubActionsServicePrincipal" --role contributor --scopes /subscriptions/<your_subscription_id>/resourceGroups/<your_resource_group> --sdk-auth
Ini akan menghasilkan JSON output yang berisi kredensial yang diperlukan untuk login ke Azure.
-
Tambah Secrets di GitHub: Tambahkan kredensial yang dihasilkan ke secrets repository GitHub Anda:
- Buka repository Anda di GitHub.
- Pergi ke
Settings > Secrets > Actions. - Klik
New repository secret. - Nama secret adalah
AZURE_CREDENTIALS. - Isi nilai secret dengan JSON output dari langkah sebelumnya.
Sekarang, setiap kali Anda push ke branch main, GitHub Actions akan otomatis menjalankan workflow untuk build dan deploy Azure Function Anda.
-
Commit dan Push Kode:
git add . git commit -m "Initial commit" git push origin main
-
Verifikasi Deployment: Setelah workflow berhasil dijalankan, Anda dapat memverifikasi deployment Anda dengan mengakses URL Azure Function yang telah Anda deploy.
Dengan mengikuti langkah-langkah di atas, Anda dapat melakukan deploy Azure Function menggunakan Golang dan GitHub Actions secara otomatis setiap kali ada perubahan di branch main.

