-
Notifications
You must be signed in to change notification settings - Fork 17
golang buildspec example #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dharmesh78
wants to merge
6
commits into
dlcbld:main
Choose a base branch
from
Dharmesh78:dharmesh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b808f7
golang buildspec example
Dharmesh78 bf41e81
readme corrected
Dharmesh78 dcd029d
updated as requested
Dharmesh78 00501b9
readme corrected
Dharmesh78 0358141
nodejs updated
Dharmesh78 32b147d
images updated
Dharmesh78 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Build Spec File Example using golang web application and Creating an executable file | ||
|
|
||
| This is an example of a golang web application and creating an executable file. With the [OCI DevOps service](https://www.oracle.com/devops/devops-service/) and this project, you'll be able to build this application,store the executable file in [OCI Artifact Registry.](https://docs.oracle.com/en-us/iaas/artifacts/using/overview.htm) | ||
|
|
||
|
|
||
| ## Building the application locally | ||
|
|
||
| ### Download the repo | ||
| The first step to get started is to download the repository to your local workspace | ||
|
|
||
| ```shell | ||
| git clone git@github.com:dlcbld/BuildSpecSampleFiles.git | ||
| cd examples/golang_webapp_example | ||
| ``` | ||
|
|
||
| ### Install and run the application | ||
|
|
||
| 1. Install golang on your system: https://golang.org/ | ||
| 2. Set the environment path variables | ||
| ```export PATH="/usr/local/go/bin:$PATH"``` | ||
| ```export GOPATH="/usr/local/go"``` | ||
| ```export PATH="$GOPATH/bin:$PATH"``` | ||
| 3. Check that path is set properly by displaying version: | ||
| ```go version``` | ||
| 4. Build the golang web application: | ||
|
|
||
| ```go build -o golangWebApp main.go``` | ||
| 5. Run the app: | ||
| ```./golangWebApp``` | ||
|
|
||
| ## Building the application in OCI DevOps | ||
| Now that you've seen you can locally build this app, let's try this out through OCI DevOps Build service. | ||
| ### Create External Connection to your Git repository | ||
| 1. Create a [DevOps Project](https://docs.oracle.com/en-us/iaas/Content/devops/using/devops_projects.htm) or use and an existing project. | ||
| 2. In your DevOps project, create an External Connection to your GitHub repository which holds your application. | ||
| - Create a Personal Access Token (PAT): https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token | ||
| - In the OCI Console, Go to Identity & Security -> Vault and create a [Vault]( https://docs.oracle.com/en-us/iaas/Content/KeyManagement/Concepts/keyoverview.htm) in compartment of your own choice. | ||
| - Create a Master Key that will be used to encrypt the PATs. | ||
| - Select Secrets from under Resources and create a secret using PAT obtained from GitHub account. | ||
| - Make a note of the OCID of the secret. | ||
| - Now, go to the desired project and select External Connection from the resources. | ||
| - Select type as GitHub and provide OCID of the secret under Personal Access Token. | ||
| - Finally, allow Build Pipeline (dynamic group with DevOps Resources) to use a PAT secret by writing a policy in the root compartment as: ``` Allow dynamic-group dg-with-devops-resources to manage secret-family in tenancy``` | ||
| ### Setup your Build Pipeline | ||
| Create a new Build Pipeline to build, test and deliver artifacts from your GitHub Repository. | ||
| ### Managed Build stage | ||
| In your Build Pipeline, first add a Managed Build stage | ||
| 1. The Build Spec File Path is the relative location in your repo of the build_spec.yaml . Leave the default, for this example. | ||
| 2. For the Primary Code Repository follow the below steps | ||
| - Select connection type as GitHub | ||
| - Select the external connection you created above | ||
| - Give the URL to the repo which contains your application. | ||
| - Select main branch. | ||
|
|
||
| ### Create a Artifact Registry repository | ||
| Create a [Artifact Registry repository](https://docs.oracle.com/en-us/iaas/artifacts/using/manage-repos.htm#create-repo). | ||
|
|
||
| ### Create a DevOps Artifact for your artifact repository | ||
| Create a DevOps Artifact to point to the Artifact Registry repository location you just created above. Enter the information for the Artifact location: | ||
| 1. Name: `myGoAppArtifact` | ||
| <img src="create_artifact_go.png" /> | ||
| 2. Type: General Artifact | ||
| 3. Artifact source: Artifact registry Repository | ||
| Select your Artifact Registry repository created in previous step. | ||
| <img src="select_artifact_go.png" /> | ||
| 4. Artifact Location: Set custom location | ||
| 5. Path: `REGION/TENANCY-NAMESPACE/myGoWebAppArtifact` | ||
| 6. Replace parameters: Yes | ||
|
|
||
| Required policies must be added in the root compartment for the Artifact Registry repository and DevOps Artifact resource. | ||
| 1. Provide access to Generic Artifactory to deliver artifacts : ```Allow dynamic-group dg-with-devops-resources to manage repos in tenancy``` | ||
| 2. Provide access to read deploy artifacts in deliver artifact stage : ```Allow dynamic-group dg-with-devops-resources to manage devops-family in tenancy``` | ||
|
|
||
| ### Add a Deliver Artifacts stage | ||
| Let's add a **Deliver Artifacts** stage to your Build Pipeline to deliver the `golangWebApp` executable file to an OCI repository. | ||
| The Deliver Artifacts stage **maps** the ouput Artifacts from the Managed Build stage with the version to deliver to a DevOps Artifact resource, and then to the OCI repository. | ||
| Add a **Deliver Artifacts** stage to your Build Pipeline after the **Managed Build** stage. To configure this stage: | ||
| 1. In your Deliver Artifacts stage, choose `Select Artifact` | ||
| 2. From the list of artifacts select the `myGoAppArtifact` artifact that you created above. | ||
| <img src="deliver_artifact_go.png" /> | ||
| 3. In the next section, you'll assign the image outputArtifact from the `build_spec.yaml` to the DevOps project artifact. For the "Build config/result Artifact name" enter: `goWebAppArtifact` | ||
|
|
||
|
|
||
| ### Run your Build in OCI DevOps | ||
|
|
||
| #### From your Build Pipeline, choose `Manual Run` | ||
| Use the Manual Run button to start a Build Run | ||
|
|
||
| Manual Run will use the Primary Code Repository, will start the Build Pipeline, first running the Managed Build stage, followed by the Deliver Artifacts stage. | ||
|
|
||
| After the Build Pipeline execution is complete, we can view the executable file stored in the OCI Artifact Registry, which can then be downloaded to local workspace. |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| version: 0.1 | ||
| component: build | ||
| timeoutInSeconds: 6000 | ||
| runAs: root | ||
| shell: bash | ||
| env: | ||
| exportedVariables: | ||
| - version | ||
| steps: | ||
| - type: Command | ||
| name: "Build golang app" | ||
| timeoutInSeconds: 6000 | ||
| command: | | ||
| wget -nv https://dl.google.com/go/go1.16.5.linux-amd64.tar.gz | ||
| tar -C /usr/local -xzf go1.16.5.linux-amd64.tar.gz | ||
| rm -rf go1.16.5.linux-amd64.tar.gz | ||
| export PATH="/usr/local/go/bin:$PATH" | ||
| export GOPATH="/usr/local/go" | ||
| export PATH="$GOPATH/bin:$PATH" | ||
| go version | ||
| go build -o golangWebApp main.go | ||
| ls -a | ||
| echo "build finished.." | ||
|
|
||
| outputArtifacts: | ||
| - name: goWebAppArtifact | ||
| type: BINARY | ||
| location: golangWebApp | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package main | ||
| import ( | ||
| "net/http" | ||
| "fmt" | ||
| "time" | ||
| "html/template" | ||
| ) | ||
|
|
||
| //Create a struct that holds information to be displayed in our HTML file | ||
| type Welcome struct { | ||
| Name string | ||
| Time string | ||
| } | ||
|
|
||
| //Go application entrypoint | ||
| func main() { | ||
| //Instantiate a Welcome struct object and pass in some random information. | ||
| //We shall get the name of the user as a query parameter from the URL | ||
| welcome := Welcome{"Dharmesh", time.Now().Format(time.Stamp)} | ||
|
|
||
| //We tell Go exactly where we can find our html file. We ask Go to parse the html file (Notice | ||
| // the relative path). We wrap it in a call to template.Must() which handles any errors and halts if there are fatal errors | ||
|
|
||
| templates := template.Must(template.ParseFiles("template/welcome-template.html")) | ||
|
|
||
|
|
||
|
|
||
| //This method takes in the URL path "/" and a function that takes in a response writer, and a http request. | ||
| http.HandleFunc("/" , func(w http.ResponseWriter, r *http.Request) { | ||
|
|
||
| //Takes the name from the URL query e.g ?name=Martin, will set welcome.Name = Martin. | ||
| if name := r.FormValue("name"); name != "" { | ||
| welcome.Name = name; | ||
| } | ||
| //If errors show an internal server error message | ||
| //I also pass the welcome struct to the welcome-template.html file. | ||
| if err := templates.ExecuteTemplate(w, "welcome-template.html", welcome); err != nil { | ||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||
| } | ||
| }) | ||
|
|
||
| //Start the web server, set the port to listen to 8080. Without a path it assumes localhost | ||
| //Print any errors from starting the webserver using fmt | ||
| fmt.Println("Listening"); | ||
| fmt.Println(http.ListenAndServe(":8080", nil)); | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
examples/golang_webapp_example/template/welcome-template.html
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <link rel="stylesheet" href="/static/stylesheets/welcome-template.css"> | ||
|
|
||
| <!-- The welcome struct (shown in the main.go code) is received within the HTML and we just need to use the . operator and retrieve the information we want --> | ||
| <title>Welcome {{.Name}}</title> | ||
| </head> | ||
| <body> | ||
| <div class="welcome center">Welcome {{.Name}}, it is {{.Time}}</div> | ||
| </body> | ||
| </html> | ||
|
|
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Build Spec File Example using nodeJS web application and Creating an executable file | ||
|
|
||
| This is an example of a nodejs web application and creating an executable file. With the [OCI DevOps service](https://www.oracle.com/devops/devops-service/) and this project, you'll be able to build this application,store the executable file in [OCI Artifact Registry.](https://docs.oracle.com/en-us/iaas/artifacts/using/overview.htm) | ||
|
|
||
|
|
||
| ## Building the application locally | ||
|
|
||
| ### Download the repo | ||
| The first step to get started is to download the repository to your local workspace | ||
|
|
||
| ```shell | ||
| git clone git@github.com:dlcbld/BuildSpecSampleFiles.git | ||
| cd examples/nodejs_example | ||
| ``` | ||
|
|
||
| ### Install and run the application | ||
|
|
||
| 1. Install nodejs on your system: | ||
| ```yum install -y oracle-nodejs-release-el7 oracle-release-el7``` | ||
| 2. Check that path is set properly by displaying version: | ||
| ```npm -version``` | ||
| 3. Install the pkg library | ||
| ```npm install --global pkg``` | ||
| 4. Package the nodejs web application file alongwith necessary modules using following command: | ||
|
|
||
| ```pkg app.js``` | ||
| 5. Run the app: | ||
| ```./app-macos``` | ||
|
|
||
| ## Building the application in OCI DevOps | ||
| Now that you've seen you can locally build this app, let's try this out through OCI DevOps Build service. | ||
| ### Create External Connection to your Git repository | ||
| 1. Create a [DevOps Project](https://docs.oracle.com/en-us/iaas/Content/devops/using/devops_projects.htm) or use and an existing project. | ||
| 2. In your DevOps project, create an External Connection to your GitHub repository which holds your application. | ||
| - Create a Personal Access Token (PAT): https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token | ||
| - In the OCI Console, Go to Identity & Security -> Vault and create a [Vault]( https://docs.oracle.com/en-us/iaas/Content/KeyManagement/Concepts/keyoverview.htm) in compartment of your own choice. | ||
| - Create a Master Key that will be used to encrypt the PATs. | ||
| - Select Secrets from under Resources and create a secret using PAT obtained from GitHub account. | ||
| - Make a note of the OCID of the secret. | ||
| - Now, go to the desired project and select External Connection from the resources. | ||
| - Select type as GitHub and provide OCID of the secret under Personal Access Token. | ||
| - Finally, allow Build Service (dynamic group with DevOps Resources) to use a PAT secret by writing a policy in the root compartment as: ``` Allow dynamic-group dg-with-devops-resources to manage secret-family in tenancy``` | ||
| ### Setup your Build Pipeline | ||
| Create a new Build Pipeline to build, test and deliver artifacts from your GitHub Repository. | ||
| ### Managed Build stage | ||
| In your Build Pipeline, first add a Managed Build stage | ||
| 1. The Build Spec File Path is the relative location in your repo of the build_spec.yaml . Leave the default, for this example. | ||
| 2. For the Primary Code Repository follow the below steps | ||
| - Select connection type as GitHub | ||
| - Select the external connection you created above | ||
| - Give the URL to the repo which contains your application. | ||
| - Select main branch. | ||
|
|
||
| ### Create a Artifact Registry repository | ||
| Create a [Artifact Registry repository](https://docs.oracle.com/en-us/iaas/artifacts/using/manage-repos.htm#create-repo). | ||
|
|
||
| ### Create a DevOps Artifact for your artifact repository | ||
| Create a DevOps Artifact to point to the Artifact Registry repository location you just created above. Enter the information for the Artifact location: | ||
| 1. Name: `myNodeJSAppArtifact` | ||
| 2. Type: General Artifact | ||
| 3. Artifact source: Artifact registry Repository | ||
| Select your Artifact Registry repository created in previous step. | ||
| 4. Artifact Location: Set custom location | ||
| 4. Path: `REGION/TENANCY-NAMESPACE/myNodeJSAppArtifact` | ||
| 1. Replace parameters: Yes | ||
| ### Add a Deliver Artifacts stage | ||
| Let's add a **Deliver Artifacts** stage to your Build Pipeline to deliver the `app-macos` executable file to an OCI repository. | ||
| The Deliver Artifacts stage **maps** the ouput Artifacts from the Managed Build stage with the version to deliver to a DevOps Artifact resource, and then to the OCI repository. | ||
| Add a **Deliver Artifacts** stage to your Build Pipeline after the **Managed Build** stage. To configure this stage: | ||
| 1. In your Deliver Artifacts stage, choose `Select Artifact` | ||
| 2. From the list of artifacts select the `myNodeJSAppArtifact` artifact that you created above | ||
| 3. In the next section, you'll assign the container image outputArtifact from the `build_spec.yaml` to the DevOps project artifact. For the "Build config/result Artifact name" enter: `NodeJSAppArtifact` | ||
|
|
||
|
|
||
| ### Run your Build in OCI DevOps | ||
|
|
||
| #### From your Build Pipeline, choose `Manual Run` | ||
| Use the Manual Run button to start a Build Run | ||
|
|
||
| Manual Run will use the Primary Code Repository, will start the Build Pipeline, first running the Managed Build stage, followed by the Deliver Artifacts stage. | ||
|
|
||
| After the Build Pipeline execution is complete, we can view the executable file stored in the OCI Artifact Registry, which can then be downloaded to local workspace. |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const http = require('http'); | ||
|
|
||
| const hostname = '127.0.0.1'; | ||
| const port = 3000; | ||
|
|
||
| const server = http.createServer((req, res) => { | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Hello World'); | ||
| }); | ||
|
|
||
| server.listen(port, hostname, () => { | ||
| console.log(`Server running at http://${hostname}:${port}/`); | ||
| }); |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| version: 0.1 | ||
| component: build | ||
| timeoutInSeconds: 600 | ||
| runAs: root | ||
| shell: bash | ||
| env: | ||
| exportedVariables: | ||
| - version | ||
| steps: | ||
| - type: Command | ||
| name: "Build NodeJS app" | ||
| timeoutInSeconds: 600 | ||
| command: | | ||
| yum install -y oracle-nodejs-release-el7 oracle-release-el7 | ||
| npm -version | ||
| npm install --global pkg | ||
| pkg app.js | ||
| ls -la | ||
| echo "build finished.." | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add docker build as well here |
||
| outputArtifacts: | ||
| - name: NodeJSAppArtifact | ||
| type: BINARY | ||
| location: app-macos | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have a docker build as well here?